Skip to content

Commit

Permalink
Merge pull request #780 from eisbuk/fix/slots_duration
Browse files Browse the repository at this point in the history
Fix/slots duration
  • Loading branch information
ikusteu committed Jul 9, 2023
2 parents d035f39 + 70a428b commit a4ce928
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 10 deletions.
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;
};
/**
* 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];
};

0 comments on commit a4ce928

Please sign in to comment.