import { requestCallback, request } from '../lib/request' import { renderDateTime } from '../lib/renderers' const endpoints = { list: '/api/user', item: '/api/user', } const getList = async (query, callback) => { return requestCallback(`${endpoints.list}?${new URLSearchParams(query)}`, {}, callback ?? (() => true)) } const getOne = async (query, callback) => { return request(`${endpoints.list}?${new URLSearchParams(query)}`, {}) .then((data) => data.rows[0] ?? null) .then(callback ?? (() => true)) } const write = async (endpoint, data, callback, callbackError) => { return requestCallback( endpoint, { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify(data), }, callback ?? (() => true), ).catch(callbackError ?? (() => true)) } const create = async (data, callback, callbackError) => { return write(`${endpoints.item}`, data, callback, callbackError) } const update = async (data, callback, callbackError) => { return write(`${endpoints.item}/${data.id}`, data, callback, callbackError) } const remove = async (id, callback, callbackError) => { return requestCallback( `${endpoints.item}/${id}`, { method: 'DELETE', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, }, callback ?? (() => true), ).catch(callbackError ?? (() => true)) } const createForm = (item) => { const data = { ...item } return { data: data, error: null, fields: [ { label: 'Nom', type: 'text', required: true, key: 'display_name', }, { label: "Nom d'utilisateur", type: 'text', required: true, key: 'username', }, { label: 'Mot de passe', type: 'password', required: data.id > 0, key: 'password', }, ], } } const getListFields = () => { return [ { key: 'display_name', label: 'Nom', width: '30%', }, { key: 'username', label: 'Utilisateur', }, { key: 'logged_at', label: 'Dernière connexion', render: (item) => renderDateTime(item.logged_at), }, ] } export { endpoints, getList, getOne, create, update, remove, getListFields, createForm }