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 @@