Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/apis/emotion/emotion.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export const useCreateEmotionLog = (userId: number | null) => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (data: { emotion: Emotion }) => createEmotionLogToday(data),
onSuccess: () => {
onSuccess: async () => {
if (userId !== null) {
queryClient.invalidateQueries({ queryKey: ['emotion'] });
await queryClient.invalidateQueries({ queryKey: ['emotion'] });
}
},
});
Expand Down
13 changes: 10 additions & 3 deletions src/app/(after-login)/epigrams/_components/TodayEmotion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ export default function TodayEmotion() {
const showTodayMood = userId && !isLoading && !emotion;

const handleEmotionClick = (emotion: Emotion) => {
if (isPending) return;

createEmotion({ emotion });
if (isPending) return Promise.resolve();
return new Promise<void>((resolve, reject) => {
createEmotion(
{ emotion },
{
onSuccess: () => resolve(),
onError: () => reject(),
},
);
});
};

return (
Expand Down
13 changes: 10 additions & 3 deletions src/app/(after-login)/mypage/_components/MyEmotions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ export default function MyEmotions() {
const { data: moodData = {}, currentMonth, setCurrentMonth } = useEmotionLogsMonthly(userId);

const handleEmotionClick = (emotion: Emotion) => {
if (isPending) return;

createEmotion({ emotion });
if (isPending) return Promise.resolve();
return new Promise<void>((resolve, reject) => {
createEmotion(
{ emotion },
{
onSuccess: () => resolve(),
onError: () => reject(),
},
);
});
};

return (
Expand Down
52 changes: 38 additions & 14 deletions src/components/TodayMood.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
'use client';

import { useState } from 'react';
import { format } from 'date-fns';
import { EmotionLog } from '@/apis/emotion/emotion.type';
import { EMOTION, Emotion } from '@/types/common';
import { cn } from '@/utils/helper';
import EmojiButton from './EmojiButton';
import Spinner from './Spinner';

interface TodayMoodProps {
label?: string;
containerClassName?: string;
labelClassName?: string;
showDate?: boolean;
emotion?: EmotionLog | null;
onEmotionClick: (emotion: Emotion) => void;
onEmotionClick: (emotion: Emotion) => Promise<void>;
}

export default function TodayMood({
Expand All @@ -23,9 +25,20 @@ export default function TodayMood({
emotion,
onEmotionClick,
}: TodayMoodProps) {
const [isLoading, setIsLoading] = useState(false);

const selectedEmotion = emotion?.emotion || null;
const today = format(new Date(), 'yyyy.MM.dd');

const handleEmotionClick = async (emotion: Emotion) => {
setIsLoading(true);
try {
await onEmotionClick(emotion);
} finally {
setIsLoading(false);
}
};

return (
<>
<div className='flex justify-between'>
Expand All @@ -34,20 +47,31 @@ export default function TodayMood({
</label>
{showDate && <span className='text-lg text-blue-400 lg:text-xl'>{today}</span>}
</div>
<div
className={cn(
'mt-[24px] flex h-[84px] w-full justify-center gap-4 md:h-[96px] lg:mt-[48px] lg:h-[136px] lg:gap-6',
containerClassName,
<div className='relative'>
<div
className={cn(
'mt-[24px] flex h-[84px] w-full justify-center gap-4 md:h-[96px] lg:mt-[48px] lg:h-[136px] lg:gap-6',
containerClassName,
)}
>
{Object.values(EMOTION).map((emotion) => (
<EmojiButton
key={emotion}
name={emotion}
onClick={() => handleEmotionClick(emotion)}
selected={selectedEmotion === emotion}
/>
))}
</div>

{isLoading && (
<div className='absolute inset-0 z-10 flex items-center justify-center rounded-[12px] backdrop-blur-sm'>
<div className='flex flex-col items-center justify-center gap-4 p-4 text-center text-blue-400'>
<Spinner />
감정을 저장하고 있습니다.
</div>
</div>
)}
>
{Object.values(EMOTION).map((emotion) => (
<EmojiButton
key={emotion}
name={emotion}
onClick={() => onEmotionClick(emotion)}
selected={selectedEmotion === emotion}
/>
))}
</div>
</>
);
Expand Down