From 23314ca3a658732678b6cae33e2799e4bf0f463c Mon Sep 17 00:00:00 2001 From: "DESKTOP-1R2VSQH\\Lenovo ThinkPad E490" Date: Tue, 28 Nov 2023 13:36:08 +0700 Subject: [PATCH] =?UTF-8?q?API=20=E0=B8=81=E0=B8=B2=E0=B8=A3=E0=B9=81?= =?UTF-8?q?=E0=B8=88=E0=B9=89=E0=B8=87=E0=B9=80=E0=B8=95=E0=B8=B7=E0=B8=AD?= =?UTF-8?q?=E0=B8=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/api.message.ts | 7 ++++ src/app.config.ts | 13 ++++--- src/interface/index/Main.ts | 1 + src/interface/response/Main.ts | 12 ++++++ src/stores/mixin.ts | 30 +++++++++++++++ src/views/HomeView.vue | 69 ++++++++++++++++++++++++++++------ 6 files changed, 115 insertions(+), 17 deletions(-) create mode 100644 src/api/api.message.ts create mode 100644 src/interface/response/Main.ts diff --git a/src/api/api.message.ts b/src/api/api.message.ts new file mode 100644 index 0000000..21c6813 --- /dev/null +++ b/src/api/api.message.ts @@ -0,0 +1,7 @@ +import env from './index' +const message = `${env.API_URI}/message` + +export default { + msgNotificate: `${message}/my-notifications`, + msgId: (id: string) => `${message}/my-notifications/${id}`, +} diff --git a/src/app.config.ts b/src/app.config.ts index d0fbb30..43b0eee 100644 --- a/src/app.config.ts +++ b/src/app.config.ts @@ -1,20 +1,23 @@ /**ใช้รวมไฟล์ย่อยๆ ของ api แต่ละไฟล์ */ /** API ระบบลงเวลา */ -import leave from "@/api/api.checkin"; -import history from "@/api/api.history"; +import leave from '@/api/api.checkin' +import history from '@/api/api.history' +import message from '@/api/api.message' // environment variables -export const s3ClusterUrl = import.meta.env.VITE_S3CLUSTER_PUBLIC_URL; +export const s3ClusterUrl = import.meta.env.VITE_S3CLUSTER_PUBLIC_URL const API = { /**leave */ ...leave, /**history */ ...history, -}; + /**message */ + ...message, +} export default { API: API, s3ClusterUrl, -}; +} diff --git a/src/interface/index/Main.ts b/src/interface/index/Main.ts index f2a6863..af075cf 100644 --- a/src/interface/index/Main.ts +++ b/src/interface/index/Main.ts @@ -13,4 +13,5 @@ interface notiType { body: string timereceive: Date } + export type { DataOption, FormRef, notiType } diff --git a/src/interface/response/Main.ts b/src/interface/response/Main.ts new file mode 100644 index 0000000..24b76cd --- /dev/null +++ b/src/interface/response/Main.ts @@ -0,0 +1,12 @@ +interface Noti { + id: string + body: string + receiverUserId: string + type: string + payload: null + isOpen: false + receiveDate: Date | null + openDate: null +} + +export type { Noti } diff --git a/src/stores/mixin.ts b/src/stores/mixin.ts index 8824ad1..46c999c 100644 --- a/src/stores/mixin.ts +++ b/src/stores/mixin.ts @@ -238,6 +238,35 @@ export const useCounterMixin = defineStore('mixin', () => { } } + function dialogRemove( + q: any, + ok?: () => void, + title?: string, // ถ้ามี cancel action ใส่เป็น null + desc?: string, // ถ้ามี cancel action ใส่เป็น null + cancel?: () => void + ) { + q.dialog({ + component: CustomComponent, + componentProps: { + title: title && title != null ? title : 'ยืนยันการลบข้อมูล', + message: + desc && desc != null + ? desc + : 'ต้องการยืนยันการลบข้อมูลนี้ใช่หรือไม่?', + icon: 'delete', + color: 'red', + textOk: 'ตกลง', + onlycancel: false, + }, + }) + .onOk(() => { + if (ok) ok() + }) + .onCancel(() => { + if (cancel) cancel() + }) + } + return { date2Thai, showLoader, @@ -247,5 +276,6 @@ export const useCounterMixin = defineStore('mixin', () => { messageError, success, notify, + dialogRemove, } }) diff --git a/src/views/HomeView.vue b/src/views/HomeView.vue index f51a920..a3cae5e 100644 --- a/src/views/HomeView.vue +++ b/src/views/HomeView.vue @@ -9,6 +9,7 @@ import config from '@/app.config' // import Type import type { FormRef } from '@/interface/response/checkin' +import type { Noti } from '@/interface/response/Main' import type { notiType } from '@/interface/index/Main' // import components @@ -19,7 +20,7 @@ import MapCheck from '@/components/MapCheckin.vue' import { useCounterMixin } from '@/stores/mixin' const mixin = useCounterMixin() -const { date2Thai, showLoader, hideLoader, messageError } = mixin +const { date2Thai, showLoader, hideLoader, messageError, dialogRemove } = mixin const router = useRouter() const $q = useQuasar() @@ -37,13 +38,65 @@ async function fetchCheckTime() { checkInId.value = data.checkInId ? data.checkInId : '' }) .catch((err) => { - console.log(err) + messageError($q, err) }) .finally(() => { hideLoader() }) } +const notiTrigger = ref(false) +const notiList = ref([]) +/** function เรียกข้อมุลแจ้งเตือน */ +async function fetchNotifications() { + showLoader() + await http + .get(config.API.msgNotificate) + .then((res) => { + const response = res.data.result + const list: notiType[] = [] + response.map((e: Noti) => { + list.push({ + id: e.id, + sender: + e.createdFullName == '' || e.createdFullName == null + ? 'เจ้าหน้าที่'[0] + : e.createdFullName[0], + body: e.body ?? '', + timereceive: new Date(e.receiveDate), + }) + }) + notiList.value = list + }) + .catch((err) => { + messageError($q, err) + }) + .finally(() => { + hideLoader() + }) +} + +/** + * function ลบรายการแจ้งเตือน + * @param id noti + */ +async function onClickDelete(id: string) { + dialogRemove($q, async () => { + await http + .delete(config.API.msgId(id)) + .then(() => { + success($q, 'ลบข้อมูลสำเร็จ') + }) + .catch((e) => { + messageError($q, e) + }) + .finally(async () => { + await fetchNotifications() + hideLoader() + }) + }) +} + /** ref อัพเดทเวลา*/ const dateNow = ref(new Date()) const Thai = ref(dateNow.value) @@ -225,16 +278,6 @@ async function onClickConfirm() { dialogTime.value = false } -const notiTrigger = ref(false) -const notiList = ref([ - { - id: '1', - sender: 'ท', - body: 'ลงเวลา', - timereceive: new Date(), - }, -]) - // class const getClass = (val: boolean) => { return { @@ -245,6 +288,7 @@ const getClass = (val: boolean) => { /** Hook*/ onMounted(async () => { await fetchCheckTime() + await fetchNotifications() updateClock() }) @@ -327,6 +371,7 @@ onMounted(async () => { dense icon="mdi-close" class="mybtn q-mx-xs" + @click="onClickDelete(n.id)" >