Switch date operations to date-fns

This commit is contained in:
Vencislav Atanasov 2024-09-28 21:07:31 +03:00
parent e2056c3d72
commit b0c6569685
5 changed files with 30 additions and 21 deletions

View File

@ -1,12 +1,19 @@
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { bg, enGB } from 'date-fns/locale';
import { format } from 'date-fns';
export default function DateHeader({ export default function DateHeader({
date, date,
lang,
}) { }) {
// TODO: format with date-fns const locale = lang === 'bg' ? bg : enGB;
return date.getDate().toString().concat('.').concat((date.getMonth() + 1).toString()).concat(' - ').concat(date.getDay());
return format(date, 'dd MMMM - EEEE', {
locale,
});
} }
DateHeader.propTypes = { DateHeader.propTypes = {
date: PropTypes.object.isRequired, date: PropTypes.object.isRequired,
lang: PropTypes.string,
}; };

View File

@ -1,8 +1,8 @@
import { format } from 'date-fns';
export default function SlotTime({ export default function SlotTime({
start, start,
end, end,
}) { }) {
// TODO: format with date-fns return format(start, 'HH:mm').concat(' - ').concat(format(end, 'HH:mm'));
return start.getHours().toString().concat(':').concat(start.getMinutes()).concat(' - ').concat(end.getHours())
.concat(':').concat(end.getMinutes());
} }

View File

@ -57,7 +57,7 @@ export default function Schedule({
<tbody> <tbody>
{rows.map(row => <tr key={row.id}> {rows.map(row => <tr key={row.id}>
{row.cells.map(cell => <td key={cell.id} {...cell.attributes}> {row.cells.map(cell => <td key={cell.id} {...cell.attributes}>
{cell.dateHeader && <DateHeader date={cell.dateHeader} />} {cell.dateHeader && <DateHeader date={cell.dateHeader} lang={lang} />}
{cell.slotTime && <SlotTime {...cell.slotTime} />} {cell.slotTime && <SlotTime {...cell.slotTime} />}
{cell.event && <Event {...cell.event} />} {cell.event && <Event {...cell.event} />}
</td>)} </td>)}

View File

@ -1,6 +1,7 @@
import { useMemo } from 'react'; import { useMemo } from 'react';
import { getMidnightTimestamp, isSameDay, sorter } from '../utils.js'; import { sorter, toMidnight } from '../utils.js';
import { langs } from '../Schedule/constants.js'; import { langs } from '../Schedule/constants.js';
import { getTime, isSameDay, toDate } from 'date-fns';
export default function useScheduleTable({ export default function useScheduleTable({
eventTypeId, eventTypeId,
@ -12,13 +13,11 @@ export default function useScheduleTable({
const filteredEvents = events.filter(event => eventTypeId > 0 ? event.event_type_id === eventTypeId : true); const filteredEvents = events.filter(event => eventTypeId > 0 ? event.event_type_id === eventTypeId : true);
const filteredEventIds = filteredEvents.map(event => event.id); const filteredEventIds = filteredEvents.map(event => event.id);
const filteredSlots = slots.sort(sorter('starts_at')).filter(slot => filteredEventIds.includes(slot.event_id)); const filteredSlots = slots.sort(sorter('starts_at')).filter(slot => filteredEventIds.includes(slot.event_id));
const days = Array.from(new Set(filteredSlots.map(slot => const days = Array.from(new Set(filteredSlots.map(slot => getTime(toMidnight(slot))))).map(ts => toDate(ts));
getMidnightTimestamp(slot.starts_at)
))).map(ts => new Date(ts));
const microslots = Array.from(new Set(filteredSlots.flatMap(slot => [ const microslots = Array.from(new Set(filteredSlots.flatMap(slot => [
slot.starts_at.getTime(), getTime(slot.starts_at),
slot.ends_at.getTime(), getTime(slot.ends_at),
]))).map(ts => new Date(ts)).sort(); ]))).sort().map(ts => toDate(ts));
const filteredHallIds = new Set(filteredSlots.map(slot => slot.hall_id)); const filteredHallIds = new Set(filteredSlots.map(slot => slot.hall_id));
const filteredHalls = halls.filter(hall => filteredHallIds.has(hall.id)); const filteredHalls = halls.filter(hall => filteredHallIds.has(hall.id));
const hallSlots = Object.fromEntries(filteredHalls.map(hall => [ const hallSlots = Object.fromEntries(filteredHalls.map(hall => [
@ -48,7 +47,7 @@ export default function useScheduleTable({
return [ return [
...showHeader ? [{ ...showHeader ? [{
id: 'header-'.concat(date.getTime().toString()), id: 'header-'.concat(getTime(date).toString()),
cells: [{ cells: [{
id: 1, id: 1,
attributes: { attributes: {
@ -58,7 +57,7 @@ export default function useScheduleTable({
}], }],
}] : [], }] : [],
...showSlot ? [{ ...showSlot ? [{
id: 'slot-'.concat(date.getTime().toString()), id: 'slot-'.concat(getTime(date).toString()),
cells: [{ cells: [{
id: 1, id: 1,
slotTime: { slotTime: {

View File

@ -1,3 +1,5 @@
import { parseJSON, set } from 'date-fns';
export const sorter = field => (a, b) => { export const sorter = field => (a, b) => {
const fieldA = a[field]; const fieldA = a[field];
const fieldB = b[field]; const fieldB = b[field];
@ -8,7 +10,7 @@ export const sorter = field => (a, b) => {
}; };
export const parseDateFields = (item, dateFields) => Object.fromEntries(Object.entries(item).map(([key, value]) => export const parseDateFields = (item, dateFields) => Object.fromEntries(Object.entries(item).map(([key, value]) =>
[key, dateFields.includes(key) ? new Date(value) : value] [key, dateFields.includes(key) ? parseJSON(value) : value]
)); ));
export function calculateProgress(...elements) { export function calculateProgress(...elements) {
@ -62,8 +64,9 @@ export const normalizeResponse = (items = [], relations = []) =>
}) })
); );
export const getMidnightTimestamp = date => (new Date(date.getTime())).setHours(0, 0, 0, 0); export const toMidnight = date => set(date, {
hours: 0,
export function isSameDay(dateA, dateB) { minutes: 0,
return getMidnightTimestamp(dateA) === getMidnightTimestamp(dateB); seconds: 0,
} milliseconds: 0,
});