load การแจ้งเตือน,กล่องข้อความ

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2024-01-19 10:20:07 +07:00
parent ad929f93e2
commit a4cfdeed38
5 changed files with 132 additions and 131 deletions

View file

@ -1,5 +1,5 @@
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from "vue";
import { ref, onMounted, onUnmounted, watch } from "vue";
import keycloak from "@/plugins/keycloak";
import { useRoute, useRouter } from "vue-router";
import { useDataStore } from "@/stores/data";
@ -76,46 +76,48 @@ const options = ref<optionType[]>([
},
]);
const getDataNotification = async (index: number, type: string) => {
// showLoader();
async function fetchmsgNoread() {
await http
.get(config.API.msgNotificate + `?page=${index}&pageSize=${20}`)
.get(config.API.msgNoread())
.then((res) => {
totalNoti.value = res.data.result;
})
.catch((err) => {
messageError($q, err);
});
}
const getDataNotification = async (index: number, type: string) => {
const thaiOptions: Intl.DateTimeFormatOptions = {
hour: "2-digit",
minute: "2-digit",
};
await http
.get(config.API.msgNotificate + `?page=${index}&pageSize=${10}`)
.then((res: any) => {
const response = res.data.result.data;
let list: notiType[] = [];
totalInbox.value = res.data.result.total;
let list: notiType[] = [];
if (type === "DEL") {
totalInbox.value = res.data.result.total;
if (notiList.value.length === 16) {
notiList.value = [];
response.map((e: any) => {
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.push(...list);
}
} else {
response.map((e: any) => {
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.push(...list);
totalInbox.value = res.data.result.total;
notiList.value = [];
}
response.map((e: any) => {
list.push({
id: e.id,
sender:
e.createdFullName == "" || e.createdFullName == null
? "เจ้าหน้าที่"[0]
: e.createdFullName[0],
body: e.body ?? "",
timereceive: `${date2Thai(e.receiveDate)} ${new Date(
e.receiveDate
).toLocaleTimeString("th-TH", thaiOptions)} .`,
isOpen: e.isOpen,
});
});
notiList.value.push(...list);
totalInbox.value = res.data.result.total;
})
.catch((e) => {
messageError($q, e);
@ -231,8 +233,8 @@ onMounted(async () => {
if (keycloak.tokenParsed) {
await fetchroleUser(keycloak.tokenParsed.role);
}
await getDataNotification(1, "NOMAL");
await fetchmsgNoread();
// await getDataNotification(1, "NOMAL");
myEventHandler(null, false);
window.addEventListener("resize", (e: any) => {
myEventHandler(e, true);
@ -431,26 +433,47 @@ const clickDelete = async (id: string, index: number) => {
.then(() => {
notiList.value.splice(index, 1);
success($q, "ลบข้อมูลสำเร็จ");
totalInbox.value--;
})
.catch((e) => {
messageError($q, e);
})
.finally(async () => {
getDataNotification(1, "DEL");
notiList.value.length === 7 && getDataNotification(1, "DEL");
hideLoader();
});
});
};
const totalInbox = ref<number>(0);
const totalNoti = ref<number>(0);
const round = ref<number>(0);
function onLoad(index: any, done: any) {
if (notiList.value.length < totalInbox.value) {
setTimeout(() => {
getDataNotification(index + 1, "NOMAL");
done();
}, 2000);
}, 1000);
}
}
watch(
() => notiTrigger.value,
() => {
if (!notiTrigger.value) {
const updatedNotifications = notiList.value.map((item) => ({
...item,
isOpen: true,
}));
notiList.value = updatedNotifications;
fetchmsgNoread();
} else {
round.value++;
round.value === 1 && getDataNotification(round.value, "NOMAL");
}
}
);
</script>
<!-- โครงเว -->
@ -497,32 +520,33 @@ function onLoad(index: any, done: any) {
flat
size="13px"
class="q-mx-md bg-grey-3"
:color="notiList.length === 0 ? 'grey-6' : 'grey-8'"
:disable="notiList.length === 0"
:color="totalNoti === 0 ? 'grey-6' : 'grey-8'"
:disable="totalNoti === 0"
>
<q-icon name="mdi-bell" size="18px" color="grey-7" />
<q-badge
rounded
v-show="notiList.length > 0"
color="negative"
text-color="white"
floating
>{{ totalInbox }}</q-badge
>
<q-badge rounded color="negative" text-color="white" floating>{{
totalNoti
}}</q-badge>
<q-menu v-model="notiTrigger" max-width="480px" :offset="[0, 10]">
<div class="q-px-md q-py-sm row col-12 items-center">
<div
class="q-px-md q-py-sm row col-12 items-center"
v-if="notiList.length !== 0"
>
<div class="text-subtitle1 text-weight-medium">การแจงเตอน</div>
<q-space />
<div class="text-grey-5">งหมด {{ totalInbox }} อความ</div>
</div>
<q-infinite-scroll
@load="onLoad"
:offset="250"
style="min-width: 300px"
v-if="notiList.length !== 0"
style="max-height: 400px"
>
<q-list
style="min-width: 300px"
v-for="(n, index) in notiList"
:key="index"
:class="!n.isOpen ? 'my-menu-link q-mt-sm' : 'q-mt-sm'"
>
<q-item v-ripple class="mytry q-py-xs" dense>
<q-item-section avatar top style="min-width: 40px">
@ -539,7 +563,7 @@ function onLoad(index: any, done: any) {
<q-item-label
caption
class="row items-center text-grey-7"
>{{ date2Thai(n.timereceive) }}</q-item-label
>{{ n.timereceive }}</q-item-label
>
</q-item-section>
<q-btn
@ -1051,6 +1075,12 @@ function onLoad(index: any, done: any) {
</template>
<style>
.my-menu-link {
background: #ebf9f7 !important;
border-radius: 5px;
border: 1px solid #1bb19ab8;
color: #1bb19ab8 !important;
}
.menuSub .q-item__section--avatar,
.menu .q-item__section--avatar {
min-width: 0px;