Skip to content

Commit

Permalink
fix: date formatting error fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
berenteb committed Feb 10, 2024
1 parent ce59c69 commit f75eed4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
5 changes: 1 addition & 4 deletions components/schedule/elements/presentation-item.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { format } from 'date-fns';
import { useNavigation } from 'expo-router';
import { Image, PressableProps, View } from 'react-native';
import { NativeStackNavigationProp } from 'react-native-screens/native-stack';
Expand All @@ -19,8 +18,6 @@ interface PresentationItemProps extends Omit<PressableProps, 'onPress' | 'onPres
export function PresentationItem({ presentation, className, ...props }: PresentationItemProps) {
const { isFavoritePresentation } = useFavoritePresentations();
const router = useNavigation<NativeStackNavigationProp<{ 'presentation-details': { id: string } }>>();
const startTime = format(new Date(presentation.startTime), 'HH:mm');
const endTime = format(new Date(presentation.endTime), 'HH:mm');
const isPast = isPresentationPast(presentation);
const isFavorite = isFavoritePresentation(presentation.slug);
const onPress = () => {
Expand Down Expand Up @@ -49,7 +46,7 @@ export function PresentationItem({ presentation, className, ...props }: Presenta
</StyledText>
<StyledText className='text-slate-500' numberOfLines={1}>
{' '}
{startTime} - {endTime}
{presentation.startTime} - {presentation.endTime}
</StyledText>
</View>
</View>
Expand Down
8 changes: 2 additions & 6 deletions components/schedule/layouts/presentation-details-page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { format } from 'date-fns';

import { usePresentation } from '../../../hooks/use-presentation';
import { Screen } from '../../base/screen';
import { ScrollContent } from '../../base/scroll-content';
Expand All @@ -17,8 +15,6 @@ interface ScheduleDetailsPageProps {

export function PresentationDetailsPage({ slug }: ScheduleDetailsPageProps) {
const { data, isLoading } = usePresentation(slug);
const startDate = data ? format(new Date(data.startTime), 'HH:mm') : '';
const endDate = data ? format(new Date(data.endTime), 'HH:mm') : '';
return (
<Screen>
<Header>
Expand All @@ -27,14 +23,14 @@ export function PresentationDetailsPage({ slug }: ScheduleDetailsPageProps) {
<>
<Title>{data?.title}</Title>
<Subtitle>
{data?.room}{startDate} - {endDate}
{data.room}{data.startTime} - {data.endTime}
</Subtitle>
</>
)}
</Header>
<ScrollContent>
{isLoading && <SkeletonParagraph />}
{data && <StyledText className='text-xl'>{data?.description}</StyledText>}
{data && <StyledText className='text-xl'>{data.description}</StyledText>}
</ScrollContent>
{data && <FavoriteButton presentation={data} />}
</Screen>
Expand Down
19 changes: 18 additions & 1 deletion services/conference.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isAfter, isBefore } from 'date-fns';
import { format, isAfter, isBefore } from 'date-fns';

import { axiosInstance } from '../config/axios.config';
import { FullConferenceDto } from '../types/conference-api.type';
Expand All @@ -7,6 +7,7 @@ export class ConferenceService {
static async getConferenceData(): Promise<FullConferenceDto> {
const response = await axiosInstance.get<FullConferenceDto>('/api/conference/index');
response.data.presentations = ConferenceService.sortPresentationsByStartDate(response.data);
response.data.presentations = ConferenceService.formatTimestamps(response.data);
return response.data;
}

Expand All @@ -23,4 +24,20 @@ export class ConferenceService {
return 0;
});
}

private static formatTimestamps(conference: FullConferenceDto) {
return conference.presentations.map((presentation) => {
presentation.startTime = ConferenceService.getFormattedTimestamp(presentation.startTime);
presentation.endTime = ConferenceService.getFormattedTimestamp(presentation.endTime);
return presentation;
});
}

private static getFormattedTimestamp(timestamp: string) {
try {
return format(new Date(timestamp), 'HH:mm');
} catch {
return 'n/a';
}
}
}
4 changes: 2 additions & 2 deletions types/conference-api.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export type PresentationDto = {
title: string;
room: string;
language: string;
startTime: Date;
endTime: Date;
startTime: string;
endTime: string;
description: string;
questionsUrl: string;
presenter: PresenterDto;
Expand Down

0 comments on commit f75eed4

Please sign in to comment.