Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/slots duration #780

Merged
merged 2 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/client/src/utils/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe("Helpers", () => {
});

test("should convert string to number", () => {
const halfStr = "21:00 - 21:20";
const oneStr = "16:00 - 17:00";
const oneHalfStr = "22:00 - 23:30";
const twoStr = "22:00 - 24:00";
Expand All @@ -46,9 +47,11 @@ describe("Helpers", () => {
// const morningResult = convertIntervalToNum(morningStr);
const oneResult = calculateIntervalDuration(oneStr);
const oneHalfResult = calculateIntervalDuration(oneHalfStr);
const halfResult = calculateIntervalDuration(halfStr);
const twoResult = calculateIntervalDuration(twoStr);
const nonStdResult = calculateIntervalDuration(nonStdStr);

expect(halfResult).toBe(0.5);
expect(oneResult).toBe(1);
expect(oneHalfResult).toBe(1.5);
expect(twoResult).toBe(2);
Expand Down
7 changes: 2 additions & 5 deletions packages/client/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const getMillisFromMidnight = (time: string) =>
* @param {string | null} interval - String slot interval
* Converts a string slot interval to a number e.g:
* `null => 0`;
* `"21:00 - 21:20" => 0.5`;
* `"16:00 - 17:00" => 1.0`;
* `"22:00 - 23:30" => 1.5`;
* `"22:00 - 24:00" => 2`;
Expand All @@ -118,11 +119,7 @@ export const calculateIntervalDuration = (interval: string | null) => {
const diffMillis =
getMillisFromMidnight(endTime) - getMillisFromMidnight(startTime);

return diffMillis <= hourInMillis
? 1
: diffMillis <= hourInMillis * 1.5
? 1.5
: 2;
return Math.ceil((diffMillis / hourInMillis) * 2) * 0.5;
};
ikusteu marked this conversation as resolved.
Show resolved Hide resolved
/**
* interpolate text including <p/>
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/IntervalCard/BookingCardContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const BookingCardContainer: React.FC<BookingContainerProps> = ({
};

const containerSizeLookup = {
[IntervalDuration["0.5h"]]: ["w-[200px]", "min-h-[110px]", "px-4", "py-2.5"],
[IntervalDuration["1h"]]: ["w-[220px]", "min-h-[110px]", "px-4", "py-2.5"],
[IntervalDuration["1.5h"]]: ["w-[320px]", "h-[128px]", "px-4", "py-3"],
[IntervalDuration["2h"]]: ["w-[401px]", "h-[146px]", "px-4", "py-3"],
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/IntervalCard/CardContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const getTimestringClasses = (
].join(" ");

const timestringBookingSizeLookup = {
[IntervalDuration["0.5h"]]: ["mb-2", "text-md", "leading-8", "font-semibold"],
[IntervalDuration["1h"]]: ["mb-2", "text-lg", "leading-8", "font-semibold"],
[IntervalDuration["1.5h"]]: [
"mb-2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const runCalculateDurationTableTests = (tests: TestParams[]) => {
describe("IntervalCard", () => {
describe("calculateDuration util", () => {
runCalculateDurationTableTests([
{ startTime: "09:00", endTime: "09:15", want: IntervalDuration["0.5h"] },
{ startTime: "09:00", endTime: "09:20", want: IntervalDuration["0.5h"] },
{ startTime: "09:00", endTime: "09:30", want: IntervalDuration["0.5h"] },

{ startTime: "09:00", endTime: "09:40", want: IntervalDuration["1h"] },
{ startTime: "09:00", endTime: "09:50", want: IntervalDuration["1h"] },
{ startTime: "09:00", endTime: "10:00", want: IntervalDuration["1h"] },
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/IntervalCard/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export enum IntervalCardVariant {
}

export enum IntervalDuration {
"0.5h",
"1h",
"1.5h",
"2h",
Expand Down
7 changes: 2 additions & 5 deletions packages/ui/src/IntervalCard/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ export const calculateDuration = (startTime: string, endTime: string) => {
const diffMillis =
getMillisFromMidnight(endTime) - getMillisFromMidnight(startTime);

return diffMillis <= hourInMillis
? IntervalDuration["1h"]
: diffMillis <= hourInMillis * 1.5
? IntervalDuration["1.5h"]
: IntervalDuration["2h"];
const duration = `${Math.ceil((diffMillis / hourInMillis) * 2) * 0.5}h`;
return IntervalDuration[duration];
};