Skip to content

Commit 56c295b

Browse files
committed
refactor(hooks): extend useDateInterval with conference dates and simplify LiveView date checks
1 parent 6508e77 commit 56c295b

File tree

3 files changed

+131
-47
lines changed

3 files changed

+131
-47
lines changed

src/hooks/useDateInterval.test.ts

Lines changed: 104 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,111 @@
1-
import { renderHook } from '@testing-library/react-hooks';
2-
import { useDateInterval } from './useDateInterval';
3-
4-
const today = new Date('2023-10-10');
5-
6-
const edition = {
7-
tickets: {
8-
startDay: '2023-10-01',
9-
endDay: '2023-10-15',
10-
},
11-
cfp: {
12-
startDay: '2023-09-01',
13-
endDay: '2023-09-30',
14-
},
15-
sponsors: {
16-
startDate: '2023-10-05',
17-
endDate: '2023-10-20',
18-
},
19-
};
20-
21-
describe('useDateInterval', () => {
22-
it('should return correct disabled states for tickets, cfp, and sponsors', () => {
1+
import { renderHook } from "@testing-library/react-hooks";
2+
import { useDateInterval } from "./useDateInterval";
3+
4+
describe("useDateInterval", () => {
5+
// Complete edition object with all required properties
6+
const createEdition = (overrides = {}) => ({
7+
tickets: {
8+
startDay: "2023-10-01",
9+
endDay: "2023-10-15",
10+
},
11+
cfp: {
12+
startDay: "2023-09-01",
13+
endDay: "2023-09-30",
14+
},
15+
sponsors: {
16+
startDate: "2023-10-05",
17+
endDate: "2023-10-20",
18+
},
19+
startDay: "2023-10-25",
20+
endDay: "2023-10-27",
21+
...overrides,
22+
});
23+
24+
it("should return correct states during the conference", () => {
25+
const today = new Date("2023-10-26");
26+
const edition = createEdition();
27+
const { result } = renderHook(() => useDateInterval(today, edition));
28+
29+
expect(result.current.isTicketsDisabled).toBe(true);
30+
expect(result.current.isCfpDisabled).toBe(true);
31+
expect(result.current.isSponsorDisabled).toBe(true);
32+
expect(result.current.isConferenceActive).toBe(true);
33+
});
34+
35+
it("should return correct states during ticket sales period", () => {
36+
const today = new Date("2023-10-10");
37+
const edition = createEdition();
2338
const { result } = renderHook(() => useDateInterval(today, edition));
2439

2540
expect(result.current.isTicketsDisabled).toBe(false);
2641
expect(result.current.isCfpDisabled).toBe(true);
2742
expect(result.current.isSponsorDisabled).toBe(false);
43+
expect(result.current.isConferenceActive).toBe(false);
44+
});
45+
46+
it("should return correct states during CFP period", () => {
47+
const today = new Date("2023-09-15");
48+
const edition = createEdition();
49+
const { result } = renderHook(() => useDateInterval(today, edition));
50+
51+
expect(result.current.isTicketsDisabled).toBe(true);
52+
expect(result.current.isCfpDisabled).toBe(false);
53+
expect(result.current.isSponsorDisabled).toBe(true);
54+
expect(result.current.isConferenceActive).toBe(false);
55+
});
56+
57+
it("should return correct states during sponsor registration period", () => {
58+
const today = new Date("2023-10-18");
59+
const edition = createEdition();
60+
const { result } = renderHook(() => useDateInterval(today, edition));
61+
62+
expect(result.current.isTicketsDisabled).toBe(true);
63+
expect(result.current.isCfpDisabled).toBe(true);
64+
expect(result.current.isSponsorDisabled).toBe(false);
65+
expect(result.current.isConferenceActive).toBe(false);
66+
});
67+
68+
it("should return all disabled states before any period starts", () => {
69+
const today = new Date("2023-08-15");
70+
const edition = createEdition();
71+
const { result } = renderHook(() => useDateInterval(today, edition));
72+
73+
expect(result.current.isTicketsDisabled).toBe(true);
74+
expect(result.current.isCfpDisabled).toBe(true);
75+
expect(result.current.isSponsorDisabled).toBe(true);
76+
expect(result.current.isConferenceActive).toBe(false);
77+
});
78+
79+
it("should return all disabled states after all periods end", () => {
80+
const today = new Date("2023-11-01");
81+
const edition = createEdition();
82+
const { result } = renderHook(() => useDateInterval(today, edition));
83+
84+
expect(result.current.isTicketsDisabled).toBe(true);
85+
expect(result.current.isCfpDisabled).toBe(true);
86+
expect(result.current.isSponsorDisabled).toBe(true);
87+
expect(result.current.isConferenceActive).toBe(false);
88+
});
89+
90+
it("should handle edge case: exactly on start date", () => {
91+
const today = new Date("2023-10-01");
92+
const edition = createEdition();
93+
const { result } = renderHook(() => useDateInterval(today, edition));
94+
95+
expect(result.current.isTicketsDisabled).toBe(false); // Should be enabled on start date
96+
expect(result.current.isCfpDisabled).toBe(true);
97+
expect(result.current.isSponsorDisabled).toBe(true);
98+
expect(result.current.isConferenceActive).toBe(false);
99+
});
100+
101+
it("should handle edge case: exactly on end date", () => {
102+
const today = new Date("2023-10-15");
103+
const edition = createEdition();
104+
const { result } = renderHook(() => useDateInterval(today, edition));
105+
106+
expect(result.current.isTicketsDisabled).toBe(false); // Should be enabled on end date
107+
expect(result.current.isCfpDisabled).toBe(true);
108+
expect(result.current.isSponsorDisabled).toBe(false);
109+
expect(result.current.isConferenceActive).toBe(false);
28110
});
29111
});

src/hooks/useDateInterval.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import { isWithinInterval } from "date-fns";
22

33
type DateInterval = {
4-
tickets:{
5-
startDay: string,
6-
endDay: string,
7-
},
8-
cfp:{
9-
startDay: string,
10-
endDay: string,
11-
},
12-
sponsors:{
13-
startDate: string,
14-
endDate: string,
15-
}
16-
}
4+
cfp: {
5+
startDay: string;
6+
endDay: string;
7+
};
8+
endDay: string;
9+
sponsors: {
10+
startDate: string;
11+
endDate: string;
12+
};
13+
startDay: string;
14+
tickets: {
15+
startDay: string;
16+
endDay: string;
17+
};
18+
};
1719

1820
export const useDateInterval = (today: Date, edition: DateInterval) => {
1921
const ticketStartDay = new Date(edition.tickets.startDay);
@@ -22,6 +24,8 @@ export const useDateInterval = (today: Date, edition: DateInterval) => {
2224
const CFPEndDay = new Date(edition.cfp.endDay);
2325
const sponsorStartDay = new Date(edition.sponsors.startDate);
2426
const sponsorEndDay = new Date(edition.sponsors.endDate);
27+
const conferenceStartDay = new Date(edition.startDay);
28+
const conferenceEndDay = new Date(edition.endDay);
2529

2630
return {
2731
isTicketsDisabled: !isWithinInterval(today, {
@@ -36,5 +40,9 @@ export const useDateInterval = (today: Date, edition: DateInterval) => {
3640
start: sponsorStartDay,
3741
end: sponsorEndDay,
3842
}),
43+
isConferenceActive: isWithinInterval(today, {
44+
start: conferenceStartDay,
45+
end: conferenceEndDay,
46+
}),
3947
};
4048
};

src/views/Talks/LiveView.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,18 @@ import { TalkCard } from "./components/TalkCard";
77
import { StyledMain } from "./Talks.style";
88
import { talkCardAdapter } from "./TalkCardAdapter";
99
import { useSentryErrorReport } from "@hooks/useSentryErrorReport";
10+
import { useDateInterval } from "@hooks/useDateInterval";
11+
import { isWithinInterval } from "date-fns";
1012

1113
const LiveView: FC<React.PropsWithChildren<unknown>> = () => {
1214
const { isLoading, error, data } = useFetchLiveView();
1315
const today = useMemo(() => new Date(), []);
14-
15-
const isBetween = useCallback(
16-
(today: Date, startDate: string, endDate: string): boolean => {
17-
return today >= new Date(startDate) && today <= new Date(endDate);
18-
},
19-
[],
20-
);
16+
const { isConferenceActive } = useDateInterval(today, conference);
2117

2218
const getPredicate = useCallback(
2319
() => (session: UngroupedSession) =>
24-
isBetween(today, session.startsAt, session.endsAt),
25-
[today, isBetween],
20+
isWithinInterval(today, { start: session.startsAt, end: session.endsAt }),
21+
[today],
2622
);
2723

2824
const filteredTalks = useMemo(() => {
@@ -48,9 +44,7 @@ const LiveView: FC<React.PropsWithChildren<unknown>> = () => {
4844

4945
{isLoading && <Loading />}
5046
<article>Live Schedule</article>
51-
{!isBetween(today, conference.startDay, conference.endDay) && (
52-
<h4>The live schedule is not ready yet</h4>
53-
)}
47+
{!isConferenceActive && <h4>The live schedule is not ready yet</h4>}
5448
{filteredTalks?.map((session) => (
5549
<TalkCard key={session.id} {...talkCardAdapter(session)} />
5650
))}

0 commit comments

Comments
 (0)