Some timeblock stuff working
This commit is contained in:
@@ -15,6 +15,30 @@ import {
|
||||
Timeblock,
|
||||
} from './schedule.types';
|
||||
import { AppwriteIds, databases } from 'src/boot/appwrite';
|
||||
import { ID, Models } from 'appwrite';
|
||||
|
||||
export function arrayToTimeTuples(arr: string[]) {
|
||||
const timeTuples: TimeTuple[] = [];
|
||||
for (let i = 0; i < arr.length; i += 2) {
|
||||
timeTuples.push([arr[i], arr[i + 1]]);
|
||||
}
|
||||
return timeTuples;
|
||||
}
|
||||
|
||||
export function buildTimeBlock(
|
||||
resource: Boat,
|
||||
time: TimeTuple,
|
||||
blockDate: string
|
||||
): Timeblock {
|
||||
/* When the time zone offset is absent, date-only forms are interpreted
|
||||
as a UTC time and date-time forms are interpreted as local time. */
|
||||
const result = {
|
||||
boatId: resource.$id,
|
||||
start: new Date(blockDate + 'T' + time[0]).toISOString(),
|
||||
end: new Date(blockDate + 'T' + time[1]).toISOString(),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
export const useScheduleStore = defineStore('schedule', () => {
|
||||
// TODO: Implement functions to dynamically pull this data.
|
||||
@@ -22,11 +46,16 @@ export const useScheduleStore = defineStore('schedule', () => {
|
||||
const timeblocks = ref<Timeblock[]>([]);
|
||||
const timeblockTemplates = ref<TimeBlockTemplate[]>([]);
|
||||
|
||||
const getTimeblocks = (date: Timestamp, boat: Boat): Timeblock[] => {
|
||||
return timeblocks.value.filter((block) => {
|
||||
return (
|
||||
compareDate(parseDate(new Date(block.start)) as Timestamp, date) &&
|
||||
block.boatId === boat.$id
|
||||
);
|
||||
});
|
||||
};
|
||||
const getTimeblocksForDate = (date: string): Timeblock[] => {
|
||||
// TODO: This needs to actually make sure we have the dates we need, stay in sync, etc.
|
||||
if (!timeblocks.value) {
|
||||
fetchTimeBlocks();
|
||||
}
|
||||
return timeblocks.value.filter((b) => {
|
||||
return compareDate(
|
||||
parseDate(new Date(b.start)) as Timestamp,
|
||||
@@ -68,14 +97,14 @@ export const useScheduleStore = defineStore('schedule', () => {
|
||||
AppwriteIds.databaseId,
|
||||
AppwriteIds.collection.timeBlockTemplate
|
||||
);
|
||||
const res = response.documents.map((d) => {
|
||||
const timeTuples: TimeTuple[] = [];
|
||||
for (let i = 0; i < d.timeTuple.length; i += 2) {
|
||||
timeTuples.push([d.timeTuple[i], d.timeTuple[i + 1]]);
|
||||
timeblockTemplates.value = response.documents.map(
|
||||
(d: Models.Document): TimeBlockTemplate => {
|
||||
return {
|
||||
...d,
|
||||
timeTuples: arrayToTimeTuples(d.timeTuple),
|
||||
} as TimeBlockTemplate;
|
||||
}
|
||||
return { ...d, timeTuple: timeTuples };
|
||||
}) as TimeBlockTemplate[];
|
||||
timeblockTemplates.value = res;
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch timeblock templates', error);
|
||||
}
|
||||
@@ -139,6 +168,20 @@ export const useScheduleStore = defineStore('schedule', () => {
|
||||
: reservations.value.push(reservation);
|
||||
};
|
||||
|
||||
const createTimeblock = async (block: Timeblock) => {
|
||||
try {
|
||||
const response = await databases.createDocument(
|
||||
AppwriteIds.databaseId,
|
||||
AppwriteIds.collection.timeBlock,
|
||||
ID.unique(),
|
||||
block
|
||||
);
|
||||
timeblocks.value.push(response as Timeblock);
|
||||
} catch (e) {
|
||||
console.log('Error creating Timeblock: ' + e);
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
reservations,
|
||||
timeblocks,
|
||||
@@ -146,10 +189,12 @@ export const useScheduleStore = defineStore('schedule', () => {
|
||||
getBoatReservations,
|
||||
getConflictingReservations,
|
||||
getTimeblocksForDate,
|
||||
getTimeblocks,
|
||||
fetchTimeBlocks,
|
||||
fetchTimeBlockTemplates,
|
||||
getNewId,
|
||||
addOrCreateReservation,
|
||||
createTimeblock,
|
||||
isReservationOverlapped,
|
||||
isResourceTimeOverlapped,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user