Skip to content

Commit

Permalink
rename : 기분 타입 이름 변경 (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmlim0070 authored Nov 1, 2024
1 parent 246902d commit ebd8a9d
Show file tree
Hide file tree
Showing 20 changed files with 155 additions and 114 deletions.
12 changes: 6 additions & 6 deletions src/entities/chart/lib/isDailyEmotion.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
DailyEmotionType,
WeeklyEmotionSummaryType
} from '@/shared/model/moodTypes';
DailyConditionType,
WeeklyConditionSummaryType
} from '@/shared/model/conditionTypes';

const isDailyEmotion = (
data: DailyEmotionType[] | WeeklyEmotionSummaryType[]
): data is DailyEmotionType[] => {
return (data as DailyEmotionType[])[0]?.day !== undefined;
data: DailyConditionType[] | WeeklyConditionSummaryType[]
): data is DailyConditionType[] => {
return (data as DailyConditionType[])[0]?.day !== undefined;
};

export default isDailyEmotion;
6 changes: 3 additions & 3 deletions src/entities/chart/ui/Chart.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Meta, StoryObj } from '@storybook/react';
import Chart from './Chart';
import {
DailyEmotionType,
} from '@/shared/model/moodTypes';
DailyConditionType,
} from '@/shared/model/conditionTypes';

