Skip to content

Commit

Permalink
♻️ refactor(ui/routes/byWeek): Extract useWeekYearFormat.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Mar 2, 2022
1 parent 389c83c commit 1e25eb3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
15 changes: 15 additions & 0 deletions imports/i18n/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,18 @@ export const useDateFormatRange = (format, options?) => {
[formatPart],
);
};

export const useWeekYearFormat = () => {
const dateFormat = useDateFormat();
return useMemo(() => {
const yearFormat = (date: Date) =>
dateFormat(date, 'YYYY', {
useAdditionalWeekYearTokens: true,
});
const weekFormat = (date: Date) => dateFormat(date, 'ww');
return {
yearFormat,
weekFormat,
};
}, [dateFormat]);
};
14 changes: 4 additions & 10 deletions imports/ui/routes/byWeek.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {useParams} from 'react-router-dom';

import startOfToday from 'date-fns/startOfToday';

import {useDateFormat} from '../../i18n/datetime';
import {useWeekYearFormat} from '../../i18n/datetime';

import branch, {BranchProps} from './branch';

Expand All @@ -17,17 +17,11 @@ const ByWeekRoute = <C extends React.ElementType>({
component: Component,
props,
}: BranchProps<C>) => {
const dateFormat = useDateFormat();
const {yearFormat, weekFormat} = useWeekYearFormat();
const today = startOfToday();
const params = useParams<Params>();
const year = Number.parseInt(
params.year ??
dateFormat(today, 'YYYY', {
useAdditionalWeekYearTokens: true,
}),
10,
);
const week = Number.parseInt(params.week ?? dateFormat(today, 'ww'), 10);
const year = Number.parseInt(params.year ?? yearFormat(today), 10);
const week = Number.parseInt(params.week ?? weekFormat(today), 10);
return <Component year={year} week={week} {...props} />;
};

Expand Down

0 comments on commit 1e25eb3

Please sign in to comment.