|
1 | 1 | <template>
|
2 |
| - <v-dialog width="700"> |
| 2 | + <v-dialog v-model="isActive" @update:model-value="close" width="700"> |
3 | 3 | <v-card rounded="lg">
|
4 | 4 | <v-card-title>
|
5 |
| - <h3 class="font-weight-regular text-h6">{{ compose.title }}</h3> |
| 5 | + <h3 class="font-weight-regular text-h6">{{ action.title }}</h3> |
6 | 6 | </v-card-title>
|
7 | 7 |
|
8 | 8 | <v-skeleton-loader
|
|
44 | 44 | :value="item[index].id"
|
45 | 45 | hide-details
|
46 | 46 | color="primary"
|
47 |
| - ></v-checkbox> |
48 |
| - <v-checkbox v-else :label="label" hide-details disabled></v-checkbox> |
| 47 | + /> |
| 48 | + <v-checkbox v-else :label="label" hide-details disabled /> |
49 | 49 | </td>
|
50 | 50 | </tr>
|
51 | 51 | </tbody>
|
52 | 52 | </v-table>
|
53 | 53 | </v-card-text>
|
54 | 54 |
|
55 | 55 | <v-card-actions class="pa-4">
|
56 |
| - <v-btn @click="isOpen = false" variant="flat" color="red">Discard</v-btn> |
| 56 | + <v-btn @click="close" variant="flat" color="red">Discard</v-btn> |
57 | 57 | <v-btn
|
58 | 58 | :disabled="form.busy || !meta.valid"
|
59 | 59 | :loading="form.busy"
|
|
62 | 62 | class="ms-4"
|
63 | 63 | @click="saveOrUpdate"
|
64 | 64 | >
|
65 |
| - {{ compose.action }} |
| 65 | + {{ action.saveButton }} |
66 | 66 | </v-btn>
|
67 | 67 | </v-card-actions>
|
68 | 68 | </Form>
|
|
71 | 71 | </template>
|
72 | 72 |
|
73 | 73 | <script setup>
|
74 |
| -import { ref, computed, onMounted } from 'vue' |
| 74 | +import { ref, computed } from 'vue' |
75 | 75 | import { Form } from 'vee-validate'
|
76 | 76 | import { useAppStore } from '@/stores/app'
|
77 |
| -import { useForm } from '@/composables/useForm' |
| 77 | +import { useForm, useEdit } from '@/composables' |
78 | 78 | import { createRoleValidation } from '@/validations/roles'
|
| 79 | +import { groupBy } from '@/utils' |
79 | 80 | import axios from '@/plugins/axios'
|
80 | 81 |
|
81 |
| -const emit = defineEmits(['created']) |
82 |
| -const props = defineProps({ |
83 |
| - compose: Object, |
84 |
| -}) |
| 82 | +const emit = defineEmits(['created', 'update:modelValue']) |
85 | 83 |
|
86 | 84 | const isLoading = ref(false)
|
87 |
| -const updateId = ref(null) |
88 | 85 | const permissions = ref([])
|
89 | 86 | const normalizePermissions = computed(() => groupBy(permissions.value, 'group_name'))
|
90 | 87 | const { notify } = useAppStore()
|
91 | 88 | const form = useForm({
|
92 | 89 | name: '',
|
93 | 90 | permissions: [],
|
94 | 91 | })
|
95 |
| -
|
96 |
| -// Lifecycle hooks |
97 |
| -
|
98 |
| -onMounted(async () => { |
99 |
| - permissions.value = (await axios.get('/permissions')).data.data |
100 |
| -}) |
101 |
| -
|
102 |
| -props.compose.onUpdate(() => { |
103 |
| - const data = props.compose.data |
104 |
| - if (data) { |
105 |
| - form.fill({ |
106 |
| - name: data.name, |
107 |
| - permissions: permissions.value.map(({ id }) => id), |
108 |
| - }) |
109 |
| - form._method = 'PUT' |
110 |
| - updateId.value = data.id |
111 |
| - } else { |
112 |
| - form.reset() |
113 |
| - } |
| 92 | +const { isActive, isEditing, onCreate, onEdit, editPayload, close, action } = useEdit(form, { |
| 93 | + title: 'Role', |
114 | 94 | })
|
115 | 95 |
|
116 |
| -// Functions |
117 |
| -
|
118 |
| -const groupBy = (xs, key) => { |
119 |
| - return xs.reduce((rv, x) => { |
120 |
| - ;(rv[x[key]] = rv[x[key]] || []).push(x) |
121 |
| - return rv |
122 |
| - }, {}) |
| 96 | +const fetchPermissions = async () => { |
| 97 | + const { data } = await axios.get('/permissions') |
| 98 | + permissions.value = data.data |
123 | 99 | }
|
124 | 100 |
|
125 |
| -const saveOrUpdate = () => { |
126 |
| - props.compose.isUpdating ? _update() : _save() |
127 |
| -} |
128 |
| -
|
129 |
| -const _update = () => { |
130 |
| - form.post(`/roles/${updateId.value}`).then(() => _reset()) |
131 |
| -} |
| 101 | +fetchPermissions() |
132 | 102 |
|
133 |
| -const _save = () => { |
134 |
| - form.post('/roles').then(() => _reset()) |
| 103 | +const saveOrUpdate = async () => { |
| 104 | + if (isEditing()) { |
| 105 | + await form.post(`/roles/${editPayload.value.id}`) |
| 106 | + } else { |
| 107 | + await form.post('/roles') |
| 108 | + } |
| 109 | + _reset() |
135 | 110 | }
|
136 | 111 |
|
137 |
| -// const prepareForUpdate = (id) => { |
138 |
| -// isLoading.value = true |
139 |
| -// axios.get(`/roles/${id}`).then(({ data: { data } }) => { |
140 |
| -// updateId.value = id |
141 |
| -// isLoading.value = false |
142 |
| -// form.fill({ |
143 |
| -// name: data.name, |
144 |
| -// permissions: data.permissions.map(({ id }) => id), |
145 |
| -// }) |
146 |
| -// form._method = 'PUT' |
147 |
| -// }) |
148 |
| -// } |
149 | 112 | const _reset = () => {
|
150 |
| - props.compose.close() |
151 | 113 | emit('created')
|
152 |
| - notify( |
153 |
| - props.compose.isUpdating |
154 |
| - ? 'The role has been successfully updated!' |
155 |
| - : 'A new role has been successfully created!' |
156 |
| - ) |
| 114 | + const message = isEditing() |
| 115 | + ? 'The role has been successfully updated!' |
| 116 | + : 'A new role has been successfully created!' |
| 117 | + notify(message) |
| 118 | + close() |
157 | 119 | }
|
158 | 120 |
|
159 |
| -// const open = (id) => { |
160 |
| -// if (id) { |
161 |
| -// isEdit.value = true |
162 |
| -// prepareForUpdate(id) |
163 |
| -// } else { |
164 |
| -// isEdit.value = false |
165 |
| -// form.reset() |
166 |
| -// } |
167 |
| -// isOpen.value = true |
168 |
| -// } |
169 |
| -
|
170 |
| -// defineExpose({ open }) |
| 121 | +defineExpose({ |
| 122 | + create: onCreate, |
| 123 | + edit: (data) => |
| 124 | + onEdit({ |
| 125 | + ...data, |
| 126 | + permissions: data.permissions.map(({ id }) => id), |
| 127 | + }), |
| 128 | +}) |
171 | 129 | </script>
|
0 commit comments