2023-05-11 06:03:22 +02:00
|
|
|
<script setup lang="ts">
|
|
|
|
/* global $fetch */
|
2023-05-24 09:40:45 +02:00
|
|
|
import { useFetch, createError, navigateTo, NuxtError, useRoute } from "nuxt/app";
|
|
|
|
import { ref, Ref, reactive } from "vue";
|
|
|
|
import { VBtn } from "vuetify/components";
|
2023-05-11 06:03:22 +02:00
|
|
|
import { definePageMeta } from "~/.nuxt/imports";
|
2023-05-24 09:40:45 +02:00
|
|
|
|
2023-05-11 06:03:22 +02:00
|
|
|
import { client as clientType } from "~/utils/types/database";
|
|
|
|
import pagedTable from "~/components/pagedTable.vue";
|
2023-05-24 09:40:45 +02:00
|
|
|
import Alerts, { AlertData } from "~/components/alerts.vue";
|
|
|
|
import { fieldDefinition } from "~/components/entryEditor.vue";
|
2023-05-11 06:03:22 +02:00
|
|
|
|
|
|
|
definePageMeta({ middleware: ["auth"] });
|
2023-05-24 09:40:45 +02:00
|
|
|
const route = useRoute();
|
|
|
|
|
|
|
|
const alerts = ref<Array<AlertData>>([]);
|
2023-05-11 06:03:22 +02:00
|
|
|
|
|
|
|
const clientsRequest = await useFetch("/api/clients");
|
|
|
|
if (clientsRequest.error.value) throw createError(clientsRequest.error.value?.data ?? "");
|
|
|
|
const clients = clientsRequest.data as Ref<NonNullable<typeof clientsRequest.data.value>>;
|
|
|
|
|
|
|
|
const countRequest = await useFetch("/api/clients/count");
|
|
|
|
if (countRequest.error.value) throw createError(countRequest.error.value?.data ?? "");
|
|
|
|
const count = countRequest.data as Ref<NonNullable<typeof countRequest.data.value>>;
|
|
|
|
|
2023-05-24 09:40:45 +02:00
|
|
|
async function rowClicked(client: string, edit = false) {
|
|
|
|
await navigateTo({
|
|
|
|
path: `/client/${client}`,
|
|
|
|
query: { edit: edit ? 1 : undefined },
|
|
|
|
});
|
2023-05-11 06:03:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function rowDelete(client: string) {
|
|
|
|
try {
|
|
|
|
await $fetch<clientType>(`/api/clients/${client}`, {
|
|
|
|
method: "DELETE",
|
|
|
|
});
|
|
|
|
clients.value = clients.value.filter(e => e.id !== client);
|
|
|
|
count.value.count--;
|
|
|
|
} catch (e) {
|
2023-05-24 09:40:45 +02:00
|
|
|
alerts.value.push({ text: (e as NuxtError).message, type: "error" });
|
2023-05-11 06:03:22 +02:00
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const loadingMore = ref<boolean>(false);
|
|
|
|
async function loadBefore() {
|
|
|
|
loadingMore.value = true;
|
|
|
|
|
2023-05-24 09:40:45 +02:00
|
|
|
try {
|
|
|
|
clients.value.push(...await $fetch("/api/clients", {
|
|
|
|
query: {
|
|
|
|
before: clients.value[clients.value.length - 1].id,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
} catch (e) {
|
|
|
|
alerts.value.push({ text: (e as NuxtError).message });
|
|
|
|
console.error(e);
|
|
|
|
}
|
2023-05-11 06:03:22 +02:00
|
|
|
loadingMore.value = false;
|
|
|
|
}
|
2023-05-24 09:40:45 +02:00
|
|
|
|
|
|
|
const createMode = ref<boolean>(route.query?.create === "1");
|
|
|
|
|
|
|
|
function editorFields(): Array<fieldDefinition> {
|
|
|
|
return [
|
|
|
|
{ key: "name", type: "text", label: "Name" },
|
|
|
|
{ key: "address", type: "text", label: "Address" },
|
|
|
|
{ key: "phone", type: "text", label: "Phone" },
|
|
|
|
{ key: "email", type: "text", label: "E-mail" },
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
const submitting = ref<boolean>(false);
|
|
|
|
|
|
|
|
// const updateForm = ref<VForm | null>(null);
|
|
|
|
const formButton = ref<VBtn | null>(null);
|
|
|
|
const formData = ref<any>({
|
|
|
|
name: null,
|
|
|
|
address: null,
|
|
|
|
phone: null,
|
|
|
|
email: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
function normalizeForm() {
|
|
|
|
for (const i in formData.value)
|
|
|
|
formData.value[i] = formData.value[i] === "" ? null : formData.value[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleSubmit() {
|
|
|
|
submitting.value = true;
|
|
|
|
normalizeForm();
|
|
|
|
try {
|
|
|
|
const result = await $fetch(
|
|
|
|
"/api/clients/", {
|
|
|
|
method: "POST",
|
|
|
|
body: formData.value,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
submitting.value = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
submitting.value = false;
|
|
|
|
createMode.value = false;
|
|
|
|
}
|
2023-05-11 06:03:22 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
2023-05-24 09:40:45 +02:00
|
|
|
<Alerts :alerts="alerts" />
|
|
|
|
<VDialog
|
|
|
|
v-model="createMode"
|
|
|
|
:persistent="submitting"
|
|
|
|
:activator="formButton as unknown as (Element | null) ?? undefined"
|
|
|
|
width="auto"
|
2023-05-11 06:03:22 +02:00
|
|
|
>
|
2023-05-24 09:40:45 +02:00
|
|
|
<VCard width="400px" :loading="submitting">
|
|
|
|
<VCardTitle>
|
|
|
|
Create client
|
|
|
|
</VCardTitle>
|
|
|
|
<VForm
|
|
|
|
ref="updateForm"
|
|
|
|
:disabled="submitting"
|
|
|
|
class="px-4"
|
|
|
|
>
|
|
|
|
<EntryEditor
|
|
|
|
:fields="editorFields()"
|
|
|
|
@update-sub-model-value="(k, v) => { formData[k] = v; }"
|
|
|
|
/>
|
|
|
|
</VForm>
|
|
|
|
<VCardActions>
|
|
|
|
<VBtn
|
|
|
|
color="primary"
|
|
|
|
@click="handleSubmit"
|
|
|
|
>
|
|
|
|
Submit
|
|
|
|
</VBtn>
|
|
|
|
</VCardActions>
|
|
|
|
</VCard>
|
|
|
|
</VDialog>
|
2023-05-11 06:03:22 +02:00
|
|
|
<VRow>
|
|
|
|
<VCol>
|
|
|
|
<VBreadcrumbs :items="['Clients']" />
|
|
|
|
<VSpacer />
|
|
|
|
<div class="text-h4">
|
|
|
|
There are {{ count?.count }} clients in the database.
|
|
|
|
</div>
|
2023-05-24 09:40:45 +02:00
|
|
|
<VBtn
|
|
|
|
ref="formButton"
|
|
|
|
>
|
|
|
|
Create
|
|
|
|
</VBtn>
|
2023-05-11 06:03:22 +02:00
|
|
|
</VCol>
|
|
|
|
</VRow>
|
|
|
|
<VRow>
|
|
|
|
<VCol cols="12">
|
|
|
|
<VCard>
|
|
|
|
<VTable>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Name</th>
|
|
|
|
<th>Address</th>
|
|
|
|
<th />
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<pagedTable
|
|
|
|
buttons
|
|
|
|
:records="clients"
|
|
|
|
:fields="['name', 'address']"
|
|
|
|
record-key="id"
|
|
|
|
@click="(id) => rowClicked(id, false)"
|
|
|
|
@click:edit="(id) => rowClicked(id, true)"
|
|
|
|
@click:delete="(id) => rowDelete(id)"
|
|
|
|
/>
|
|
|
|
</VTable>
|
|
|
|
</VCard>
|
|
|
|
<VCol>
|
|
|
|
<VBtn
|
|
|
|
v-if="clients.length < count.count"
|
|
|
|
color="primary"
|
|
|
|
:loading="loadingMore"
|
|
|
|
@click="loadBefore"
|
|
|
|
>
|
|
|
|
Load more
|
|
|
|
</VBtn>
|
|
|
|
</VCol>
|
|
|
|
</VCol>
|
|
|
|
</VRow>
|
|
|
|
</template>
|