Skip to content

Commit

Permalink
wip mark all unspecified as busy
Browse files Browse the repository at this point in the history
  • Loading branch information
jho44 committed Feb 16, 2024
1 parent ef66d9e commit dbe0b39
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 54 deletions.
41 changes: 0 additions & 41 deletions src/lib/logics/Calendar/ScheduleTable/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,47 +41,6 @@ export const markRowUnavailableLocally = ({
return displayedRows;
};

const requestToMarkMultipleRowsAsBusy = async (monthDays: string[]) => {
await writeReq('/db', {
type: 'scheduleMultipleBusy',
days: monthDays
});
};

export const markUnspecifiedRowsAsBusy = async ({
displayedRows,
dbRows
}: {
displayedRows: Row[];
dbRows: Row[];
}) => {
const unspecifiedInds = displayedRows
.map((r, i) => {
if (!r.availRange) return i;
return null;
})
.filter((i) => i !== null) as number[];

// now update UI all at once
unspecifiedInds.map((i) =>
markRowUnavailableLocally({
i,
displayedRows,
// dbRow: dbRows[i],
status: AvailabilityStatus.BUSY
})
);

try {
await requestToMarkMultipleRowsAsBusy(unspecifiedInds.map((i) => displayedRows[i].monthDay));
} catch (err) {
console.error(err);
console.error('Something went wrong with marking unspecified rows as busy');
displayedRows = [...dbRows];
return;
}
};

export const showEditor = ({ i, openedRows }: { i: number; openedRows: Set<number> }) => {
openedRows.add(i);
tick().then(() => {
Expand Down
11 changes: 10 additions & 1 deletion src/lib/logics/Calendar/Wrapper/logic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DAYS } from '$lib/constants';
import { destructRange } from '$lib/parse';
import type { Row } from '$lib/types';
import { writeReq } from '$lib/utils';
import { AvailabilityStatus, type AvailabilityDate } from '@prisma/client';
import { UNAVAILABLE } from '../_shared/constants';
import { extractAvailRange } from '../_shared/utils';
Expand Down Expand Up @@ -80,6 +81,14 @@ export const initVals = (dbVals: { dbDates: AvailabilityDates; timeZone: string
return {
dbRows,
rowsOnMount,
displayedRows,
displayedRows
};
};

export const requestToMarkMultipleRowsAsBusy = async (monthDays: string[]) => {
console.log(monthDays)
// await writeReq('/db', {
// type: 'scheduleMultipleBusy',
// days: monthDays
// });
};
2 changes: 2 additions & 0 deletions src/lib/server/dbRoutes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import rejectFriendReq from './rejectFriendReq';
import rejectHouseholdInvite from './rejectHouseholdInvite';
import upsertDate from './upsertDate';
import upsertHousehold from './upsertHousehold';
import upsertUnspecifiedDatesAsBusy from './upsertUnspecifiedDatesAsBusy';
import upsertUser from './upsertUser';

export {
Expand All @@ -19,5 +20,6 @@ export {
rejectHouseholdInvite,
upsertDate,
upsertHousehold,
upsertUnspecifiedDatesAsBusy,
upsertUser
};
107 changes: 107 additions & 0 deletions src/lib/server/dbRoutes/upsertUnspecifiedDatesAsBusy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { AvailabilityStatus, type User } from '@prisma/client';
import { error } from '@sveltejs/kit';
import { dateNotes } from '../sanitize';
import AvailabilityDateRepository from '../repository/AvailabilityDate';

export default async function upsertUnspecifiedDatesAsBusy(
req: {
},
user: User
) {
const { householdId } = user;
if (!householdId) {
throw error(401, {
message: 'You need to create / join a household before saving a schedule'
});
}


[...Array(21).keys()].forEach((x) => {
const date = new Date(new Date().setDate(now.getDate() + x));
const englishDay = DAYS[date.getDay()];
const monthDay = `${date.getMonth() + 1}/${date.getDate()}`;

if (allAvailableDates && monthDay in allAvailableDates) {
const availRange = allAvailableDates[monthDay].availRange;

if (availRange === 'Busy') {
if (lastIsBusy) {
const lastEntry = res[res.length - 1].availRange;
// if dateRange has multiple days, then just change the last date
// otherwise just append new date to last one with a hyphen
const separator = '-';
if (lastEntry.includes(separator)) {
const dates = lastEntry.split(separator);
dates[dates.length - 1] = `${englishDay} ${monthDay}`;
res[res.length - 1].availRange = dates.join(separator);
} else {
res[res.length - 1].availRange += `${separator}${englishDay} ${monthDay}`;
}
} else {
lastIsBusy = true;
res.push({
status: 'Busy',
availRange: `${englishDay} ${monthDay}`
});
}
} else {
let emoticons = '';
const { startHr, startMin, endHr, endMin } = destructRange(availRange) as {
[key: string]: number;
};
const notes = allAvailableDates[monthDay].notes;
if (allAvailableDates[monthDay].emoticons) {
for (const emoji of allAvailableDates[monthDay].emoticons.split(',')) {
emoticons += EMOTICONS_REVERSE[emoji];
}
}

lastIsBusy = false;

const payload = {
englishDay,
monthDay,
availRange,
notes,
emoticons: emoticons.length ? emoticons : 'N/A',
startHr,
startMin,
endHr,
endMin,
householdId
};
if (monthDay in dates) {
dates[monthDay].push(payload);
} else {
dates[monthDay] = [payload];
}

res.push({ ...payload, status: 'Available' });
}
} else {
lastIsBusy = false;
}
});
// const res = {
// // date,
// status: AvailabilityStatus.BUSY,
// notes: '',
// emoticons: undefined,
// startTime: undefined,
// endTime: undefined
// };

// if (status === AvailabilityStatus.BUSY) {
// await AvailabilityDateRepository.upsert({ householdId, ...res });
// return res;
// }

// // if an entry for this date already exists in the db, then patch it
// // otherwise create it
// const sanitizedNotes = dateNotes(notes ?? '');
// res.notes = sanitizedNotes;

// await AvailabilityDateRepository.upsert({ householdId, ...res });

// return res;
}
43 changes: 31 additions & 12 deletions src/routes/calendar/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import Notifier from '$lib/components/Calendar/Notifier.svelte';
import ScheduleTable from '$lib/components/Calendar/ScheduleTable.svelte';
import NavBar from '$lib/components/NavBar.svelte';
import { markUnspecifiedRowsAsBusy } from '$lib/logics/Calendar/ScheduleTable/logic';
import { initVals } from '$lib/logics/Calendar/Wrapper/logic';
import { initVals, requestToMarkMultipleRowsAsBusy } from '$lib/logics/Calendar/Wrapper/logic';
import type { Row } from '$lib/types';
import { AvailabilityStatus } from '@prisma/client';
import { onMount } from 'svelte';
let { dbAvailabilityDates, user, kidNames, circleInfo } = $page.data;
Expand All @@ -22,6 +22,31 @@
});
$: displayedRows = [...dbRows];
const markUnspecifiedRowsAsBusy = async () => {
const unspecifiedInds = displayedRows
.map((r, i) => {
if (r.availRange === AvailabilityStatus.UNSPECIFIED) return i;
return null;
})
.filter((i) => i !== null) as number[];
// now update UI all at once
unspecifiedInds.map((i) => {
displayedRows[i].notes = '';
displayedRows[i].emoticons = new Set();
displayedRows[i].availRange = AvailabilityStatus.BUSY;
});
try {
await requestToMarkMultipleRowsAsBusy(unspecifiedInds.map((i) => displayedRows[i].monthDay));
} catch (err) {
alert('Something went wrong with saving'); // TODO: come up with better UI for showing err
console.error(err);
console.error('Something went wrong with marking unspecified rows as busy');
displayedRows = [...dbRows];
}
};
</script>

<div>
Expand All @@ -34,18 +59,12 @@
on:changed:displayedRow={(e) => (displayedRows[e.detail.i] = e.detail.row)}
on:markedRow={() => (dbRows = displayedRows)}
on:markedRow:available={(e) => {
displayedRows[e.detail.i] = e.detail.newRow
dbRows = displayedRows
displayedRows[e.detail.i] = e.detail.newRow;
dbRows = displayedRows;
}}
/>
<button
class="mark-all-busy-btn"
style="margin: 1rem;"
on:click={() =>
markUnspecifiedRowsAsBusy({
displayedRows,
dbRows
})}>Mark unspecified days as busy</button
<button class="mark-all-busy-btn" style="margin: 1rem;" on:click={markUnspecifiedRowsAsBusy}
>Mark unspecified days as busy</button
>
<Notifier {dbRows} {rowsOnMount} {circleInfo} {user} {kidNames} />
</div>
Expand Down

0 comments on commit dbe0b39

Please sign in to comment.