-
-
Notifications
You must be signed in to change notification settings - Fork 735
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for broadcast calendar (#2597)
Co-authored-by: Zhao <[email protected]> Co-authored-by: gpbl <[email protected]>
- Loading branch information
1 parent
5800719
commit 6833704
Showing
9 changed files
with
157 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from "react"; | ||
|
||
import { DayPicker } from "react-day-picker"; | ||
|
||
export function BroadcastCalendar() { | ||
return <DayPicker showOutsideDays showWeekNumber broadcastCalendar />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { | ||
getBroadcastWeeksInMonth, | ||
startOfBroadcastWeek, | ||
endOfBroadcastWeek | ||
} from "./broadcastCalendar"; | ||
|
||
describe("broadcastCalendar", () => { | ||
test("getBroadcastWeeksInMonth should return correct number of weeks", () => { | ||
// Test for a month with 5 weeks | ||
expect(getBroadcastWeeksInMonth(new Date(2023, 0, 1))).toBe(5); // January 2023 | ||
// Test for a month with 4 weeks | ||
expect(getBroadcastWeeksInMonth(new Date(2023, 1, 1))).toBe(4); // February 2023 | ||
}); | ||
|
||
test("startOfBroadcastWeek should return correct start date", () => { | ||
// Test for a month starting on a Monday | ||
expect(startOfBroadcastWeek(new Date(2023, 0, 1))).toEqual( | ||
new Date(2022, 11, 26) | ||
); // December 26 2022 | ||
// Test for a month starting on a Wednesday | ||
expect(startOfBroadcastWeek(new Date(2020, 0, 1))).toEqual( | ||
new Date(2019, 11, 30) | ||
); // December 30 2019 | ||
}); | ||
|
||
test("endOfBroadcastWeek should return correct end date", () => { | ||
const startDate = startOfBroadcastWeek(new Date(2023, 0, 1)); | ||
expect(endOfBroadcastWeek(new Date(2023, 0, 1))).toEqual( | ||
new Date( | ||
startDate.getFullYear(), | ||
startDate.getMonth(), | ||
startDate.getDate() + 34 | ||
) | ||
); // January 2023 | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** Return the number of weeks to display in the broadcast calendar. */ | ||
export function getBroadcastWeeksInMonth(month: Date): number { | ||
const NUMBER_OF_5_WEEKS = 5; | ||
const NUMBER_OF_4_WEEKS = 4; | ||
|
||
const firstDayOfMonth = new Date(month.getFullYear(), month.getMonth(), 1); | ||
const dayOfWeekOfFirstDayOfMonth = | ||
firstDayOfMonth.getDay() > 0 ? firstDayOfMonth.getDay() : 7; | ||
const beginDate = new Date( | ||
month.getFullYear(), | ||
month.getMonth(), | ||
month.getDate() - dayOfWeekOfFirstDayOfMonth + 1 | ||
); | ||
const numberOfWeeks = | ||
month.getMonth() === | ||
new Date( | ||
beginDate.getFullYear(), | ||
beginDate.getMonth(), | ||
beginDate.getDate() + NUMBER_OF_5_WEEKS * 7 - 1 | ||
).getMonth() | ||
? NUMBER_OF_5_WEEKS | ||
: NUMBER_OF_4_WEEKS; | ||
return numberOfWeeks; | ||
} | ||
|
||
/** Return the start date of the week in the broadcast calendar. */ | ||
export function startOfBroadcastWeek(date: Date): Date { | ||
const year = date.getFullYear(); | ||
const month = date.getMonth(); | ||
|
||
const firstOfMonth = new Date(year, month, 1); | ||
const dayOfWeek = firstOfMonth.getUTCDay(); // 0 = Sunday, 1 = Monday, etc. | ||
|
||
// If the first of the month is a Monday, then the broadcast month starts on that day. | ||
// Otherwise, the broadcast starts on the previous Monday, and if first of the month is Sunday, then the starts on the previous week's Monday. | ||
if (dayOfWeek === 1) { | ||
return firstOfMonth; | ||
} else if (dayOfWeek === 0) { | ||
const startDate = new Date(year, month, 1 - dayOfWeek + 1 - 7); | ||
return startDate; | ||
} else { | ||
const startDate = new Date(year, month, 1 - dayOfWeek + 1); | ||
return startDate; | ||
} | ||
} | ||
|
||
/** Return the end date of the week in the broadcast calendar. */ | ||
export function endOfBroadcastWeek(date: Date): Date { | ||
const startDate = startOfBroadcastWeek(date); | ||
// end date based on the weeks to show, it is calculated by getBroadcastWeeksInMonth | ||
const numberOfWeeks = getBroadcastWeeksInMonth(date); | ||
const endDate = new Date( | ||
startDate.getFullYear(), | ||
startDate.getMonth(), | ||
startDate.getDate() + numberOfWeeks * 7 - 1 | ||
); | ||
return endDate; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters