|
1 | | -import { ref, toValue, watchEffect } from 'vue' |
| 1 | +import { ref, toValue } from 'vue' |
2 | 2 | import axios from 'axios' |
3 | | -import type { ApiErrorResponse } from '@userfrosting/sprinkle-core/interfaces' |
4 | | -import type { RoleResponse } from '../interfaces' |
| 3 | +import { useRegle } from '@regle/core' |
| 4 | +import { Severity, type ApiErrorResponse } from '@userfrosting/sprinkle-core/interfaces' |
| 5 | +import type { |
| 6 | + RoleCreateResponse, |
| 7 | + RoleCreateRequest, |
| 8 | + RoleEditRequest, |
| 9 | + RoleEditResponse, |
| 10 | + RoleDeleteResponse, |
| 11 | + RoleResponse |
| 12 | +} from '../interfaces' |
| 13 | +import { useAlertsStore } from '@userfrosting/sprinkle-core/stores' |
| 14 | +import { useRuleSchemaAdapter } from '@userfrosting/sprinkle-core/composables' |
| 15 | +import schemaFile from '../../schema/requests/role.yaml?raw' |
5 | 16 |
|
6 | 17 | /** |
7 | | - * API used to fetch data about a specific role. |
8 | | - * |
9 | | - * This interface is tied to the `RoleApi` API, accessed at the GET |
10 | | - * `/api/roles/r/{slug}` endpoint and the `RoleResponse` Typescript |
11 | | - * interface. |
12 | | - * |
13 | | - * This composable accept a {slug} to select the role. Any changes to the |
14 | | - * {slug} is watched and will trigger an update. |
15 | | - * |
16 | | - * Available ref: |
17 | | - * - role: RoleResponse |
18 | | - * - error: AlertInterface | null |
19 | | - * - loading: boolean |
20 | | - * - fetchRole(): void - Trigger a refresh of the user data |
| 18 | + * API Composable |
21 | 19 | */ |
22 | | -export function useRoleApi(slug: any) { |
23 | | - const loading = ref(false) |
24 | | - const error = ref<ApiErrorResponse | null>() |
25 | | - const role = ref<RoleResponse>({ |
26 | | - id: 0, |
27 | | - slug: '', |
| 20 | +export function useRoleApi() { |
| 21 | + const defaultFormData = (): RoleCreateRequest => ({ |
28 | 22 | name: '', |
29 | | - description: '', |
30 | | - created_at: '', |
31 | | - updated_at: '', |
32 | | - deleted_at: null, |
33 | | - users_count: 0 |
| 23 | + slug: '', |
| 24 | + description: '' |
34 | 25 | }) |
35 | 26 |
|
36 | | - async function fetchRole() { |
37 | | - loading.value = true |
38 | | - error.value = null |
| 27 | + const apiLoading = ref<boolean>(false) |
| 28 | + const apiError = ref<ApiErrorResponse | null>(null) |
| 29 | + const formData = ref<RoleCreateRequest>(defaultFormData()) |
| 30 | + |
| 31 | + // Load the schema and set up the validator |
| 32 | + const { r$ } = useRegle(formData, useRuleSchemaAdapter().adapt(schemaFile)) |
39 | 33 |
|
40 | | - await axios |
| 34 | + async function fetchRole(slug: string) { |
| 35 | + apiLoading.value = true |
| 36 | + apiError.value = null |
| 37 | + |
| 38 | + return axios |
41 | 39 | .get<RoleResponse>('/api/roles/r/' + toValue(slug)) |
42 | 40 | .then((response) => { |
43 | | - role.value = response.data |
| 41 | + return response.data |
44 | 42 | }) |
45 | 43 | .catch((err) => { |
46 | | - error.value = err.response.data |
| 44 | + apiError.value = err.response.data |
| 45 | + |
| 46 | + throw apiError.value |
47 | 47 | }) |
48 | 48 | .finally(() => { |
49 | | - loading.value = false |
| 49 | + apiLoading.value = false |
50 | 50 | }) |
51 | 51 | } |
52 | 52 |
|
53 | | - watchEffect(() => { |
54 | | - fetchRole() |
55 | | - }) |
| 53 | + async function createRole(data: RoleCreateRequest) { |
| 54 | + apiLoading.value = true |
| 55 | + apiError.value = null |
| 56 | + return axios |
| 57 | + .post<RoleCreateResponse>('/api/roles', data) |
| 58 | + .then((response) => { |
| 59 | + // Add the message to the alert stream |
| 60 | + useAlertsStore().push({ |
| 61 | + title: response.data.title, |
| 62 | + description: response.data.description, |
| 63 | + style: Severity.Success |
| 64 | + }) |
| 65 | + }) |
| 66 | + .catch((err) => { |
| 67 | + apiError.value = err.response.data |
| 68 | + |
| 69 | + throw apiError.value |
| 70 | + }) |
| 71 | + .finally(() => { |
| 72 | + apiLoading.value = false |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + async function updateRole(slug: string, data: RoleEditRequest) { |
| 77 | + apiLoading.value = true |
| 78 | + apiError.value = null |
| 79 | + return axios |
| 80 | + .put<RoleEditResponse>('/api/roles/r/' + slug, data) |
| 81 | + .then((response) => { |
| 82 | + // Add the message to the alert stream |
| 83 | + useAlertsStore().push({ |
| 84 | + title: response.data.title, |
| 85 | + description: response.data.description, |
| 86 | + style: Severity.Success |
| 87 | + }) |
| 88 | + }) |
| 89 | + .catch((err) => { |
| 90 | + apiError.value = err.response.data |
56 | 91 |
|
57 | | - return { role, error, loading, fetchRole } |
| 92 | + throw apiError.value |
| 93 | + }) |
| 94 | + .finally(() => { |
| 95 | + apiLoading.value = false |
| 96 | + }) |
| 97 | + } |
| 98 | + |
| 99 | + async function deleteRole(slug: string) { |
| 100 | + apiLoading.value = true |
| 101 | + apiError.value = null |
| 102 | + return axios |
| 103 | + .delete<RoleDeleteResponse>('/api/roles/r/' + slug) |
| 104 | + .then((response) => { |
| 105 | + // Add the message to the alert stream |
| 106 | + useAlertsStore().push({ |
| 107 | + title: response.data.title, |
| 108 | + description: response.data.description, |
| 109 | + style: Severity.Success |
| 110 | + }) |
| 111 | + }) |
| 112 | + .catch((err) => { |
| 113 | + apiError.value = err.response.data |
| 114 | + |
| 115 | + throw apiError.value |
| 116 | + }) |
| 117 | + .finally(() => { |
| 118 | + apiLoading.value = false |
| 119 | + }) |
| 120 | + } |
| 121 | + |
| 122 | + function resetForm() { |
| 123 | + formData.value = defaultFormData() |
| 124 | + } |
| 125 | + |
| 126 | + return { |
| 127 | + fetchRole, |
| 128 | + createRole, |
| 129 | + updateRole, |
| 130 | + deleteRole, |
| 131 | + apiLoading, |
| 132 | + apiError, |
| 133 | + formData, |
| 134 | + r$, |
| 135 | + resetForm |
| 136 | + } |
58 | 137 | } |
0 commit comments