Skip to content

Commit b9178f8

Browse files
refactor: drop useComposer hoc and replace it with useEdit composable (#3)
1 parent 1451bdf commit b9178f8

File tree

21 files changed

+197
-301
lines changed

21 files changed

+197
-301
lines changed

src/components/dashboard/bar/SettingsMenu.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<v-img
1313
aspect-ratio="1/1"
1414
cover
15-
:src="authStore.user.avatar"
15+
:src="authStore.user.avatar_url"
1616
width="40"
1717
class="rounded-pill"
1818
>

src/components/dashboard/chat/Toolbar.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<v-list-item
1616
v-else
1717
lines="two"
18-
:prepend-avatar="conversation.avatar"
18+
:prepend-avatar="conversation.avatar_url"
1919
density="comfortable"
2020
:title="conversation.name"
2121
subtitle="Driver"

src/components/dashboard/chat/Typing.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</audio>
77

88
<v-avatar>
9-
<v-img cover :src="partner.avatar"></v-img>
9+
<v-img cover :src="partner.avatar_url"></v-img>
1010
</v-avatar>
1111

1212
<div class="bg-grey-lighten-3 rounded-lg ms-4 py-2 px-3">

src/components/dashboard/chat/conversations/List.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
class="py-3"
2121
>
2222
<template #prepend>
23-
<v-avatar :image="conversation.avatar" />
23+
<v-avatar :image="conversation.avatar_url" />
2424
</template>
2525

2626
<h4 class="font-weight-medium d-flex align-center justify-space-between">

src/components/dashboard/chat/messages/Text.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
:class="{ 'justify-start': !current, 'flex-end flex-row-reverse': current }"
55
>
66
<v-avatar v-if="data.type === 'text'" size="40">
7-
<v-img cover :src="data.user.avatar"></v-img>
7+
<v-img cover :src="data.user.avatar_url"></v-img>
88
</v-avatar>
99

1010
<!-- The message body -->

src/components/dashboard/chat/messages/Voice.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<template #badge>
88
<v-icon size="23" color="grey-darken-2">mdi-microphone</v-icon>
99
</template>
10-
<v-avatar size="40" :image="data.user.avatar" />
10+
<v-avatar size="40" :image="data.user.avatar_url" />
1111
</v-badge>
1212

1313
<!-- The message body -->

src/components/dashboard/profile/Avatar.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
icon="mdi-camera"
99
style="cursor: pointer"
1010
>
11-
<v-avatar size="90" :image="url ? url : user.avatar" />
11+
<v-avatar size="90" :image="url ? url : user.avatar_url" />
1212
</v-badge>
1313

1414
<input @change="init" ref="uploader" type="file" accept="image/*" class="d-none" />
Lines changed: 36 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<template>
2-
<v-dialog width="700">
2+
<v-dialog v-model="isActive" @update:model-value="close" width="700">
33
<v-card rounded="lg">
44
<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>
66
</v-card-title>
77

88
<v-skeleton-loader
@@ -44,16 +44,16 @@
4444
:value="item[index].id"
4545
hide-details
4646
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 />
4949
</td>
5050
</tr>
5151
</tbody>
5252
</v-table>
5353
</v-card-text>
5454

5555
<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>
5757
<v-btn
5858
:disabled="form.busy || !meta.valid"
5959
:loading="form.busy"
@@ -62,7 +62,7 @@
6262
class="ms-4"
6363
@click="saveOrUpdate"
6464
>
65-
{{ compose.action }}
65+
{{ action.saveButton }}
6666
</v-btn>
6767
</v-card-actions>
6868
</Form>
@@ -71,101 +71,59 @@
7171
</template>
7272

7373
<script setup>
74-
import { ref, computed, onMounted } from 'vue'
74+
import { ref, computed } from 'vue'
7575
import { Form } from 'vee-validate'
7676
import { useAppStore } from '@/stores/app'
77-
import { useForm } from '@/composables/useForm'
77+
import { useForm, useEdit } from '@/composables'
7878
import { createRoleValidation } from '@/validations/roles'
79+
import { groupBy } from '@/utils'
7980
import axios from '@/plugins/axios'
8081
81-
const emit = defineEmits(['created'])
82-
const props = defineProps({
83-
compose: Object,
84-
})
82+
const emit = defineEmits(['created', 'update:modelValue'])
8583
8684
const isLoading = ref(false)
87-
const updateId = ref(null)
8885
const permissions = ref([])
8986
const normalizePermissions = computed(() => groupBy(permissions.value, 'group_name'))
9087
const { notify } = useAppStore()
9188
const form = useForm({
9289
name: '',
9390
permissions: [],
9491
})
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',
11494
})
11595
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
12399
}
124100
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()
132102
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()
135110
}
136111
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-
// }
149112
const _reset = () => {
150-
props.compose.close()
151113
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()
157119
}
158120
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+
})
171129
</script>

