Skip to content

Commit

Permalink
Use a class for ResponseInfo
Browse files Browse the repository at this point in the history
This allows us to have helper methods on the class.
  • Loading branch information
jnatten committed Nov 14, 2024
1 parent 9307cef commit 711ce65
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import "@fontsource/source-serif-pro/index.css";
import { i18nInstance } from "@ndla/ui";
import { getCookie, setCookie } from "@ndla/util";
import App from "./App";
import ResponseContext from "./components/ResponseContext";
import ResponseContext, { ResponseInfo } from "./components/ResponseContext";
import { VersionHashProvider } from "./components/VersionHashContext";
import { STORED_LANGUAGE_COOKIE_KEY } from "./constants";
import { getLocaleInfoFromPath, initializeI18n, isValidLocale, supportedLanguages } from "./i18n";
Expand Down Expand Up @@ -179,13 +179,14 @@ const renderOrHydrate = (container: HTMLElement, children: ReactNode) => {
hydrateRoot(container, children);
}
};
const responseContext = new ResponseInfo(serverResponse);

renderOrHydrate(
document.getElementById("root")!,
<HelmetProvider>
<I18nextProvider i18n={i18n}>
<ApolloProvider client={client}>
<ResponseContext.Provider value={{ status: serverResponse }}>
<ResponseContext.Provider value={responseContext}>
<VersionHashProvider value={versionHash}>
<LanguageWrapper basename={basename} />
</VersionHashProvider>
Expand Down
11 changes: 10 additions & 1 deletion src/components/ResponseContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@

import { createContext } from "react";

export interface ResponseInfo {
export class ResponseInfo {
status?: number;

constructor(status?: number) {
this.status = status;
}

isAccessDeniedError(): boolean {
return this.status === 401 || this.status === 403;
}
}

const ResponseContext = createContext<ResponseInfo | undefined>(undefined);
export default ResponseContext;
2 changes: 1 addition & 1 deletion src/server/render/defaultRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const defaultRender: RenderFunc = async (req) => {
const client = createApolloClient(locale, versionHash, req.path);
const i18n = initializeI18n(i18nInstance, locale);
const redirectContext: RedirectInfo = {};
const responseContext: ResponseInfo = {};
const responseContext: ResponseInfo = new ResponseInfo();
// @ts-ignore
const helmetContext: FilledContext = {};

Expand Down

0 comments on commit 711ce65

Please sign in to comment.