mandant-crm/frontend/composables/useUsers.ts
Tim Stollberg 570f17e8b8 feat(frontend): commit uncommitted settings, custom fields, and import work
This local working tree had accumulated a substantial amount of
finished-but-never-committed frontend work: custom client fields
(replacing the old static ClientFields with a dynamic
ClientCustomFieldCard + useCustomFields), CSV import
(useImport + settings/import page), history type management
(useHistoryTypes + settings/history-types page), an admin-only route
middleware, several new icon components, a shared useApiFetch
composable, and a client detail page restructure ([id].vue ->
[id]/index.vue). None of it had ever been pushed, so production was
running a stale build that was already linking to routes/components
that didn't exist server-side (missing IconSettings, 404s on
/settings/import, etc). This commit brings the repo in line with
local state.
2026-07-18 16:27:19 +07:00

51 lines
1.3 KiB
TypeScript

import type { User } from '~/types'
export const useUsers = () => {
const users = useState<User[]>('users.list', () => [])
const fetchUsers = async (): Promise<void> => {
const { data } = await useApiFetch<{ data: User[] }>('/api/users')
users.value = data
}
const createUser = async (payload: {
name: string
email: string
password: string
password_confirmation: string
role: 'admin' | 'staff'
}): Promise<User> => {
const { data } = await useApiFetch<{ data: User }>('/api/users', {
method: 'POST',
body: payload,
})
users.value.push(data)
return data
}
const updateUser = async (
id: number,
payload: {
name: string
email: string
password?: string
password_confirmation?: string
role: 'admin' | 'staff'
}
): Promise<User> => {
const { data } = await useApiFetch<{ data: User }>(`/api/users/${id}`, {
method: 'PUT',
body: payload,
})
const index = users.value.findIndex((u) => u.id === id)
if (index >= 0) users.value[index] = data
return data
}
const deleteUser = async (id: number): Promise<void> => {
await useApiFetch(`/api/users/${id}`, { method: 'DELETE' })
users.value = users.value.filter((u) => u.id !== id)
}
return { users, fetchUsers, createUser, updateUser, deleteUser }
}