src/components/dashboard/users/Compose.vue

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<template>
2-
<v-dialog width="700">
2+
<v-dialog v-model="isActive" @update:model-value="close" width="700">
33
<v-card rounded="lg">
44
<v-card-title class="px-6 py-3 border-b">
5-
<h4 class="font-weight-medium">{{ compose.title }}</h4>
5+
<h4 class="font-weight-medium">{{ action.title }}</h4>
66
</v-card-title>
77

88
<v-skeleton-loader
@@ -73,20 +73,21 @@
7373
]"
7474
required
7575
/>
76+
<pre>{{ editPayload }}</pre>
7677

7778
<app-uploader
7879
v-model="form.avatar"
7980
required
8081
label="Upload a profile picture"
8182
accept="image/*"
8283
extensions="jpg,svg,jpeg,png,bmp,gif,webp"
83-
:preview-url="compose.data?.avatar"
84-
:is-updating="compose.isUpdating"
84+
:preview-url="editPayload?.avatar_url"
85+
:is-updating="isEditing()"
8586
/>
8687
</v-card-text>
8788

8889
<v-card-actions class="border-t px-4 py-3 mt-3">
89-
<v-btn @click="compose.close()" variant="plain" color="red">Discard</v-btn>
90+
<v-btn @click="close" variant="plain" color="red">Discard</v-btn>
9091

9192
<v-btn
9293
type="submit"
@@ -96,7 +97,7 @@
9697
color="primary"
9798
class="ms-4"
9899
>
99-
{{ compose.action }}
100+
{{ action.saveButton }}
100101
</v-btn>
101102
</v-card-actions>
102103
</Form>
@@ -105,18 +106,16 @@
105106
</template>
106107

107108
<script setup>
108-
import { onMounted, ref } from 'vue'
109+
import { ref } from 'vue'
109110
import { Form } from 'vee-validate'
110111
import { useAppStore } from '@/stores/app'
111-
import { useForm } from '@/composables/useForm'
112+
import { useForm, useEdit } from '@/composables'
112113
import { composeUserValidation } from '@/validations/users'
113114
import axios from '@/plugins/axios'
114115
115-
const emit = defineEmits(['created'])
116-
const props = defineProps({ compose: Object })
116+
const emit = defineEmits(['created', 'update:modelValue'])
117117
118118
const isLoading = ref(false)
119-
const updateId = ref(null)
120119
const roles = ref([])
121120
const { notify } = useAppStore()
122121
const form = useForm({
@@ -126,52 +125,45 @@ const form = useForm({
126125
role: '',
127126
status: 1,
128127
team: '',
128+
avatar: null,
129129
})
130-
131-
props.compose.onUpdate(() => {
132-
const data = props.compose.data
133-
if (data) {
134-
form.fill({
135-
...data,
136-
role: data.role.id,
137-
status: data.status.value,
138-
})
139-
form._method = 'PUT'
140-
updateId.value = data.id
141-
} else {
142-
form.reset()
143-
}
130+
const { isActive, isEditing, onCreate, onEdit, editPayload, close, action } = useEdit(form, {
131+
title: 'User',
144132
})
145133
146-
// Lifeycle hooks
147-
148-
onMounted(async () => {
134+
const fetchRoles = async () => {
149135
roles.value = (await axios.get('/roles')).data.data
150136
form.role = roles.value.find((role) => role.name === 'admin').id
151-
})
152-
153-
// Functions
154-
155-
const saveOrUpdate = () => {
156-
props.compose.isUpdating ? _update() : _save()
157137
}
158138
159-
const _save = () => {
160-
form.post('/users').then(() => _reset())
161-
}
139+
fetchRoles()
162140
163-
const _update = () => {
164-
form.post(`/users/${updateId.value}`).then(() => _reset())
141+
const saveOrUpdate = async () => {
142+
if (isEditing()) {
143+
await form.post(`/users/${editPayload.value.id}`)
144+
} else {
145+
await form.post('/users')
146+
}
147+
_reset()
165148
}
166149
167150
const _reset = () => {
168-
props.compose.close()
169151
emit('created')
170152
notify(
171-
props.compose.isUpdating
153+
isEditing()
172154
? 'The user info has been successfully updated!'
173155
: 'A new user has been successfully created!'
174156
)
175-
form.reset()
157+
close()
176158
}
159+
160+
defineExpose({
161+
create: onCreate,
162+
edit: (data) =>
163+
onEdit({
164+
...data,
165+
role: data.role.id,
166+
status: data.status.value,
167+
}),
168+
})
177169
</script>

src/composables/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export { useForm } from './useForm'
2+
export { useUser } from './useUser'
3+
export { useEdit } from './useEdit'
4+
export { useIsDark } from './useIsDark'
5+
export { useLoader } from './useLoader'

0 commit comments

Comments
 (0)