Open silver-or opened 1 month ago
// .env 파일 DATE_FORMAT=ko-KR
// utils/dateFormatter.ts export function formatDate(date: Date): string { return date.toLocaleDateString(process.env.DATE_FORMAT, { year: 'numeric', month: 'numeric', day: 'numeric', }).replace(/. /g, '.').replace(/.$/, ''); }
서버 사이드 렌더링 고려: Next.js를 사용 중이라면, getServerSideProps나 getStaticProps에서 날짜를 미리 포맷팅하여 전달할 수 있습니다.
typescriptCopyexport async function getServerSideProps() { const currentDate = formatDate(new Date()); return { props: { currentDate } }; }
클라이언트 사이드에서 날짜 재계산: 필요하다면, 클라이언트 사이드에서 useEffect를 사용하여 날짜를 다시 계산할 수 있습니다.
typescriptCopyuseEffect(() => { setFormattedDate(formatDate(new Date())); }, []);
export function formatDate(date: Date): string { return date.toLocaleDateString('ko-KR', { year: 'numeric', month: 'numeric', day: 'numeric', }).replace(/. /g, '.').replace(/.$/, ''); }
서버 컴포넌트에서 사용: 서버 컴포넌트에서 날짜를 표시할 때 이 함수를 사용합니다.
typescriptCopyimport { formatDate } from '../utils/dateFormatter';
// ...
const formattedDate = formatDate(new Date());
클라이언트 컴포넌트에서 사용: 클라이언트 컴포넌트에서도 동일한 함수를 사용합니다.
typescriptCopyimport { formatDate } from '../utils/dateFormatter';
// ...
const formattedDate = formatDate(new Date());
캘린더
BottomContainer.tsx
해당 코드
getImageUrl.tsx
export async function getImageUrl(imageKey: string): Promise {
try {
const response = await axios.get(
/api/calendar?key=${encodeURIComponent(imageKey)}
);}
import axios from 'axios';
interface ProcessImageParams { imageUrl: string; userId: number; babyId: number; }
export interface CalendarResult { year: string | null; month: string; events: Array<{ date: string; activities: Array<{ name: string; time: string | null; infomation: string; }>; }>; etc: string | null; user_id: string; baby_id: string; }
export async function processImage({ imageUrl, userId, babyId }: ProcessImageParams): Promise {
try {
const response = await axios.post('http://localhost:8000/process_image', {
user_id: userId.toString(),
baby_id: babyId.toString(),
image_path: imageUrl,
});
}
catch (error) { console.error('이미지 업로드 또는 처리 중 오류:', error); localStorage.setItem('calendarError', '이미지 처리 중 오류가 발생했습니다. 다시 시도해 주세요.'); router.push('/calendarResult'); }
'use client'; import React, { useEffect, useState } from 'react'; import { CalendarResult } from '@/utils/processImage'; import EventCalendar from '@/components/EventCalendar';
const CalendarResultPage: React.FC = () => { const [calendarData, setCalendarData] = useState<CalendarResult | null>(null); const [error, setError] = useState<string | null>(null);
};
export default CalendarResultPage;
import React from 'react'; import Link from 'next/link'; import { CalendarResult } from '@/utils/processImage';
interface Activity { name: string; time: string | null; infomation: string; }
interface Event { date: string; activities: Activity[]; }
interface EventCalendarProps { result: CalendarResult; }
const EventCalendar: React.FC = ({ result }) => {
if (!result || !result.events) {
return 표시할 이벤트가 없습니다 ;
}
};
export default EventCalendar;