-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathuseApplications.ts
More file actions
57 lines (51 loc) · 1.62 KB
/
useApplications.ts
File metadata and controls
57 lines (51 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type { Ref } from 'vue'
import { usePreviewReadOnly } from '~/composables/usePreviewReadOnly'
/**
* Composable for managing the applications list with filtering, pagination, and mutations.
* Wraps `useFetch('/api/applications')` with a singleton key for shared state.
*/
export function useApplications(options?: {
jobId?: Ref<string | undefined> | string
candidateId?: Ref<string | undefined> | string
status?: Ref<string | undefined> | string
}) {
const { handlePreviewReadOnlyError } = usePreviewReadOnly()
const query = computed(() => ({
...(toValue(options?.jobId) && { jobId: toValue(options?.jobId) }),
...(toValue(options?.candidateId) && { candidateId: toValue(options?.candidateId) }),
...(toValue(options?.status) && { status: toValue(options?.status) }),
}))
const { data, status: fetchStatus, error, refresh } = useFetch('/api/applications', {
key: 'applications',
query,
headers: useRequestHeaders(['cookie']),
})
const applications = computed(() => data.value?.data ?? [])
const total = computed(() => data.value?.total ?? 0)
/** Create a new application (link candidate → job) and refresh the list */
async function createApplication(payload: {
candidateId: string
jobId: string
notes?: string
}) {
try {
const created = await $fetch('/api/applications', {
method: 'POST',
body: payload,
})
await refresh()
return created
} catch (error) {
handlePreviewReadOnlyError(error)
throw error
}
}
return {
applications,
total,
fetchStatus,
error,
refresh,
createApplication,
}
}