import type { User } from '~/types' export const useUsers = () => { const users = useState('users.list', () => []) const fetchUsers = async (): Promise => { 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 => { 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 => { 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 => { await useApiFetch(`/api/users/${id}`, { method: 'DELETE' }) users.value = users.value.filter((u) => u.id !== id) } return { users, fetchUsers, createUser, updateUser, deleteUser } }