From 5f1bee26cf2a986a1fa1afd8f4363336ab42b4ba Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Sun, 27 Feb 2022 22:30:00 -0800 Subject: [PATCH] Refactor some endpoints --- components/BottomHeader/index.tsx | 2 +- components/CharacterGrid/index.tsx | 2 +- components/SummonGrid/index.tsx | 2 +- components/WeaponGrid/index.tsx | 2 +- utils/api.tsx | 13 +++++++------ 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/components/BottomHeader/index.tsx b/components/BottomHeader/index.tsx index 30972b90..dd7cff0e 100644 --- a/components/BottomHeader/index.tsx +++ b/components/BottomHeader/index.tsx @@ -44,7 +44,7 @@ const BottomHeader = () => { function deleteTeam(event: React.MouseEvent) { if (appState.party.editable && appState.party.id) { - api.endpoints.parties.destroy(appState.party.id, headers) + api.endpoints.parties.destroy({ id: appState.party.id, params: headers }) .then(() => { // Push to route router.push('/') diff --git a/components/CharacterGrid/index.tsx b/components/CharacterGrid/index.tsx index f5429e01..3dc7659b 100644 --- a/components/CharacterGrid/index.tsx +++ b/components/CharacterGrid/index.tsx @@ -58,7 +58,7 @@ const CharacterGrid = (props: Props) => { // Methods: Fetching an object from the server async function fetchGrid(shortcode: string) { - return api.endpoints.parties.getOneWithObject({ id: shortcode, object: 'characters' }) + return api.endpoints.parties.getOneWithObject({ id: shortcode, object: 'characters', params: headers }) .then(response => processResult(response)) .catch(error => processError(error)) } diff --git a/components/SummonGrid/index.tsx b/components/SummonGrid/index.tsx index 97404734..42388f48 100644 --- a/components/SummonGrid/index.tsx +++ b/components/SummonGrid/index.tsx @@ -68,7 +68,7 @@ const SummonGrid = (props: Props) => { // Methods: Fetching an object from the server async function fetchGrid(shortcode: string) { - return api.endpoints.parties.getOneWithObject({ id: shortcode, object: 'summons' }) + return api.endpoints.parties.getOneWithObject({ id: shortcode, object: 'summons', params: headers }) .then(response => processResult(response)) .catch(error => processError(error)) } diff --git a/components/WeaponGrid/index.tsx b/components/WeaponGrid/index.tsx index 103bf728..140b7503 100644 --- a/components/WeaponGrid/index.tsx +++ b/components/WeaponGrid/index.tsx @@ -64,7 +64,7 @@ const WeaponGrid = (props: Props) => { // Methods: Fetching an object from the server async function fetchGrid(shortcode: string) { - return api.endpoints.parties.getOneWithObject({ id: shortcode, object: 'weapons' }) + return api.endpoints.parties.getOneWithObject({ id: shortcode, object: 'weapons', params: headers }) .then(response => processResult(response)) .catch(error => processError(error)) } diff --git a/utils/api.tsx b/utils/api.tsx index 683b2e71..5af1e299 100644 --- a/utils/api.tsx +++ b/utils/api.tsx @@ -1,15 +1,16 @@ import axios, { Axios, AxiosRequestConfig, AxiosResponse } from "axios" +import { appState } from "./appState" interface Entity { name: string } type CollectionEndpoint = (params?: {}) => Promise> -type IdEndpoint = ({ id }: { id: string }) => Promise> -type IdWithObjectEndpoint = ({ id, object }: { id: string, object: string }) => Promise> +type IdEndpoint = ({ id, params }: { id: string, params?: {} }) => Promise> +type IdWithObjectEndpoint = ({ id, object, params }: { id: string, object: string, params?: {} }) => Promise> type PostEndpoint = (object: {}, headers?: {}) => Promise> type PutEndpoint = (id: string, object: {}, headers?: {}) => Promise> -type DestroyEndpoint = (id: string, headers?: {}) => Promise> +type DestroyEndpoint = ({ id, params }: { id: string, params?: {} }) => Promise> interface EndpointMap { getAll: CollectionEndpoint @@ -42,11 +43,11 @@ class Api { return { getAll: (params?: {}) => axios.get(resourceUrl, params), - getOne: ({ id }: { id: string }) => axios.get(`${resourceUrl}/${id}/`), - getOneWithObject: ({ id, object }: { id: string, object: string }) => axios.get(`${resourceUrl}/${id}/${object}`), + getOne: ({ id, params }: { id: string, params?: {} }) => axios.get(`${resourceUrl}/${id}/`, params), + getOneWithObject: ({ id, object, params }: { id: string, object: string, params?: {} }) => axios.get(`${resourceUrl}/${id}/${object}`, params), create: (object: {}, headers?: {}) => axios.post(resourceUrl, object, headers), update: (id: string, object: {}, headers?: {}) => axios.put(`${resourceUrl}/${id}`, object, headers), - destroy: (id: string, headers?: {}) => axios.delete(`${resourceUrl}/${id}`, headers) + destroy: ({ id, params }: { id: string, params?: {} }) => axios.delete(`${resourceUrl}/${id}`, params) } as EndpointMap }