Skip to content

Commit

Permalink
Use resolvedUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
canac committed Jan 7, 2025
1 parent 8063388 commit c8c0ef0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
8 changes: 4 additions & 4 deletions pages/accountLists/[accountListId].page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ const AccountListIdPage = ({
};

export const getServerSideProps = makeGetServerSideProps(
async (session, { query, req }) => {
async (session, { query, resolvedUrl }) => {
const underscoreRedirect = await handleUnderscoreAccountListRedirect(
session,
req.url,
resolvedUrl,
);
if (underscoreRedirect) {
return underscoreRedirect;
Expand Down Expand Up @@ -132,7 +132,7 @@ export const getServerSideProps = makeGetServerSideProps(
error.graphQLErrors.every(
(error) => !isAccountListNotFoundError(error),
);
if (!req.url || nonAccountListError) {
if (nonAccountListError) {
return {
redirect: {
destination: '/',
Expand All @@ -149,7 +149,7 @@ export const getServerSideProps = makeGetServerSideProps(
return {
redirect: {
destination: replaceUrlAccountList(
req.url,
resolvedUrl,
data.user.defaultAccountList,
),
permanent: false,
Expand Down
7 changes: 3 additions & 4 deletions pages/api/utils/pagePropsHelpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ jest.mock('next-auth/react');
jest.mock('src/lib/apollo/ssrClient', () => jest.fn());

const context = {
req: {},
query: { accountListId: 'account-list-1' },
resolvedUrl: '/page?param=value',
} as unknown as GetServerSidePropsContext;
Expand Down Expand Up @@ -78,9 +77,9 @@ describe('pagePropsHelpers', () => {

describe('redirects to the default account list if the URL contains "_"', () => {
const context = {
req: { url: '/accountLists/_/contacts' },
resolvedUrl: '/filters?param=value',
resolvedUrl: '/accountLists/_/contacts',
} as unknown as GetServerSidePropsContext;

beforeEach(() => {
const user = { apiToken: 'token' };
(getSession as jest.Mock).mockResolvedValue({ user });
Expand Down Expand Up @@ -109,7 +108,7 @@ describe('pagePropsHelpers', () => {
it('redirects to dashboard with default account list"', async () => {
await expect(
ensureSessionAndAccountList({
req: { url: '/accountLists/_' },
resolvedUrl: '/accountLists/_',
} as unknown as GetServerSidePropsContext),
).resolves.toMatchObject({
redirect: {
Expand Down
8 changes: 4 additions & 4 deletions pages/api/utils/pagePropsHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const enforceAdmin: GetServerSideProps<PagePropsWithSession> = async (

const underscoreRedirect = await handleUnderscoreAccountListRedirect(
session,
context.req.url,
context.resolvedUrl,
);
if (underscoreRedirect) {
return underscoreRedirect;
Expand All @@ -66,7 +66,7 @@ export const ensureSessionAndAccountList: GetServerSideProps<

const underscoreRedirect = await handleUnderscoreAccountListRedirect(
session,
context.req.url,
context.resolvedUrl,
);
if (underscoreRedirect) {
return underscoreRedirect;
Expand All @@ -81,9 +81,9 @@ export const ensureSessionAndAccountList: GetServerSideProps<

export const handleUnderscoreAccountListRedirect = async (
session: Session,
url?: string,
url: string,
): Promise<{ redirect: Redirect } | undefined> => {
if (url?.startsWith('/accountLists/_')) {
if (url.startsWith('/accountLists/_')) {
// Redirect to the default account list if the "_" is where the account list ID would be in the URL
// This is a common pattern in our app, so we handle it here to avoid repeating
const ssrClient = makeSsrClient(session.user.apiToken);
Expand Down

0 comments on commit c8c0ef0

Please sign in to comment.