Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
DovileMel committed Jun 5, 2024
2 parents e455cdd + 05368d2 commit e124fad
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/utils/functions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { format, isToday, parseISO, endOfDay, startOfDay } from 'date-fns';
import { format, isToday, parseISO, endOfDay, startOfDay, isTomorrow, isYesterday } from 'date-fns';
import { toZonedTime } from 'date-fns-tz';
import { toast } from 'react-toastify';
import { routes } from './routes';
Expand Down Expand Up @@ -36,23 +36,32 @@ export const formatDateAndTime = (date?: Date | string) =>

export const formatTime = (date?: Date | string) => (date ? format(new Date(date), 'HH:mm') : '');

export const getTimeLabel = (event: Event) => {
const { startAt, endAt, isFullDay } = event;
function getDateTranslate(date: Date) {
if (isToday(date)) return 'Šiandien';
else if (isTomorrow(date)) return 'Rytoj';
else if (isYesterday(date)) return 'Vakar';
}

const startAtParsed = parseISO(startAt);
export const getTimeLabel = ({ startAt, endAt, isFullDay }: Event) => {
function getFormatted(value?: string) {
if (!value) return;

if (endAt) {
const endAtParsed = parseISO(endAt);
return `${formatDateAndTime(startAtParsed)} - ${formatDateAndTime(endAtParsed)}`;
const parsedValue = parseISO(value);

const prefix = getDateTranslate(parsedValue);
if (isFullDay) return prefix ? prefix : formatDate(parsedValue);
else if (prefix) return `${prefix} ${formatTime(parsedValue)}`;
return formatDateAndTime(parsedValue);
}

const todayLabel = isToday(startAtParsed) ? `Šiandien` : '';
const startAtFormatted = getFormatted(startAt);
const endAtFormatted = getFormatted(endAt);

if (isFullDay) {
return todayLabel || formatDate(startAt);
if (endAtFormatted) {
return `${startAtFormatted} - ${endAtFormatted}`;
}

return todayLabel ? `${todayLabel} ${formatTime(startAt)}` : formatDateAndTime(startAt);
return startAtFormatted;
};

export const isEmpty = (value: any) => {
Expand Down

0 comments on commit e124fad

Please sign in to comment.