From 662d0b279d6ddc7a260e353cc2cad206d350d806 Mon Sep 17 00:00:00 2001 From: amandesai01 Date: Fri, 6 Sep 2024 22:18:24 +0530 Subject: [PATCH] chore(refactor): update dataflow for postings --- app/components/admin/Posting/Form.vue | 0 app/composables/postings.ts | 69 +++++++++++++++ app/composables/repositories.ts | 5 +- app/pages/admin/applications.vue | 7 +- app/pages/admin/postings/edit.vue | 117 ++++++-------------------- app/pages/admin/postings/index.vue | 11 +-- app/pages/admin/postings/new.vue | 114 +++++++++++++++++++++++++ server/api/posting/index.get.ts | 45 +++------- server/api/postings/index.get.ts | 43 +++++++--- server/api/postings/lite.get.ts | 19 +++++ shared/schemas/posting.ts | 4 + 11 files changed, 292 insertions(+), 142 deletions(-) create mode 100644 app/components/admin/Posting/Form.vue create mode 100644 app/composables/postings.ts create mode 100644 app/pages/admin/postings/new.vue create mode 100644 server/api/postings/lite.get.ts diff --git a/app/components/admin/Posting/Form.vue b/app/components/admin/Posting/Form.vue new file mode 100644 index 00000000..e69de29b diff --git a/app/composables/postings.ts b/app/composables/postings.ts new file mode 100644 index 00000000..f6a2451d --- /dev/null +++ b/app/composables/postings.ts @@ -0,0 +1,69 @@ +import type { z } from 'zod'; +import type { JobPosting } from '~~/server/db/schema'; +import type { + createJobPostingSchema, + deleteJobPostingSchema, + fetchJobPostingFilterSchema, + updateJobPostingSchema, +} from '~~/shared/schemas/posting'; + +export type PostingLite = { id: string; title: string }; +export type PostingsLite = PostingLite[]; +export function usePostingsLiteRepository( + options: { immediate?: boolean } = { immediate: true } +) { + return useObjectRepository< + PostingsLite, + never, + never, + never, + never, + never, + never + >({ + key: 'postings-lite', + fetchURL: '/api/postings/lite', + initFn: () => [], + immediate: options.immediate, + }); +} + +export type Postings = JobPosting[]; +export function usePostingsRepository() { + return useObjectRepository< + Postings, + never, + never, + never, + never, + never, + never + >({ + key: 'postings', + fetchURL: '/api/postings', + initFn: () => [], + }); +} + +export type CreatePostingSchema = z.infer; +export type UpdatePostingSchema = z.infer; +export type FetchPostingSchema = z.infer; +export type DeletePostingSchema = z.infer; +export function usePostingRepository(query: FetchPostingSchema) { + return useObjectRepository< + JobPosting, + FetchPostingSchema, + UpdatePostingSchema, + never, + CreatePostingSchema, + never, + DeletePostingSchema + >({ + key: `${query.id}-posting`, + fetchURL: '/api/posting', + fetchQuery: query, + postURL: '/api/posting', + updateURL: '/api/posting', + deleteURL: '/api/posting', + }); +} diff --git a/app/composables/repositories.ts b/app/composables/repositories.ts index 33bf9997..8175c795 100644 --- a/app/composables/repositories.ts +++ b/app/composables/repositories.ts @@ -47,6 +47,7 @@ export type LookupRepositoryOptions = { updateURL?: string; deleteURL?: string; initFn?: () => T; + immediate?: boolean; }; /** @@ -68,6 +69,8 @@ export async function useObjectRepository< >( options: LookupRepositoryOptions ): Promise> { + const immediate = + typeof options.immediate === 'undefined' ? true : options.immediate; const { data, setData, firstFetched, fetching, changing } = useObjectState( options.key, options.initFn @@ -91,7 +94,7 @@ export async function useObjectRepository< setFetchData(); }; - if (!firstFetched.value) { + if (!firstFetched.value && immediate) { firstFetched.value = true; await fetchExecute(); setFetchData(); diff --git a/app/pages/admin/applications.vue b/app/pages/admin/applications.vue index 693bcc04..7f52f1a0 100644 --- a/app/pages/admin/applications.vue +++ b/app/pages/admin/applications.vue @@ -16,8 +16,11 @@ const selectedPostings = ref([]); const postingsById = ref>({}); // id <> title; const { applicants, applications, fetch: fetchApplicants } = useApplications(); -const { data: postings } = - useFetch<{ id: string; title: string }[]>('/api/postings'); +const { data: postings, refresh } = await usePostingsLiteRepository({ + immediate: false, +}); + +onMounted(refresh); if (postingIdsQuery) { try { diff --git a/app/pages/admin/postings/edit.vue b/app/pages/admin/postings/edit.vue index b0aec95e..9e806edf 100644 --- a/app/pages/admin/postings/edit.vue +++ b/app/pages/admin/postings/edit.vue @@ -1,9 +1,5 @@ @@ -118,7 +67,8 @@ const onDelete = async () => { {{ isUpdating ? posting.title : 'New Posting' }} + /> + {{ data.title }} @@ -127,29 +77,18 @@ const onDelete = async () => { title="Delete Posting?" content="You won't be able to undo this action. You will loose access to applicant list." @confirm="onDelete" - v-if="isUpdating" > - { @confirm="onSubmit" >