Skip to content

Commit

Permalink
vinvoor: fix crash when no scans are registered
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Jul 16, 2024
1 parent dc2ef3d commit 5d0f872
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 30 deletions.
6 changes: 3 additions & 3 deletions vinvoor/src/cards/CardsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const getComparator = <Key extends keyof Card>(
order: TableOrder,
orderBy: Key
): ((
a: { [key in Key]: number | string },
b: { [key in Key]: number | string }
a: { [key in Key]: number | string | Date },
b: { [key in Key]: number | string | Date }
) => number) => {
return order === "desc"
? (a, b) => descendingComparator(a, b, orderBy)
Expand Down Expand Up @@ -115,7 +115,7 @@ export const CardsTable = () => {

const visibleRows = useMemo(
() =>
stableSort(cards, getComparator(order, orderBy)).slice(
stableSort<Card>(cards, getComparator(order, orderBy)).slice(
page * rowsPerPage,
page * rowsPerPage + rowsPerPage
),
Expand Down
11 changes: 5 additions & 6 deletions vinvoor/src/overview/checkin/CheckIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import { ScanContext } from "../Overview";
export const CheckIn = () => {
const { scans } = useContext(ScanContext);

const checkedIn = isTheSameDay(
scans[scans.length - 1].scanTime,
new Date()
);
const checkedIn =
scans.length > 0 &&
isTheSameDay(scans[scans.length - 1].scanTime, new Date());

return checkedIn ? (
<Alert
Expand All @@ -21,7 +20,7 @@ export const CheckIn = () => {
}}
>
<AlertTitle>Checked in</AlertTitle>
Thank you for stopping by the kelder!
Nice of you to stop by!
</Alert>
) : (
<Alert
Expand All @@ -32,7 +31,7 @@ export const CheckIn = () => {
}}
>
<AlertTitle>Not checked in</AlertTitle>
We miss you in the kelder!
We miss you!
</Alert>
);
};
32 changes: 11 additions & 21 deletions vinvoor/src/overview/streak/Streak.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ const isStreakDay = (date1: Date, date2: Date) => {
const getStreak = (scans: readonly Scan[]): [boolean, number] => {
let streak = 0;
const isOnStreak =
isTheSameDay(scans[scans.length - 1].scanTime, new Date()) ||
isWeekendBetween(scans[scans.length - 1].scanTime, new Date());
scans.length > 0 &&
(isTheSameDay(scans[scans.length - 1].scanTime, new Date()) ||
isWeekendBetween(scans[scans.length - 1].scanTime, new Date()));

if (isOnStreak) {
let i = scans.length;
Expand All @@ -59,32 +60,21 @@ export const Streak = () => {
const { scans } = useContext(ScanContext);
const [isOnStreak, streak] = getStreak(scans);

return isOnStreak ? (
<Box display="flex" alignItems="flex-end" justifyContent="center">
<Typography
variant="h2"
color="primary"
fontWeight="bold"
sx={{ mr: 1 }}
>
{streak}
</Typography>
<Typography variant="body2" color="primary">
day{streak > 1 ? "s" : ""} streak
</Typography>
</Box>
) : (
<Box display="flex" alignItems="flex-end" justifyContent="center">
const color = isOnStreak ? "primary" : "error";
const textEnd = isOnStreak ? "streak" : "absent";

return (
<Box display="flex" alignItems="flex-end" justifyContent="end">
<Typography
variant="h2"
color="error"
color={color}
fontWeight="bold"
sx={{ mr: 1 }}
>
{streak}
</Typography>
<Typography variant="body2" color="error">
day{streak > 1 ? "s" : ""} absent
<Typography variant="body2" color={color}>
day{streak > 1 ? "s" : ""} {textEnd}
</Typography>
</Box>
);
Expand Down

0 comments on commit 5d0f872

Please sign in to comment.