Refactor Reservations into new store
All checks were successful
Build BAB Application Deployment Artifact / build (push) Successful in 2m2s
All checks were successful
Build BAB Application Deployment Artifact / build (push) Successful in 2m2s
This commit is contained in:
84
src/stores/reservation.ts
Normal file
84
src/stores/reservation.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import type { Reservation } from './schedule.types';
|
||||
import { ref } from 'vue';
|
||||
import { AppwriteIds, databases } from 'src/boot/appwrite';
|
||||
import { Timestamp, parsed } from '@quasar/quasar-ui-qcalendar';
|
||||
|
||||
export const useReservationStore = defineStore('reservation', () => {
|
||||
const reservations = ref<Reservation[]>([]);
|
||||
|
||||
const getConflictingReservations = (
|
||||
resource: string,
|
||||
start: Date,
|
||||
end: Date
|
||||
): Reservation[] => {
|
||||
const overlapped = reservations.value.filter(
|
||||
(entry: Reservation) =>
|
||||
entry.resource == resource &&
|
||||
new Date(entry.start) < end &&
|
||||
new Date(entry.end) > start
|
||||
);
|
||||
return overlapped;
|
||||
};
|
||||
|
||||
const isResourceTimeOverlapped = (
|
||||
resource: string,
|
||||
start: Date,
|
||||
end: Date
|
||||
): boolean => {
|
||||
return getConflictingReservations(resource, start, end).length > 0;
|
||||
};
|
||||
|
||||
const isReservationOverlapped = (res: Reservation): boolean => {
|
||||
return isResourceTimeOverlapped(
|
||||
res.resource,
|
||||
new Date(res.start),
|
||||
new Date(res.end)
|
||||
);
|
||||
};
|
||||
|
||||
const addOrCreateReservation = (reservation: Reservation) => {
|
||||
const index = reservations.value.findIndex(
|
||||
(res) => res.id == reservation.id
|
||||
);
|
||||
index != -1
|
||||
? (reservations.value[index] = reservation)
|
||||
: reservations.value.push(reservation);
|
||||
};
|
||||
async function fetchReservations() {
|
||||
try {
|
||||
const response = await databases.listDocuments(
|
||||
AppwriteIds.databaseId,
|
||||
AppwriteIds.collection.reservation
|
||||
);
|
||||
reservations.value = response.documents as Reservation[];
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch timeblocks', error);
|
||||
}
|
||||
}
|
||||
|
||||
const getBoatReservations = (
|
||||
searchDate: Timestamp,
|
||||
boat?: string
|
||||
): Reservation[] => {
|
||||
const result = reservations.value.filter((x) => {
|
||||
return (
|
||||
((parsed(x.start)?.date == searchDate.date ||
|
||||
parsed(x.end)?.date == searchDate.date) && // Part of reservation falls on day
|
||||
x.resource != undefined && // A boat is defined
|
||||
!boat) ||
|
||||
x.resource == boat // A specific boat has been passed, and matches
|
||||
);
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
||||
return {
|
||||
getBoatReservations,
|
||||
fetchReservations,
|
||||
addOrCreateReservation,
|
||||
isReservationOverlapped,
|
||||
isResourceTimeOverlapped,
|
||||
getConflictingReservations,
|
||||
};
|
||||
});
|
||||
@@ -8,12 +8,7 @@ import {
|
||||
compareDate,
|
||||
} from '@quasar/quasar-ui-qcalendar';
|
||||
|
||||
import {
|
||||
Reservation,
|
||||
IntervalTemplate,
|
||||
TimeTuple,
|
||||
Interval,
|
||||
} from './schedule.types';
|
||||
import { IntervalTemplate, TimeTuple, Interval } from './schedule.types';
|
||||
import { AppwriteIds, databases } from 'src/boot/appwrite';
|
||||
import { ID, Models } from 'appwrite';
|
||||
import { buildISODate } from 'src/utils/misc';
|
||||
@@ -83,7 +78,6 @@ export function buildInterval(
|
||||
|
||||
export const useScheduleStore = defineStore('schedule', () => {
|
||||
// TODO: Implement functions to dynamically pull this data.
|
||||
const reservations = ref<Reservation[]>([]);
|
||||
const timeblocks = ref<Interval[]>([]);
|
||||
const timeblockTemplates = ref<IntervalTemplate[]>([]);
|
||||
|
||||
@@ -105,34 +99,6 @@ export const useScheduleStore = defineStore('schedule', () => {
|
||||
});
|
||||
};
|
||||
|
||||
async function fetchReservations() {
|
||||
try {
|
||||
const response = await databases.listDocuments(
|
||||
AppwriteIds.databaseId,
|
||||
AppwriteIds.collection.reservation
|
||||
);
|
||||
reservations.value = response.documents as Reservation[];
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch timeblocks', error);
|
||||
}
|
||||
}
|
||||
|
||||
const getBoatReservations = (
|
||||
searchDate: Timestamp,
|
||||
boat?: string
|
||||
): Reservation[] => {
|
||||
const result = reservations.value.filter((x) => {
|
||||
return (
|
||||
((parsed(x.start)?.date == searchDate.date ||
|
||||
parsed(x.end)?.date == searchDate.date) && // Part of reservation falls on day
|
||||
x.resource != undefined && // A boat is defined
|
||||
!boat) ||
|
||||
x.resource == boat // A specific boat has been passed, and matches
|
||||
);
|
||||
});
|
||||
return result;
|
||||
};
|
||||
|
||||
async function fetchIntervals() {
|
||||
try {
|
||||
const response = await databases.listDocuments(
|
||||
@@ -179,45 +145,6 @@ export const useScheduleStore = defineStore('schedule', () => {
|
||||
// return scheduleStore.getConflictingReservations(boat, start, end);
|
||||
// };
|
||||
|
||||
const getConflictingReservations = (
|
||||
resource: string,
|
||||
start: Date,
|
||||
end: Date
|
||||
): Reservation[] => {
|
||||
const overlapped = reservations.value.filter(
|
||||
(entry: Reservation) =>
|
||||
entry.resource == resource &&
|
||||
new Date(entry.start) < end &&
|
||||
new Date(entry.end) > start
|
||||
);
|
||||
return overlapped;
|
||||
};
|
||||
|
||||
const isResourceTimeOverlapped = (
|
||||
resource: string,
|
||||
start: Date,
|
||||
end: Date
|
||||
): boolean => {
|
||||
return getConflictingReservations(resource, start, end).length > 0;
|
||||
};
|
||||
|
||||
const isReservationOverlapped = (res: Reservation): boolean => {
|
||||
return isResourceTimeOverlapped(
|
||||
res.resource,
|
||||
new Date(res.start),
|
||||
new Date(res.end)
|
||||
);
|
||||
};
|
||||
|
||||
const addOrCreateReservation = (reservation: Reservation) => {
|
||||
const index = reservations.value.findIndex(
|
||||
(res) => res.id == reservation.id
|
||||
);
|
||||
index != -1
|
||||
? (reservations.value[index] = reservation)
|
||||
: reservations.value.push(reservation);
|
||||
};
|
||||
|
||||
const createInterval = async (interval: Interval) => {
|
||||
try {
|
||||
const response = await databases.createDocument(
|
||||
@@ -315,15 +242,8 @@ export const useScheduleStore = defineStore('schedule', () => {
|
||||
};
|
||||
|
||||
return {
|
||||
reservations,
|
||||
timeblocks,
|
||||
timeblockTemplates,
|
||||
getBoatReservations,
|
||||
getConflictingReservations,
|
||||
addOrCreateReservation,
|
||||
isReservationOverlapped,
|
||||
isResourceTimeOverlapped,
|
||||
fetchReservations,
|
||||
getIntervalsForDate,
|
||||
getIntervals,
|
||||
fetchIntervals,
|
||||
|
||||
Reference in New Issue
Block a user