-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(troubleshooting page): Refactor data fetching
- Loading branch information
Showing
2 changed files
with
69 additions
and
8 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
frontend/app/(defaultLayout)/app-router/Troubleshooting/[device]/[problem]/[wikiid]/data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { | ||
IFixitAPIClient, | ||
VarnishBypassHeader, | ||
} from '@ifixit/ifixit-api-client'; | ||
import { SentryError } from '@ifixit/sentry'; | ||
import { TroubleshootingApiData } from '@templates/troubleshooting/hooks/useTroubleshootingProps'; | ||
import { ifixitOrigin } from 'app/_helpers/app-helpers'; | ||
import { cache } from 'react'; | ||
|
||
export const findProblemWiki = cache( | ||
async (wikiid: string): Promise<TroubleshootingApiData | null> => { | ||
const client = new IFixitAPIClient({ | ||
origin: ifixitOrigin(), | ||
headers: VarnishBypassHeader, | ||
}); | ||
|
||
const resp = await client.getRaw( | ||
`Troubleshooting/wikiid/${wikiid}`, | ||
'troubleshooting' | ||
); | ||
|
||
if (resp.status === 404) { | ||
return null; | ||
} | ||
|
||
if (!resp.ok) { | ||
throw new SentryError(resp.statusText); | ||
} | ||
|
||
return await resp.json(); | ||
} | ||
); |
45 changes: 37 additions & 8 deletions
45
frontend/app/(defaultLayout)/app-router/Troubleshooting/[device]/[problem]/[wikiid]/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,53 @@ | ||
import { flags } from '@config/flags'; | ||
import { notFound } from 'next/navigation'; | ||
import { notFound, redirect } from 'next/navigation'; | ||
import { findProblemWiki } from './data'; | ||
import { invariant } from '@ifixit/helpers'; | ||
|
||
interface WikiPageProps { | ||
interface ProblemWikiPageProps { | ||
params: { | ||
device: string; | ||
problem: string; | ||
wikiid: string; | ||
}; | ||
searchParams: { | ||
disableCacheGets?: string | string[] | undefined; | ||
variant?: string; | ||
}; | ||
searchParams: Record<string, any>; | ||
} | ||
|
||
export default async function WikiPage({ params }: WikiPageProps) { | ||
export default async function ProblemWikiPage({ | ||
params, | ||
searchParams, | ||
}: ProblemWikiPageProps) { | ||
if (!flags.APP_ROUTER_TROUBLESHOOTING_PAGE_ENABLED) notFound(); | ||
|
||
const problemWiki = await findProblemWiki(params.wikiid); | ||
|
||
if (problemWiki == null) notFound(); | ||
|
||
if (currentPath() !== canonicalPath()) | ||
redirect(canonicalPath() + currentSearchParams()); | ||
|
||
return ( | ||
<div> | ||
WikiPage: <code>{JSON.stringify(params, null, 2)}</code> | ||
<h1>{problemWiki.title}</h1> | ||
</div> | ||
); | ||
|
||
function currentPath() { | ||
return `/Troubleshooting/${params.device}/${problemHandle()}/${ | ||
params.wikiid | ||
}`; | ||
} | ||
|
||
function canonicalPath() { | ||
invariant(problemWiki); | ||
return new URL(problemWiki.canonicalUrl).pathname; | ||
} | ||
|
||
function problemHandle() { | ||
return decodeURIComponent(params.problem); | ||
} | ||
|
||
function currentSearchParams() { | ||
if (Object.keys(searchParams).length === 0) return ''; | ||
return `?${new URLSearchParams(searchParams)}`; | ||
} | ||
} |