Skip to content

Commit 895de2a

Browse files
committed
Refactor role API composables: consolidate role creation, editing, and deletion into a single useRolesApi composable; update related components and routes to utilize the new structure.
1 parent 0b67bc1 commit 895de2a

File tree

16 files changed

+254
-255
lines changed

16 files changed

+254
-255
lines changed

packages/sprinkle-admin/app/assets/composables/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ export { usePermissionApi } from './usePermissionApi'
1414

1515
// Role
1616
export { useRoleApi } from './useRoleApi'
17-
export { useRoleCreateApi } from './useRoleCreateApi'
18-
export { useRoleEditApi } from './useRoleEditApi'
19-
export { useRoleDeleteApi } from './useRoleDeleteApi'
17+
export { useRolesApi } from './useRolesApi'
2018
export { useRoleUpdateApi } from './useRoleUpdateApi'
2119
export { useRolePermissionsApi } from './useRolePermissionsApi'
2220

packages/sprinkle-admin/app/assets/composables/useRoleCreateApi.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

packages/sprinkle-admin/app/assets/composables/useRoleDeleteApi.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

packages/sprinkle-admin/app/assets/composables/useRoleEditApi.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import { ref, toValue } from 'vue'
2+
import axios from 'axios'
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'
16+
17+
/**
18+
* API Composable
19+
*/
20+
export function useRolesApi() {
21+
const defaultFormData = (): RoleCreateRequest => ({
22+
name: '',
23+
slug: '',
24+
description: ''
25+
})
26+
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))
33+
34+
async function fetchRole(slug: string) {
35+
apiLoading.value = true
36+
apiError.value = null
37+
38+
return axios
39+
.get<RoleResponse>('/api/roles/r/' + toValue(slug))
40+
.then((response) => {
41+
return response.data
42+
})
43+
.catch((err) => {
44+
apiError.value = err.response.data
45+
46+
throw apiError.value
47+
})
48+
.finally(() => {
49+
apiLoading.value = false
50+
})
51+
}
52+
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
91+
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+
}
137+
}

packages/sprinkle-admin/app/assets/routes/RolesRoutes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default [
2121
path: 'r/:slug', // roles/r/{slug}
2222
name: 'admin.role',
2323
meta: {
24+
title: 'ROLE.PAGE',
2425
description: 'ROLE.INFO_PAGE',
2526
permission: {
2627
slug: 'uri_role'

packages/sprinkle-admin/app/schema/requests/role/create.yaml renamed to packages/sprinkle-admin/app/schema/requests/role.yaml

File renamed without changes.

packages/sprinkle-admin/app/schema/requests/role/edit-info.yaml

Lines changed: 0 additions & 26 deletions
This file was deleted.

packages/sprinkle-admin/app/src/Controller/Role/RoleCreateAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
class RoleCreateAction
4545
{
4646
// Request schema for client side form validation
47-
protected string $schema = 'schema://requests/role/create.yaml';
47+
protected string $schema = 'schema://requests/role.yaml';
4848

4949
/**
5050
* Inject dependencies.

packages/sprinkle-admin/app/src/Controller/Role/RoleEditAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
class RoleEditAction
4646
{
4747
// Request schema for client side form validation
48-
protected string $schema = 'schema://requests/role/edit-info.yaml';
48+
protected string $schema = 'schema://requests/role.yaml';
4949

5050
/**
5151
* Inject dependencies.

0 commit comments

Comments
 (0)