felicity-lims/webapp/stores/notice.ts

78 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-04-10 09:29:10 +08:00
import { defineStore } from 'pinia';
import { GET_NOTICES_BY_CREATOR } from '../graphql/notice.queries';
import { INotice } from '../models/notice';
import { subtractDates } from '../utils';
2022-04-04 02:54:31 +08:00
2023-04-10 09:29:10 +08:00
import { useApiUtil } from '../composables';
2022-04-04 02:54:31 +08:00
const { withClientQuery } = useApiUtil();
2023-04-10 09:29:10 +08:00
export const useNoticeStore = defineStore('notice', {
state: () =>
({
notices: [],
fetchingNotices: false,
filterBy: 'all',
filters: ['all', 'active', 'expired'],
} as {
notices: INotice[];
fetchingNotices: boolean;
filterBy: string;
filters: string[];
}),
2022-04-04 02:54:31 +08:00
getters: {
2023-04-10 09:29:10 +08:00
getNotices: state => state.notices,
getActiveNotices: state => state.notices?.filter(n => !n.expired),
getMyNotices: state => uid => state.notices?.filter(n => n.createdByUid === uid),
getFilterBy: state => state.filterBy,
getFilters: state => state.filters,
2022-04-04 02:54:31 +08:00
},
actions: {
2023-04-10 09:29:10 +08:00
async fetchMyNotices(uid: string) {
2022-04-15 16:32:24 +08:00
this.fetchingNotices = true;
2023-04-10 09:29:10 +08:00
await withClientQuery(GET_NOTICES_BY_CREATOR, { uid }, 'noticesByCreator')
.then(payload => {
this.fetchingNotices = false;
this.notices = payload?.map(n => modifyExpiry(n));
})
.catch(err => (this.fetchingNotices = false));
2022-04-04 02:54:31 +08:00
},
2023-04-10 09:29:10 +08:00
addNotice(notice: INotice) {
2022-04-08 16:30:42 +08:00
this.notices?.unshift(modifyExpiry(notice));
2022-04-04 02:54:31 +08:00
},
2023-04-10 09:29:10 +08:00
updateNotice(notice: INotice) {
2022-04-08 16:30:42 +08:00
const index = this.notices?.findIndex(x => x.uid === notice.uid);
2023-04-10 09:29:10 +08:00
if (index > -1) this.notices[index] = modifyExpiry(notice);
2022-04-04 02:54:31 +08:00
},
2023-04-10 09:29:10 +08:00
deleteNotice(notice: INotice) {
2022-04-08 16:30:42 +08:00
const index = this.notices?.findIndex(x => x.uid === notice.uid);
2023-04-10 09:29:10 +08:00
if (index > -1) this.notices?.splice(index, 1);
},
},
});
2022-04-04 02:54:31 +08:00
2023-04-10 09:29:10 +08:00
const hasExpired = (notice: INotice) => new Date() > new Date(notice.expiry);
2022-04-04 02:54:31 +08:00
2023-04-10 09:29:10 +08:00
const daysToExpiry = (notice: INotice) => subtractDates(new Date(), new Date(notice.expiry));
2022-04-04 02:54:31 +08:00
const modifyExpiry = (notice: INotice): INotice => {
const expired = hasExpired(notice);
const days = +daysToExpiry(notice);
2023-04-10 09:29:10 +08:00
notice.expired = expired;
notice.dayToExpiration = days;
if (expired === true) {
if (days === 0) {
notice.status = 'expired today';
2022-04-04 02:54:31 +08:00
} else {
2023-04-10 09:29:10 +08:00
notice.status = 'expired ' + days + ' days ago';
2022-04-04 02:54:31 +08:00
}
} else {
2023-04-10 09:29:10 +08:00
if (days === 0) {
notice.status = 'expiring today';
2022-04-04 02:54:31 +08:00
} else {
2023-04-10 09:29:10 +08:00
notice.status = 'expiring in ' + days + ' days';
2022-04-04 02:54:31 +08:00
}
}
return notice;
2023-04-10 09:29:10 +08:00
};