|
| 1 | +<template lang="pug"> |
| 2 | +q-page.container |
| 3 | + .q-px-md |
| 4 | + sesame-searchfilters(:fields="fieldsList") |
| 5 | + template(#rightSelect) |
| 6 | + div |
| 7 | + |
| 8 | + sesame-2pan( |
| 9 | + :simple="true" |
| 10 | + :data="agents?.data" |
| 11 | + :total="agents?.total" |
| 12 | + :columns="columns" |
| 13 | + :visibleColumns="visibleColumns" |
| 14 | + :fieldsList="fieldsList" |
| 15 | + :selected="selected" |
| 16 | + :pagination="pagination" |
| 17 | + :pending="pending" |
| 18 | + :refresh="refreshEvent" |
| 19 | + :error="error" |
| 20 | + :titleKey=["username"] |
| 21 | + :crud="crud" |
| 22 | + :actions="actions" |
| 23 | + :defaultRightPanelButton="true" |
| 24 | + ) |
| 25 | + //- template(#top-left) |
| 26 | + //- sesame-table-top-left(:selected="selected" @updateLifestep="updateLifestep($event)" @clear="selected = []") |
| 27 | + //- template(#body-cell-states="props") |
| 28 | + //- sesame-table-state-col(:identity="props.row") |
| 29 | + //- template(#right-panel-actions-content-after="{target}") |
| 30 | + //- sesame-identity-form-actions(:identity="target" @submit="submit($event)" @sync="sync" @logs="logs") |
| 31 | + template(#right-panel-content="{ payload }") |
| 32 | + //- sesame-generic-form( |
| 33 | + //- :payload="payload" ref="form" |
| 34 | + //- :schema="schema" |
| 35 | + //- :uischema="uischema" |
| 36 | + //- v-model:validations="validations" |
| 37 | + //- ) |
| 38 | + sesame-json-form-renderer( |
| 39 | + v-model:data="payload.target" |
| 40 | + v-model:validations="validations" |
| 41 | + :schema="schema" |
| 42 | + :uischema="uischema" |
| 43 | + ) |
| 44 | +</template> |
| 45 | + |
| 46 | +<script lang="ts" setup> |
| 47 | +import usePagination from '~/composables/usePagination' |
| 48 | +import useAgentsSchema from '~/composables/useAgentsSchema' |
| 49 | +import { ref, provide, watch, computed } from 'vue' |
| 50 | +import { useFetch, useRoute, useRouter } from 'nuxt/app' |
| 51 | +import { useQuasar } from 'quasar' |
| 52 | +import type { QTableProps } from 'quasar' |
| 53 | +import type { components, operations } from '#build/types/service-api' |
| 54 | +import { useErrorHandling } from '#imports' |
| 55 | +type Agent = components['schemas']['AgentsDto'] |
| 56 | +type Response = operations['AgentsController_search']['responses']['200']['content']['application/json'] |
| 57 | +
|
| 58 | +defineOptions({ |
| 59 | + name: 'Agents', |
| 60 | +}) |
| 61 | +
|
| 62 | +const agentsSchema = useAgentsSchema() |
| 63 | +const schema = agentsSchema.schema |
| 64 | +const uischema = agentsSchema.uischema |
| 65 | +const validations = ref([]) |
| 66 | +
|
| 67 | +const daysjs = useDayjs() |
| 68 | +const route = useRoute() |
| 69 | +const router = useRouter() |
| 70 | +const $q = useQuasar() |
| 71 | +const { handleError } = useErrorHandling() |
| 72 | +const form = ref<any>(null) |
| 73 | +
|
| 74 | +onMounted(() => { |
| 75 | + initializePagination(agents.value?.total) |
| 76 | +}) |
| 77 | +
|
| 78 | +const { pagination, onRequest, initializePagination } = usePagination() |
| 79 | +
|
| 80 | +const queryWithoutRead = computed(() => { |
| 81 | + const { read, ...rest } = route.query |
| 82 | + return rest |
| 83 | +}) |
| 84 | +
|
| 85 | +const { |
| 86 | + data: agents, |
| 87 | + pending, |
| 88 | + refresh, |
| 89 | + error, |
| 90 | +} = await useHttp<Response>('/core/agents', { |
| 91 | + method: 'get', |
| 92 | + query: queryWithoutRead |
| 93 | +}) |
| 94 | +
|
| 95 | +if (error.value) { |
| 96 | + $q.notify({ |
| 97 | + message: 'Impossible de récupérer les agents', |
| 98 | + type: 'negative', |
| 99 | + }) |
| 100 | +} |
| 101 | +
|
| 102 | +const columns = ref<QTableProps['columns']>([ |
| 103 | + { |
| 104 | + name: 'username', |
| 105 | + label: 'Nom d\'utilisateur', |
| 106 | + field: (row: Agent) => row.username, |
| 107 | + align: 'left', |
| 108 | + sortable: true, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: 'email', |
| 112 | + label: 'Email', |
| 113 | + field: (row: Agent) => row.email, |
| 114 | + align: 'left', |
| 115 | + sortable: true, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: 'actions', |
| 119 | + label: 'Actions', |
| 120 | + field: 'actions', |
| 121 | + align: 'left', |
| 122 | + }, |
| 123 | +]) |
| 124 | +const visibleColumns = ref<QTableProps['visibleColumns']>([ |
| 125 | + 'username', |
| 126 | + 'email', |
| 127 | + 'actions', |
| 128 | +]) |
| 129 | +const columnsType = ref([ |
| 130 | + { name: 'username', type: 'text' }, |
| 131 | + { name: 'email', type: 'text' }, |
| 132 | + { name: 'actions', type: 'text' }, |
| 133 | + { name: 'actions', type: 'text' }, |
| 134 | + { name: 'actions', type: 'text' }, |
| 135 | +]) |
| 136 | +
|
| 137 | +const selected = ref([]) |
| 138 | +
|
| 139 | +function refreshEvent() { |
| 140 | + refresh() |
| 141 | + selected.value = [] |
| 142 | +} |
| 143 | +
|
| 144 | +const crud = { |
| 145 | + create: true, |
| 146 | + read: true, |
| 147 | + update: true, |
| 148 | + delete: true, |
| 149 | +} |
| 150 | +
|
| 151 | +async function submit(agent: Agent) { |
| 152 | + console.log('submit from index') |
| 153 | + form.value.submit() |
| 154 | +} |
| 155 | +
|
| 156 | +async function sync(agent: Agent) { |
| 157 | + console.log('sync') |
| 158 | + form.value.sync() |
| 159 | +} |
| 160 | +
|
| 161 | +function logs(agent: Agent) { |
| 162 | + console.log('logs') |
| 163 | +} |
| 164 | +
|
| 165 | +const actions = { |
| 166 | + cancel: async (row: Agent) => { |
| 167 | + console.log('cancel') |
| 168 | + validations.value = {} |
| 169 | + }, |
| 170 | + create: async (row: Agent) => { |
| 171 | +
|
| 172 | + // console.log('row', row) |
| 173 | +
|
| 174 | + const sanitizedAgent = { ...row } |
| 175 | + delete sanitizedAgent.metadata |
| 176 | +
|
| 177 | + const { data: result, pending, error, refresh } = await useHttp(`/core/agents`, { |
| 178 | + method: 'POST', |
| 179 | + body: { ...sanitizedAgent }, |
| 180 | + }); |
| 181 | + if (error.value) { |
| 182 | + handleError({ |
| 183 | + error: error.value, |
| 184 | + message: 'Erreur lors de la création' |
| 185 | + }) |
| 186 | + validations.value = error.value.data.validations |
| 187 | + } else { |
| 188 | + $q.notify({ |
| 189 | + message: 'Création effectuée', |
| 190 | + color: 'positive', |
| 191 | + position: 'top-right', |
| 192 | + icon: 'mdi-check-circle-outline', |
| 193 | + }) |
| 194 | + return row |
| 195 | + } |
| 196 | + }, |
| 197 | + update: async (row: Agent) => { |
| 198 | + const sanitizedAgent = { ...row } |
| 199 | + delete sanitizedAgent.metadata |
| 200 | +
|
| 201 | + const { data: result, pending, error, refresh } = await useHttp(`/core/agents/${row._id}`, { |
| 202 | + method: 'PATCH', |
| 203 | + body: sanitizedAgent, |
| 204 | + }); |
| 205 | + if (error.value) { |
| 206 | + handleError({ |
| 207 | + error: error.value, |
| 208 | + message: 'Erreur lors de la sauvegarde' |
| 209 | + }) |
| 210 | + validations.value = error.value.data.validations |
| 211 | + } else { |
| 212 | + $q.notify({ |
| 213 | + message: 'Sauvegarde effectuée', |
| 214 | + color: 'positive', |
| 215 | + position: 'top-right', |
| 216 | + icon: 'mdi-check-circle-outline', |
| 217 | + }) |
| 218 | + } |
| 219 | + return row |
| 220 | + }, |
| 221 | + delete: async (row: Agent) => { |
| 222 | + $q.dialog({ |
| 223 | + dark: true, |
| 224 | + title: 'Suppresion d\'un l\'agent', |
| 225 | + message: `Vous êtes sur le point de supprimer l\'agent <b>${row.username}</b>. Êtes-vous sûr ?`, |
| 226 | + persistent: true, |
| 227 | + html: true, |
| 228 | + ok: { |
| 229 | + push: true, |
| 230 | + color: 'negative', |
| 231 | + label: 'Supprimer', |
| 232 | + }, |
| 233 | + cancel: { |
| 234 | + push: true, |
| 235 | + color: 'grey-8', |
| 236 | + label: 'Annuler', |
| 237 | + }, |
| 238 | + }).onOk(async () => { |
| 239 | + const { data: result, pending, error, refresh } = await useHttp(`/core/agents/${row._id}`, { |
| 240 | + method: 'DELETE', |
| 241 | + }); |
| 242 | + if (error.value) { |
| 243 | + handleError({ |
| 244 | + error: error.value, |
| 245 | + message: 'Erreur lors de la suppression', |
| 246 | + }) |
| 247 | + validations.value = error.value.data.validations |
| 248 | + } else { |
| 249 | + $q.notify({ |
| 250 | + message: 'Suppression effectuée', |
| 251 | + color: 'positive', |
| 252 | + position: 'top-right', |
| 253 | + icon: 'mdi-check-circle-outline', |
| 254 | + }) |
| 255 | + } |
| 256 | +
|
| 257 | + }) |
| 258 | + return row |
| 259 | + }, |
| 260 | + read: async (row, onMounted = false) => { |
| 261 | + if (!onMounted) pushQuery({ key: 'read', value: row._id }) |
| 262 | + const { data } = await useHttp<Agent>(`/core/agents/${row._id}`, { |
| 263 | + method: 'get', |
| 264 | + }) |
| 265 | + validations.value = {} |
| 266 | + return data.value?.data |
| 267 | + }, |
| 268 | + onMounted: async () => { |
| 269 | + if (route.query.read) { |
| 270 | + const id = route.query.read as string |
| 271 | +
|
| 272 | + const { data } = await useHttp<Agent>(`/core/agents/${id}`, { |
| 273 | + method: 'get', |
| 274 | + }) |
| 275 | + return data.value?.data |
| 276 | + // const row = agents.value?.data.find((row) => row._id === id) |
| 277 | + // if (row) { |
| 278 | + // return row |
| 279 | + // } |
| 280 | + } |
| 281 | + return null |
| 282 | + } |
| 283 | +
|
| 284 | +} |
| 285 | +
|
| 286 | +const fieldsList = computed(() => { |
| 287 | + return columns.value!.reduce((acc: { name: string; label: string; type?: string }[], column) => { |
| 288 | + if (visibleColumns.value!.includes(column.name) && column.name !== 'actions' && column.name !== 'states') { |
| 289 | + const type = columnsType.value.find((type) => type.name === column.name)?.type |
| 290 | + acc.push({ |
| 291 | + name: column.name, |
| 292 | + label: column.label, |
| 293 | + type, |
| 294 | + }) |
| 295 | + } |
| 296 | + return acc |
| 297 | + }, []) |
| 298 | +}) |
| 299 | +
|
| 300 | +provide('fieldsList', fieldsList.value) |
| 301 | +</script> |
0 commit comments