From 90813abf76f6ffefdc2f0f7a05fcfb394f291fb4 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Mon, 12 Sep 2022 19:36:22 -0800 Subject: [PATCH] fix(frontend): fix mis-matched state errors (#9) implenmented a store for each global item and tied it to an event bus and used the listener on the requests object to intercept responses from the API and run the appripriate get call when updates were detected. --- frontend/components/Form/TextArea.vue | 1 - frontend/components/Item/CreateModal.vue | 14 ++--- frontend/components/Location/CreateModal.vue | 2 - frontend/composables/use-api.ts | 20 +++++++ frontend/composables/use-events.ts | 38 +++++++++++++ frontend/layouts/home.vue | 60 ++++++++++++++++++++ frontend/pages/home.vue | 26 +++++---- frontend/pages/item/[id]/edit.vue | 19 ++++--- frontend/pages/item/[id]/index.vue | 2 - frontend/stores/items.ts | 33 +++++++++++ frontend/stores/labels.ts | 33 +++++++++++ frontend/stores/locations.ts | 33 +++++++++++ 12 files changed, 247 insertions(+), 34 deletions(-) create mode 100644 frontend/composables/use-events.ts create mode 100644 frontend/stores/items.ts create mode 100644 frontend/stores/labels.ts create mode 100644 frontend/stores/locations.ts diff --git a/frontend/components/Form/TextArea.vue b/frontend/components/Form/TextArea.vue index 1157f11..0580f8e 100644 --- a/frontend/components/Form/TextArea.vue +++ b/frontend/components/Form/TextArea.vue @@ -60,7 +60,6 @@ } onUpdated(() => { - console.log("updated"); if (props.inline) { setHeight(); } diff --git a/frontend/components/Item/CreateModal.vue b/frontend/components/Item/CreateModal.vue index 4d8a123..ffc192a 100644 --- a/frontend/components/Item/CreateModal.vue +++ b/frontend/components/Item/CreateModal.vue @@ -27,6 +27,8 @@ diff --git a/frontend/composables/use-api.ts b/frontend/composables/use-api.ts index 2a51fd7..4eb753f 100644 --- a/frontend/composables/use-api.ts +++ b/frontend/composables/use-api.ts @@ -3,6 +3,22 @@ import { UserApi } from "~~/lib/api/user"; import { Requests } from "~~/lib/requests"; import { useAuthStore } from "~~/stores/auth"; +export type Observer = { + handler: (r: Response) => void; +}; + +export type RemoveObserver = () => void; + +const observers: Record = {}; + +export function defineObserver(key: string, observer: Observer): RemoveObserver { + observers[key] = observer; + + return () => { + delete observers[key]; + }; +} + function logger(r: Response) { console.log(`${r.status} ${r.url} ${r.statusText}`); } @@ -23,5 +39,9 @@ export function useUserApi(): UserApi { } }); + for (const [_, observer] of Object.entries(observers)) { + requests.addResponseInterceptor(observer.handler); + } + return new UserApi(requests); } diff --git a/frontend/composables/use-events.ts b/frontend/composables/use-events.ts new file mode 100644 index 0000000..3c1af07 --- /dev/null +++ b/frontend/composables/use-events.ts @@ -0,0 +1,38 @@ +export enum EventTypes { + // ClearStores event is used to inform the stores that _all_ the data they are using + // is now out of date and they should refresh - This is used when the user makes large + // changes to the data such as bulk actions or importing a CSV file + ClearStores, +} + +export type EventFn = () => void; + +export interface IEventBus { + on(event: EventTypes, fn: EventFn, key: string): void; + off(event: EventTypes, key: string): void; + emit(event: EventTypes): void; +} + +class EventBus implements IEventBus { + private listeners: Record> = { + [EventTypes.ClearStores]: {}, + }; + + on(event: EventTypes, fn: EventFn, key: string): void { + this.listeners[event][key] = fn; + } + + off(event: EventTypes, key: string): void { + delete this.listeners[event][key]; + } + + emit(event: EventTypes): void { + Object.values(this.listeners[event]).forEach(fn => fn()); + } +} + +const bus = new EventBus(); + +export function useEventBus(): IEventBus { + return bus; +} diff --git a/frontend/layouts/home.vue b/frontend/layouts/home.vue index 634897b..9f0adc7 100644 --- a/frontend/layouts/home.vue +++ b/frontend/layouts/home.vue @@ -7,3 +7,63 @@ + + diff --git a/frontend/pages/home.vue b/frontend/pages/home.vue index 599cf50..ae6d8c1 100644 --- a/frontend/pages/home.vue +++ b/frontend/pages/home.vue @@ -1,4 +1,8 @@ diff --git a/frontend/pages/item/[id]/edit.vue b/frontend/pages/item/[id]/edit.vue index 34286d0..afc5d88 100644 --- a/frontend/pages/item/[id]/edit.vue +++ b/frontend/pages/item/[id]/edit.vue @@ -1,5 +1,7 @@