Rename TimeBlock to Interva. More Interval functionality.
Some checks failed
Build BAB Application Deployment Artifact / build (push) Failing after 1m40s
Some checks failed
Build BAB Application Deployment Artifact / build (push) Failing after 1m40s
This commit is contained in:
@@ -10,18 +10,13 @@ import {
|
||||
|
||||
import {
|
||||
Reservation,
|
||||
TimeBlockTemplate,
|
||||
IntervalTemplate,
|
||||
TimeTuple,
|
||||
TimeBlock,
|
||||
Interval,
|
||||
} from './schedule.types';
|
||||
import { AppwriteIds, databases } from 'src/boot/appwrite';
|
||||
import { ID, Models } from 'appwrite';
|
||||
|
||||
export type Interval = {
|
||||
start: string;
|
||||
end: string;
|
||||
};
|
||||
|
||||
export function arrayToTimeTuples(arr: string[]) {
|
||||
const timeTuples: TimeTuple[] = [];
|
||||
for (let i = 0; i < arr.length; i += 2) {
|
||||
@@ -34,16 +29,17 @@ export function timeTuplesOverlapped(tuples: TimeTuple[]): Interval[] {
|
||||
return blocksOverlapped(
|
||||
tuples.map((tuples) => {
|
||||
return {
|
||||
boatId: '',
|
||||
start: '01/01/2001 ' + tuples[0],
|
||||
end: '01/01/2001 ' + tuples[1],
|
||||
};
|
||||
})
|
||||
).map((t) => {
|
||||
return { start: t.start.split(' ')[1], end: t.end.split(' ')[1] };
|
||||
return { ...t, start: t.start.split(' ')[1], end: t.end.split(' ')[1] };
|
||||
});
|
||||
}
|
||||
|
||||
export function blocksOverlapped(blocks: TimeBlock[] | Interval[]): Interval[] {
|
||||
export function blocksOverlapped(blocks: Interval[] | Interval[]): Interval[] {
|
||||
return Array.from(
|
||||
new Set(
|
||||
blocks
|
||||
@@ -60,26 +56,30 @@ export function blocksOverlapped(blocks: TimeBlock[] | Interval[]): Interval[] {
|
||||
export function copyTimeTuples(tuples: TimeTuple[]): TimeTuple[] {
|
||||
return tuples.map((t) => Object.assign([], t));
|
||||
}
|
||||
export function copyTimeBlockTemplate(
|
||||
template: TimeBlockTemplate
|
||||
): TimeBlockTemplate {
|
||||
export function copyIntervalTemplate(
|
||||
template: IntervalTemplate
|
||||
): IntervalTemplate {
|
||||
return {
|
||||
...template,
|
||||
timeTuples: copyTimeTuples(template.timeTuples),
|
||||
timeTuples: copyTimeTuples(template.timeTuples || []),
|
||||
};
|
||||
}
|
||||
|
||||
export function buildTimeBlock(
|
||||
export function buildISODate(date: string, time: string | null): string {
|
||||
return new Date(date + 'T' + time || '00:00').toISOString();
|
||||
}
|
||||
|
||||
export function buildInterval(
|
||||
resource: Boat,
|
||||
time: TimeTuple,
|
||||
blockDate: string
|
||||
): TimeBlock {
|
||||
): Interval {
|
||||
/* 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(),
|
||||
start: buildISODate(blockDate, time[0]),
|
||||
end: buildISODate(blockDate, time[1]),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
@@ -87,10 +87,10 @@ export function buildTimeBlock(
|
||||
export const useScheduleStore = defineStore('schedule', () => {
|
||||
// TODO: Implement functions to dynamically pull this data.
|
||||
const reservations = ref<Reservation[]>([]);
|
||||
const timeblocks = ref<TimeBlock[]>([]);
|
||||
const timeblockTemplates = ref<TimeBlockTemplate[]>([]);
|
||||
const timeblocks = ref<Interval[]>([]);
|
||||
const timeblockTemplates = ref<IntervalTemplate[]>([]);
|
||||
|
||||
const getTimeBlocks = (date: Timestamp, boat: Boat): TimeBlock[] => {
|
||||
const getIntervals = (date: Timestamp, boat: Boat): Interval[] => {
|
||||
return timeblocks.value.filter((block) => {
|
||||
return (
|
||||
compareDate(parseDate(new Date(block.start)) as Timestamp, date) &&
|
||||
@@ -98,7 +98,7 @@ export const useScheduleStore = defineStore('schedule', () => {
|
||||
);
|
||||
});
|
||||
};
|
||||
const getTimeBlocksForDate = (date: string): TimeBlock[] => {
|
||||
const getIntervalsForDate = (date: string): Interval[] => {
|
||||
// TODO: This needs to actually make sure we have the dates we need, stay in sync, etc.
|
||||
return timeblocks.value.filter((b) => {
|
||||
return compareDate(
|
||||
@@ -123,37 +123,37 @@ export const useScheduleStore = defineStore('schedule', () => {
|
||||
});
|
||||
};
|
||||
|
||||
async function fetchTimeBlocks() {
|
||||
async function fetchIntervals() {
|
||||
try {
|
||||
const response = await databases.listDocuments(
|
||||
AppwriteIds.databaseId,
|
||||
AppwriteIds.collection.timeBlock
|
||||
);
|
||||
timeblocks.value = response.documents as TimeBlock[];
|
||||
timeblocks.value = response.documents as Interval[];
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch timeblocks', error);
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchTimeBlockTemplates() {
|
||||
async function fetchIntervalTemplates() {
|
||||
try {
|
||||
const response = await databases.listDocuments(
|
||||
AppwriteIds.databaseId,
|
||||
AppwriteIds.collection.timeBlockTemplate
|
||||
);
|
||||
timeblockTemplates.value = response.documents.map(
|
||||
(d: Models.Document): TimeBlockTemplate => {
|
||||
(d: Models.Document): IntervalTemplate => {
|
||||
return {
|
||||
...d,
|
||||
timeTuples: arrayToTimeTuples(d.timeTuple),
|
||||
} as TimeBlockTemplate;
|
||||
} as IntervalTemplate;
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch timeblock templates', error);
|
||||
}
|
||||
}
|
||||
// const getConflicts = (timeblock: TimeBlock, boat: Boat) => {
|
||||
// const getConflicts = (timeblock: Interval, boat: Boat) => {
|
||||
// const start = date.buildDate({
|
||||
// hour: timeblock.start.hour,
|
||||
// minute: timeblock.start.minute,
|
||||
@@ -212,21 +212,50 @@ export const useScheduleStore = defineStore('schedule', () => {
|
||||
: reservations.value.push(reservation);
|
||||
};
|
||||
|
||||
const createTimeBlock = async (block: TimeBlock) => {
|
||||
const createInterval = async (interval: Interval) => {
|
||||
try {
|
||||
const response = await databases.createDocument(
|
||||
AppwriteIds.databaseId,
|
||||
AppwriteIds.collection.timeBlock,
|
||||
ID.unique(),
|
||||
block
|
||||
interval
|
||||
);
|
||||
timeblocks.value.push(response as TimeBlock);
|
||||
timeblocks.value.push(response as Interval);
|
||||
} catch (e) {
|
||||
console.error('Error creating TimeBlock: ' + e);
|
||||
console.error('Error creating Interval: ' + e);
|
||||
}
|
||||
};
|
||||
|
||||
const createTimeBlockTemplate = async (template: TimeBlockTemplate) => {
|
||||
const updateInterval = async (interval: Interval) => {
|
||||
try {
|
||||
if (interval.$id) {
|
||||
const response = await databases.updateDocument(
|
||||
AppwriteIds.databaseId,
|
||||
AppwriteIds.collection.timeBlock,
|
||||
interval.$id,
|
||||
{ ...interval, $id: undefined }
|
||||
);
|
||||
timeblocks.value.push(response as Interval);
|
||||
console.log(interval, response);
|
||||
} else {
|
||||
console.error('Update interval called without an ID');
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error updating Interval: ' + e);
|
||||
}
|
||||
};
|
||||
const deleteInterval = async (id: string) => {
|
||||
try {
|
||||
await databases.deleteDocument(
|
||||
AppwriteIds.databaseId,
|
||||
AppwriteIds.collection.timeBlock,
|
||||
id
|
||||
);
|
||||
timeblocks.value = timeblocks.value.filter((block) => block.$id !== id);
|
||||
} catch (e) {
|
||||
console.error('Error deleting Interval: ' + e);
|
||||
}
|
||||
};
|
||||
const createIntervalTemplate = async (template: IntervalTemplate) => {
|
||||
try {
|
||||
const response = await databases.createDocument(
|
||||
AppwriteIds.databaseId,
|
||||
@@ -234,12 +263,12 @@ export const useScheduleStore = defineStore('schedule', () => {
|
||||
ID.unique(),
|
||||
{ name: template.name, timeTuple: template.timeTuples.flat(2) }
|
||||
);
|
||||
timeblockTemplates.value.push(response as TimeBlockTemplate);
|
||||
timeblockTemplates.value.push(response as IntervalTemplate);
|
||||
} catch (e) {
|
||||
console.error('Error updating TimeBlockTemplate: ' + e);
|
||||
console.error('Error updating IntervalTemplate: ' + e);
|
||||
}
|
||||
};
|
||||
const deleteTimeBlockTemplate = async (id: string) => {
|
||||
const deleteIntervalTemplate = async (id: string) => {
|
||||
try {
|
||||
await databases.deleteDocument(
|
||||
AppwriteIds.databaseId,
|
||||
@@ -250,11 +279,11 @@ export const useScheduleStore = defineStore('schedule', () => {
|
||||
(template) => template.$id !== id
|
||||
);
|
||||
} catch (e) {
|
||||
console.error('Error deleting TimeBlockTemplate: ' + e);
|
||||
console.error('Error deleting IntervalTemplate: ' + e);
|
||||
}
|
||||
};
|
||||
const updateTimeBlockTemplate = async (
|
||||
template: TimeBlockTemplate,
|
||||
const updateIntervalTemplate = async (
|
||||
template: IntervalTemplate,
|
||||
id: string
|
||||
) => {
|
||||
try {
|
||||
@@ -268,10 +297,15 @@ export const useScheduleStore = defineStore('schedule', () => {
|
||||
}
|
||||
);
|
||||
timeblockTemplates.value = timeblockTemplates.value.map((b) =>
|
||||
b.$id !== id ? b : (response as TimeBlockTemplate)
|
||||
b.$id !== id
|
||||
? b
|
||||
: ({
|
||||
...response,
|
||||
timeTuples: arrayToTimeTuples(response.timeTuple),
|
||||
} as IntervalTemplate)
|
||||
);
|
||||
} catch (e) {
|
||||
console.error('Error updating TimeBlockTemplate: ' + e);
|
||||
console.error('Error updating IntervalTemplate: ' + e);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -281,16 +315,18 @@ export const useScheduleStore = defineStore('schedule', () => {
|
||||
timeblockTemplates,
|
||||
getBoatReservations,
|
||||
getConflictingReservations,
|
||||
getTimeBlocksForDate,
|
||||
getTimeBlocks,
|
||||
fetchTimeBlocks,
|
||||
fetchTimeBlockTemplates,
|
||||
getIntervalsForDate,
|
||||
getIntervals,
|
||||
fetchIntervals,
|
||||
fetchIntervalTemplates,
|
||||
getNewId,
|
||||
addOrCreateReservation,
|
||||
createTimeBlock,
|
||||
createTimeBlockTemplate,
|
||||
deleteTimeBlockTemplate,
|
||||
updateTimeBlockTemplate,
|
||||
createInterval,
|
||||
updateInterval,
|
||||
deleteInterval,
|
||||
createIntervalTemplate,
|
||||
deleteIntervalTemplate,
|
||||
updateIntervalTemplate,
|
||||
isReservationOverlapped,
|
||||
isResourceTimeOverlapped,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user