const meta: Meta<typeof Chart> = {
component: Chart,
Expand All @@ -14,7 +14,7 @@ export default meta;

type Story = StoryObj<typeof Chart>;

const dailyEmotionData : DailyEmotionType[] = [
const dailyEmotionData : DailyConditionType[] = [
{ day: 'Mon' as const, mood: null },
{ day: 'Tue' as const, mood: '나쁨' },
{ day: 'Wed' as const, mood: '나쁨' },
Expand Down
8 changes: 4 additions & 4 deletions src/entities/chart/ui/Chart.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import {
DailyEmotionType,
WeeklyEmotionSummaryType
} from '@/shared/model/moodTypes';
DailyConditionType,
WeeklyConditionSummaryType
} from '@/shared/model/conditionTypes';

import {
LineChart,
Expand All @@ -18,7 +18,7 @@ import isDailyEmotion from '../lib/isDailyEmotion';
const emotions = ['매우 나쁨', '나쁨', '보통', '좋음', '매우 좋음'];

interface ChartProps {
data: DailyEmotionType[] | WeeklyEmotionSummaryType[];
data: DailyConditionType[] | WeeklyConditionSummaryType[];
}

const Chart = ({ data }: ChartProps) => {
Expand Down
35 changes: 35 additions & 0 deletions src/entities/music/api/fetchMusicRecommendation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { createGptRequestQuery } from '../lib/createGptRequestQuery';
import { gptAnswerType, MoodDataType } from '../model/type';
import { fetchGptRecommend } from './fetchGptRecommend';

/**
* 음악 아이텝을 반환합니다.
* @param emotionData d
* @param param1
* @returns
*/
export const fetchMusicRecommendation = async (
emotionData: MoodDataType | null,
{
onSuccess,
onError,
onValidationError
}: {
onSuccess: (data: gptAnswerType) => void;
onError: () => void;
onValidationError: () => void;
}
) => {
if (!emotionData) {
onValidationError();
return;
}
try {
const requestQuery = createGptRequestQuery(emotionData);
const recommendedMusic = await fetchGptRecommend(requestQuery);
onSuccess(recommendedMusic);
} catch (error) {
onError();
throw error;
}
};
7 changes: 6 additions & 1 deletion src/entities/music/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export { fetchGptRecommend } from './api/fetchGptRecommend';
export { youtubeSearch } from './api/fetchMusicList';
export { spotifySearch } from './api/fetchMusicList';
export { fetchMusicRecommendation } from './api/fetchMusicRecommendation';
export { useMusicSearch } from './hooks/useMusicSearch';
export { createGptRequestQuery } from './lib/createGptRequestQuery';
export { MusicCard } from './ui/MusicCard';
export { EmptyMusicCard } from './ui/EmptyMusicCard';
export { useMusicSearch } from './hooks/useMusicSearch';
28 changes: 28 additions & 0 deletions src/entities/music/lib/createGptRequestQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { gptQueryParamsType, MoodDataType } from '../model/type';

// 테스트 다이러리
const INITIAL_DIARY = {
title: '우울해',
content: '너무 우울해서 빵샀어'
};

/**
* 감정데이터, 일기데이터를 조합해 쿼리 데이터를 생성
* @param emotionData
* @returns
*/
export const createGptRequestQuery = (
emotionData: MoodDataType
): gptQueryParamsType => {
return {
title: INITIAL_DIARY.title,
content: INITIAL_DIARY.content,
...(emotionData?.mood && { mood: emotionData.mood }),
...(emotionData?.emotion && { emotion: emotionData.emotion }),
...(emotionData?.subEmotion && {
subemotion: emotionData.subEmotion.filter(
(emotion): emotion is string => emotion !== null
)
})
};
};
7 changes: 7 additions & 0 deletions src/entities/music/model/type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ConditionType } from '@/shared/model/conditionTypes';

export interface MusicItem {
youtubeId: string;
thumbnailUrl: string;
Expand Down Expand Up @@ -48,3 +50,8 @@ export interface gptQueryParamsType {
}

export type gptAnswerType = string[];
export interface MoodDataType {
mood: ConditionType;
emotion: string | null;
subEmotion: (string | null)[];
}
17 changes: 0 additions & 17 deletions src/entities/music/model/useMusicStore.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/features/chart/lib/isWeekly.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
WeeklyDataType,
MonthlyDataType
} from '../../../shared/model/moodTypes';
} from '../../../shared/model/conditionTypes';

const isWeekly = (
data: WeeklyDataType | MonthlyDataType
Expand Down
2 changes: 0 additions & 2 deletions src/features/diary-write/musicList/ui/MusicCardList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { useEffect, useState } from 'react';
import { Container, HiddenYoutubeContainer } from './MusicCardList.styled';
import { MusicItem, MusicCardListProps } from '../model/type';
import { EmptyMusicCard, MusicCard } from '../../../../entities/music';
import useMusicStore from '@/entities/music/model/useMusicStore';

export const MusicCardList = ({
responseMusicList,
Expand Down Expand Up @@ -38,7 +37,6 @@ export const MusicCardList = ({
}
};

// TODO - iframe 유튜브 api 모듈로 변경
return (
<Container>
<HiddenYoutubeContainer>
Expand Down
4 changes: 2 additions & 2 deletions src/pages/DiaryWritePage/model/type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EmotionType } from '@/shared/model/moodTypes';
import { ConditionType } from '@/shared/model/conditionTypes';

export interface MoodDataType {
mood: EmotionType;
mood: ConditionType;
emotion: string | null;
subEmotion: (string | null)[];
}
90 changes: 31 additions & 59 deletions src/pages/DiaryWritePage/ui/DiaryWritePage.tsx
Original file line number Diff line number Diff line change
@@ -1,87 +1,59 @@
import React, { useEffect, useState } from 'react';
import { SelectMusicContainer } from '@/widgets/select-music/ui/SelectMusicContainer';
import Header from '@/widgets/header/ui/Header';
import { Container, Section } from './DiaryWritePage.styled';
import React, { useState } from 'react';
import { fetchMusicRecommendation } from '@/entities/music';
import { gptAnswerType, MusicItem } from '@/entities/music/model/type';
import { useToastStore } from '@/features/Toast/hooks/useToastStore';
import { SelectMusicContainer } from '@/widgets/select-music';
import { SelectEmotionContainer } from '@/widgets/select-emotion';
import { WriteDiaryContainer } from '@/widgets/write-diary';
import { fetchGptRecommend } from '@/entities/music/api/fetchGptRecommend';
import {
gptAnswerType,
gptQueryParamsType,
MusicItem
} from '@/entities/music/model/type';
import { EmotionType } from '@/shared/model/moodTypes';
import { Emotions } from '@/shared/model/EmotionEnum';
import { Container, Section } from './DiaryWritePage.styled';
import { MoodDataType } from '../model/type';

// TODO - 리팩토링 (로직 분리)
export const DiaryWritePage = () => {
// 테스트 다이러리
const diary = {
title: '우울해',
content: '너무 우울해서 빵샀어'
};

const { addToast } = useToastStore();
// const [diaryData, setDiaryData] = useState();
const [emotionData, setEmotionData] = useState<MoodDataType | null>(null);
const [musicData, setMusicData] = useState<MusicItem | null>(null);

const [gptRecommendMusicList, setGptRecommendMusicList] =
const [userEmotionState, setUserEmotionState] =
useState<MoodDataType | null>(null);
const [selectedMusic, setSelectedMusic] = useState<MusicItem | null>(null);
const [recommendedMusicList, setRecommendedMusicList] =
useState<gptAnswerType>([]);

// // 일기 데이터가 넘어오면 셋팅
// const handleDiarySubmit = (diaryData) => {
// setDiaryData(diaryData);
// };

const handleMoodSelect = (moodState: MoodDataType) => {
console.log('감정 데이터 셋팅 : ', moodState);
setEmotionData(moodState);
};

const handleMusicSelect = (music: MusicItem | null) => {
console.log('음악 데이터 셋팅 : ', music);
setMusicData(music);
const handleEmotionSubmit = (submittedEmotion: MoodDataType) => {
console.log('사용자 감정 데이터 저장:', submittedEmotion);
setUserEmotionState(submittedEmotion);
};

// TODO - diary 파라미터 넣어야함
const createGptQuery = (mood: MoodDataType) => {
const gptQuery: gptQueryParamsType = {
title: '우울해',
content: '너무 우울해서 빵샀어',
...(mood?.mood && { mood: mood.mood }),
...(mood?.emotion && { emotion: mood.emotion }),
...(mood?.subEmotion && {
subemotion: mood.subEmotion.filter(
(item): item is string => item !== null
)
})
};
return gptQuery;
const handleMusicSelection = (selectedMusicItem: MusicItem | null) => {
console.log('선택된 음악 데이터 저장:', selectedMusicItem);
setSelectedMusic(selectedMusicItem);
};

const testFunction = async (mood: MoodDataType | null) => {
console.log('테스트 함수 실행됨');
if (mood === null) {
console.log('감정 선택을 먼저 완료해주세요');
} else {
const gptQuery = createGptQuery(mood);
const recommendations = await fetchGptRecommend(gptQuery);
setGptRecommendMusicList(recommendations);
}
const handleFetchRecommendations = async (
emotionData: MoodDataType | null
) => {
await fetchMusicRecommendation(emotionData, {
onSuccess: setRecommendedMusicList,
onError: () => addToast('음악 추천 요청에 실패했습니다.', 'error'),
onValidationError: () =>
addToast('먼저 감정을 선택해주세요!', 'warning')
});
};

return (
<Container>
<Section>
{/* <WriteDiaryContainer onDiarySubmit={handleDiarySubmit} /> */}
{/* <WriteDiaryContainer onSubmit={handleDiarySubmit} /> */}
<SelectEmotionContainer
onMoodSelect={handleMoodSelect}
onNext={testFunction}
onMoodSelect={handleEmotionSubmit}
onNext={handleFetchRecommendations}
/>
<SelectMusicContainer
onMusicSelect={handleMusicSelect}
gptRecommendMusicList={gptRecommendMusicList}
onMusicSelect={handleMusicSelection}
gptRecommendMusicList={recommendedMusicList}
/>
</Section>
</Container>
Expand Down
2 changes: 1 addition & 1 deletion src/shared/api/mood.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WeeklyDataType, MonthlyDataType } from '../model/moodTypes';
import { WeeklyDataType, MonthlyDataType } from '../model/conditionTypes';
import { moodQueryParamType } from '../model/moodQueryParamType';
import defaultApi from '@/shared/api/api';

Expand Down
2 changes: 1 addition & 1 deletion src/shared/hooks/useGetMood.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WeeklyDataType, MonthlyDataType } from '../model/moodTypes';
import { WeeklyDataType, MonthlyDataType } from '../model/conditionTypes';
import { useQuery } from '@tanstack/react-query';
import { moodQueryParamType } from '../model/moodQueryParamType';
import { getMoodApi } from '../api/mood';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
export type EmotionType =
export type ConditionType =
| '좋음'
| '나쁨'
| '보통'
| '매우 좋음'
| '매우 나쁨'
| null;

export interface DailyEmotionType {
export interface DailyConditionType {
day: 'Mon' | 'Tue' | 'Wed' | 'Thu' | 'Fri' | 'Sat' | 'Sun';
mood: EmotionType;
mood: ConditionType;
}

export interface WeeklyDataType {
period: string;
mostFrequentEmotion: EmotionType;
mostFrequentEmotion: ConditionType;
frequency: number | null;
allEmotions: DailyEmotionType[];
allEmotions: DailyConditionType[];
}

export interface WeeklyEmotionSummaryType {
export interface WeeklyConditionSummaryType {
week: number;
mostFrequentEmotion: EmotionType;
mostFrequentEmotion: ConditionType;
}

export interface MonthlyDataType {
period: string;
weeklyResults: WeeklyEmotionSummaryType[];
weeklyResults: WeeklyConditionSummaryType[];
frequency: number | null;
mostFrequentEmotion: EmotionType;
mostFrequentEmotion: ConditionType;
}
4 changes: 2 additions & 2 deletions src/widgets/select-emotion/model/type.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { MoodDataType } from '@/pages/DiaryWritePage/model/type';
import { EmotionType } from '@/shared/model/moodTypes';
import { ConditionType } from '@/shared/model/conditionTypes';

export interface SelectEmotionContainerProps {
onMoodSelect: (mood: MoodDataType) => void;
onNext?: (mood: MoodDataType) => void;
}

export interface MoodState {
mood: EmotionType;
mood: ConditionType;
emotion: string | null;
subEmotion: (string | null)[];
}
Expand Down
Loading

0 comments on commit ebd8a9d

Please sign in to comment.