From dd7a8099efbd87d50cd21ed50fd2ce040800f08a Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Sun, 5 Feb 2023 11:26:34 -0900 Subject: [PATCH] improve unnecessary data fetches --- frontend/composables/use-api.ts | 2 +- frontend/layouts/default.vue | 16 ++++++++++++---- frontend/lib/requests/requests.ts | 9 ++++----- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/frontend/composables/use-api.ts b/frontend/composables/use-api.ts index cd06588..04c3bdc 100644 --- a/frontend/composables/use-api.ts +++ b/frontend/composables/use-api.ts @@ -4,7 +4,7 @@ import { Requests } from "~~/lib/requests"; import { useAuthStore } from "~~/stores/auth"; export type Observer = { - handler: (r: Response) => void; + handler: (r: Response, req?: RequestInit) => void; }; export type RemoveObserver = () => void; diff --git a/frontend/layouts/default.vue b/frontend/layouts/default.vue index 5006336..a81a4a2 100644 --- a/frontend/layouts/default.vue +++ b/frontend/layouts/default.vue @@ -181,11 +181,18 @@ }, ]; + function isMutation(method: string | undefined) { + return method === "POST" || method === "PUT" || method === "DELETE"; + } + function isSuccess(status: number) { + return status >= 200 && status < 300; + } + const labelStore = useLabelStore(); const reLabel = /\/api\/v1\/labels\/.*/gm; const rmLabelStoreObserver = defineObserver("labelStore", { - handler: r => { - if (r.status === 201 || r.url.match(reLabel)) { + handler: (resp, req) => { + if (isMutation(req?.method) && isSuccess(resp.status) && resp.url.match(reLabel)) { labelStore.refresh(); } console.debug("labelStore handler called by observer"); @@ -195,11 +202,12 @@ const locationStore = useLocationStore(); const reLocation = /\/api\/v1\/locations\/.*/gm; const rmLocationStoreObserver = defineObserver("locationStore", { - handler: r => { - if (r.status === 201 || r.url.match(reLocation)) { + handler: (resp, req) => { + if (isMutation(req?.method) && isSuccess(resp.status) && resp.url.match(reLocation)) { locationStore.refreshChildren(); locationStore.refreshParents(); } + console.debug("locationStore handler called by observer"); }, }); diff --git a/frontend/lib/requests/requests.ts b/frontend/lib/requests/requests.ts index 9d62c36..f8ed355 100644 --- a/frontend/lib/requests/requests.ts +++ b/frontend/lib/requests/requests.ts @@ -5,8 +5,7 @@ export enum Method { DELETE = "DELETE", } -export type RequestInterceptor = (r: Response) => void; -export type ResponseInterceptor = (r: Response) => void; +export type ResponseInterceptor = (r: Response, rq?: RequestInit) => void; export interface TResponse { status: number; @@ -32,8 +31,8 @@ export class Requests { this.responseInterceptors.push(interceptor); } - private callResponseInterceptors(response: Response) { - this.responseInterceptors.forEach(i => i(response)); + private callResponseInterceptors(response: Response, request?: RequestInit) { + this.responseInterceptors.forEach(i => i(response, request)); } private url(rest: string): string { @@ -90,7 +89,7 @@ export class Requests { } const response = await fetch(this.url(rargs.url), payload); - this.callResponseInterceptors(response); + this.callResponseInterceptors(response, payload); const data: T = await (async () => { if (response.status === 204) {