-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* sync config * app * add router, header and footer * routes * model urls * create api * fix import * fix env vars * default service name * simplify exports * handle query state * fix handleQueryState * use params required * on validation success * get location inside of router * fix rerenders * reuse query state handler * fix api caching * fix * prefer cache value * tagid to modelid * fix dependencies * fix type * add deps * sync * fix import * remove new deps * fix errors
- Loading branch information
Showing
25 changed files
with
500 additions
and
571 deletions.
There are no files selected for viewing
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
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
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
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
This file was deleted.
Oops, something went wrong.
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,96 @@ | ||
import { | ||
createApi as _createApi, | ||
fetchBaseQuery, | ||
} from "@reduxjs/toolkit/query/react" | ||
|
||
import { SERVICE_API_URL } from "../env" | ||
import { getCsrfCookie, logout } from "../utils/auth" | ||
import defaultTagTypes from "./tagTypes" | ||
|
||
// TODO: decide if we want to keep any of this. | ||
// export function handleResponseError(error: FetchBaseQueryError): void { | ||
// if ( | ||
// error.status === 400 && | ||
// typeof error.data === "object" && | ||
// error.data !== null | ||
// ) { | ||
// // Parse the error's data from snake_case to camelCase. | ||
// snakeCaseToCamelCase(error.data) | ||
// } else if (error.status === 401) { | ||
// // TODO: redirect to appropriate login page based on user type. | ||
// window.location.href = `${PORTAL_BASE_URL}/login/teacher` | ||
// } else { | ||
// // Catch-all error pages by status-code. | ||
// window.location.href = `${PORTAL_BASE_URL}/error/${ | ||
// [403, 404].includes(error.status as number) ? error.status : 500 | ||
// }` | ||
// } | ||
// } | ||
|
||
export default function createApi<TagTypes extends string = never>({ | ||
tagTypes = [], | ||
}: { | ||
tagTypes?: readonly TagTypes[] | ||
} = {}) { | ||
const fetch = fetchBaseQuery({ | ||
baseUrl: `${SERVICE_API_URL}/`, | ||
credentials: "include", | ||
prepareHeaders: (headers, { type }) => { | ||
if (type === "mutation") { | ||
let csrfToken = getCsrfCookie() | ||
if (csrfToken) headers.set("x-csrftoken", csrfToken) | ||
} | ||
|
||
return headers | ||
}, | ||
}) | ||
|
||
const api = _createApi({ | ||
// https://redux-toolkit.js.org/rtk-query/usage/customizing-queries#implementing-a-custom-basequery | ||
baseQuery: async (args, api, extraOptions) => { | ||
if (api.type === "mutation" && getCsrfCookie() === undefined) { | ||
// Get the CSRF token. | ||
const { error } = await fetch( | ||
{ url: "/csrf/cookie", method: "GET" }, | ||
api, | ||
{}, | ||
) | ||
|
||
// Validate we got the CSRF token. | ||
if (error !== undefined) { | ||
console.error(error) | ||
// TODO | ||
// window.location.href = `${PORTAL_BASE_URL}/error/500` | ||
} | ||
if (getCsrfCookie() === undefined) { | ||
// TODO | ||
// window.location.href = `${PORTAL_BASE_URL}/error/500` | ||
} | ||
} | ||
|
||
// Send the HTTP request and fetch the response. | ||
return await fetch(args, api, extraOptions) | ||
}, | ||
tagTypes: [...defaultTagTypes, ...tagTypes], | ||
endpoints: build => ({ | ||
logout: build.mutation<null, null>({ | ||
query: () => ({ | ||
url: "session/logout/", | ||
method: "POST", | ||
}), | ||
async onQueryStarted(_, { dispatch, queryFulfilled }) { | ||
try { | ||
await queryFulfilled | ||
} catch (error) { | ||
console.error("Failed to call logout endpoint...", error) | ||
} finally { | ||
logout() | ||
dispatch(api.util.resetApiState()) | ||
} | ||
}, | ||
}), | ||
}), | ||
}) | ||
|
||
return api | ||
} |
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,47 +1,8 @@ | ||
import getReadAuthFactorEndpoints, { | ||
type ListAuthFactorsArg, | ||
type ListAuthFactorsResult, | ||
AUTH_FACTOR_TAG, | ||
} from "./authFactor" | ||
import getReadClassEndpoints, { | ||
type ListClassesArg, | ||
type ListClassesResult, | ||
type RetrieveClassArg, | ||
type RetrieveClassResult, | ||
CLASS_TAG, | ||
} from "./klass" | ||
import getReadSchoolEndpoints, { | ||
type RetrieveSchoolArg, | ||
type RetrieveSchoolResult, | ||
SCHOOL_TAG, | ||
} from "./school" | ||
import getReadUserEndpoints, { | ||
type ListUsersArg, | ||
type ListUsersResult, | ||
type RetrieveUserArg, | ||
type RetrieveUserResult, | ||
USER_TAG, | ||
} from "./user" | ||
|
||
export { | ||
AUTH_FACTOR_TAG, | ||
CLASS_TAG, | ||
getReadAuthFactorEndpoints, | ||
getReadClassEndpoints, | ||
getReadSchoolEndpoints, | ||
getReadUserEndpoints, | ||
SCHOOL_TAG, | ||
USER_TAG, | ||
type ListAuthFactorsArg, | ||
type ListAuthFactorsResult, | ||
type ListClassesArg, | ||
type ListClassesResult, | ||
type ListUsersArg, | ||
type ListUsersResult, | ||
type RetrieveClassArg, | ||
type RetrieveClassResult, | ||
type RetrieveSchoolArg, | ||
type RetrieveSchoolResult, | ||
type RetrieveUserArg, | ||
type RetrieveUserResult, | ||
} | ||
export * from "./authFactor" | ||
export { default as getReadAuthFactorEndpoints } from "./authFactor" | ||
export * from "./klass" | ||
export { default as getReadClassEndpoints } from "./klass" | ||
export * from "./school" | ||
export { default as getReadSchoolEndpoints } from "./school" | ||
export * from "./user" | ||
export { default as getReadUserEndpoints } from "./user" |
Oops, something went wrong.