Skip to content

Commit cebd621

Browse files
authored
Merge pull request #251 from sang-gyeong/web
[FE] Feat : useSWR mutate λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ 데이터 변경사항 μ¦‰μ‹œ λ°˜μ˜ν•˜κΈ°
2 parents bd11a0c + 465bbb5 commit cebd621

File tree

6 files changed

+87
-38
lines changed

6 files changed

+87
-38
lines changed

β€Žfrontend/src/components/ArtistList/index.tsxβ€Ž

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
11
import React from 'react';
22
import styled from '@styles/themed-components';
33
import ArtistCard from '@components/Common/Card/ArtistCard';
4+
import { mutate } from 'swr';
5+
import api from '@api/index';
6+
import { useAuthDispatch } from '@context/AuthContext';
47

58
const ArtistList = ({ artistList }) => {
9+
const dispatch = useAuthDispatch();
10+
11+
const fetchData = async id => {
12+
await api.delete(`library/artists/${id}`);
13+
dispatch({ type: 'DELETE_ARTIST', artistId: id });
14+
const updatedArtists = await artistList.filter(artist => artist.id !== id);
15+
mutate(
16+
'/library/artists',
17+
data => {
18+
return { ...data, data: updatedArtists };
19+
},
20+
false,
21+
);
22+
};
23+
24+
const deleteArtist = (e, id) => {
25+
fetchData(id);
26+
};
27+
628
return (
729
<ListContainer>
830
{artistList
931
? artistList?.map(artist => (
10-
<ArtistCard key={artist.id} artistMetaData={artist} type="myArtist" />
32+
<ArtistCard
33+
key={artist.id}
34+
artistMetaData={artist}
35+
deleteArtist={deleteArtist}
36+
type="myArtist"
37+
/>
1138
))
1239
: null}
1340
</ListContainer>

β€Žfrontend/src/components/Common/Card/ArtistCard/index.tsxβ€Ž

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import CircleImage from '@components/Common/CircleImage';
44
import CircleHeartButton from '@components/Common/Button/CircleHeartButton';
55
import A from '@components/Common/A';
66
import api from '@api/index';
7-
import { mutate } from 'swr';
87
import logEventHandler from '@utils/logEventHandler';
98
import ClickEventWrapper from '@components/EventWrapper/ClickEventWrapper';
109
import { useAuthDispatch } from '@context/AuthContext';
1110

1211
interface IArtistMetaProps {
1312
artistMetaData: ArtistMeta;
1413
type?: string | null;
14+
deleteArtist?: any;
1515
}
1616

1717
interface ArtistMeta {
@@ -21,18 +21,8 @@ interface ArtistMeta {
2121
imgUrl: string;
2222
}
2323

24-
const ArtistCard = ({ artistMetaData: artist, type }: IArtistMetaProps) => {
24+
const ArtistCard = ({ artistMetaData: artist, type, deleteArtist }: IArtistMetaProps) => {
2525
const target = 'ArtistCard';
26-
const dispatch = useAuthDispatch();
27-
28-
const deleteArtist = async (e, id) => {
29-
await api.delete(`library/artists/${id}`);
30-
e.target.closest('.artist-card').style.opacity = '0';
31-
e.target.closest('.artist-card').style.transform = 'translate(0px, -35px)';
32-
dispatch({ type: 'DELETE_ARTIST', artistId: id });
33-
mutate(`${process.env.NEXT_PUBLIC_API_BASE_URL}/library/artists`);
34-
};
35-
3626
return (
3727
<Container className="artist-card">
3828
<ImageContainer>

β€Žfrontend/src/components/Common/Dropdown/ArtistDropdown.tsxβ€Ž

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react';
22
import styled from '@styles/themed-components';
33
import { Dropdown } from 'semantic-ui-react';
4-
import api from '@api/index';
54
import logEventHandler from '@utils/logEventHandler';
65
import ClickEventWrapper from '@components/EventWrapper/ClickEventWrapper';
76
import * as T from '../../../constants/dropdownText';
@@ -12,21 +11,13 @@ interface ILogData {
1211
parameters: any;
1312
}
1413

15-
const ArtistDropdown = id => {
16-
const postLog = logData => {
17-
api.post('/log', logData);
18-
};
19-
14+
function ArtistDropdown({ id, addArtistEvent }) {
2015
const libraryLogData: ILogData = {
2116
eventTime: new Date(),
2217
eventName: 'library_event',
2318
parameters: { action: 'add', type: 'artist', id },
2419
};
2520

26-
const addLibraryEvent = () => {
27-
api.post('/library/artists', { artistId: id }).then(() => postLog(libraryLogData));
28-
};
29-
3021
return (
3122
<Wrapper>
3223
<Dropdown
@@ -39,7 +30,7 @@ const ArtistDropdown = id => {
3930
<Dropdown.Item
4031
style={dropdownItemStyle}
4132
text={T.LIKE}
42-
onClick={logEventHandler(addLibraryEvent, libraryLogData)}
33+
onClick={logEventHandler(addArtistEvent, libraryLogData)}
4334
/>
4435
</ClickEventWrapper>
4536
<ClickEventWrapper target="ShareBtn/artist" id={id}>
@@ -49,7 +40,7 @@ const ArtistDropdown = id => {
4940
</Dropdown>
5041
</Wrapper>
5142
);
52-
};
43+
}
5344

5445
const authDropdownStyle = {
5546
width: '100%',

β€Žfrontend/src/components/Common/PlayTrackItem/index.tsxβ€Ž

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useRouter } from 'next/router';
33
import styled from 'styled-components';
44
import { IoMdHeartEmpty, IoMdHeart } from 'react-icons/io';
55
import logEventHandler from '@utils/logEventHandler';
6-
6+
import { mutate } from 'swr';
77
import api from '@api/index';
88
import { useAuthState, useAuthDispatch } from '@context/AuthContext';
99
import A from '@components/Common/A';
@@ -15,12 +15,27 @@ function PlayTrackItem({ type, trackData: track }) {
1515
const { trackList } = state;
1616
const target = 'PlayTrackItem';
1717

18-
const addTrackEvent = () => {
19-
api.post(`library/tracks`, { trackId: track.id });
18+
const addTrackEvent = async () => {
19+
await api.post(`library/tracks`, { trackId: track.id });
20+
mutate(
21+
'/library/tracks',
22+
data => {
23+
return { ...data, data: [...data.data, track].sort((a, b) => a.id - b.id) };
24+
},
25+
true,
26+
);
2027
dispatch({ type: 'ADD_TRACK', trackId: track.id });
2128
};
22-
const deleteTrackEvent = () => {
23-
api.delete(`library/tracks/${track.id}`);
29+
const deleteTrackEvent = async () => {
30+
await api.delete(`library/tracks/${track.id}`);
31+
mutate(
32+
'/library/tracks',
33+
data => {
34+
const updatedTracks = data.data.filter(item => item.id !== track.id);
35+
return { ...data, data: [...updatedTracks] };
36+
},
37+
false,
38+
);
2439
dispatch({ type: 'DELETE_TRACK', trackId: track.id });
2540
};
2641

β€Žfrontend/src/pages/Detail/Artist/index.tsxβ€Ž

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import RelatedArtist from '@components/Common/SampleSection/RelatedArtist';
88
import TrackList from '@components/TrackList';
99
import logEventHandler from '@utils/logEventHandler';
1010
import api from '@api/index';
11+
import { mutate } from 'swr';
1112

1213
import getMultipleNames from '@utils/getMultipleNames';
1314
import AlbumList from '@components/AlbumList';
@@ -23,14 +24,29 @@ function ArtistDetail({ artistInfo: artist }) {
2324
const { artistList } = state;
2425
const [artistState, setArtistState] = useState(artistList);
2526

26-
const deleteArtistEvent = () => {
27-
api.delete(`library/artists/${artist.id}`);
27+
const deleteArtistEvent = async () => {
28+
await api.delete(`library/artists/${artist.id}`);
2829
dispatch({ type: 'DELETE_ARTIST', artistId: artist.id });
30+
mutate(
31+
'/library/artists',
32+
data => {
33+
const updatedArtists = data.data.filter(item => item.id !== artist.id);
34+
return { ...data, data: [...updatedArtists] };
35+
},
36+
true,
37+
);
2938
};
3039

31-
const addArtistEvent = () => {
32-
api.post(`library/artists`, { artistId: artist.id });
40+
const addArtistEvent = async () => {
41+
await api.post(`library/artists`, { artistId: artist.id });
3342
dispatch({ type: 'ADD_ARTIST', artistId: artist.id });
43+
mutate(
44+
'/library/artists',
45+
data => {
46+
return { ...data, data: [...data.data, artist] };
47+
},
48+
true,
49+
);
3450
};
3551

3652
useEffect(() => {
@@ -48,7 +64,9 @@ function ArtistDetail({ artistInfo: artist }) {
4864
<TopContainer>
4965
<MainTitle>{artist.name}</MainTitle>
5066
<SubTitle>
51-
{artist.debut.replace(/-/g, '.').slice(0, 10)} 데뷔 Β·{' '}
67+
{artist.debut.replace(/-/g, '.').slice(0, 10)}
68+
{' '}
69+
데뷔 Β·{' '}
5270
{getMultipleNames(artist.genres)}
5371
</SubTitle>
5472
</TopContainer>
@@ -71,7 +89,7 @@ function ArtistDetail({ artistInfo: artist }) {
7189
</ButtonContainer>
7290
<ButtonContainer>
7391
<HiOutlineDotsHorizontal size={24} color="575757" />
74-
<ArtistDropdown id={artist.id} />
92+
<ArtistDropdown id={artist.id} addArtistEvent={addArtistEvent} />
7593
</ButtonContainer>
7694
</ButtonWrapper>
7795
</BottomContainer>

β€Žfrontend/src/utils/logEventHandler.tsxβ€Ž

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
import { mutate } from 'swr';
12
import api from '../api';
23

3-
const postLog = logData => {
4+
const postLog = async logData => {
45
const timeStampedLog = { ...logData, eventTime: new Date() };
5-
api.post('/log', timeStampedLog);
6+
await api.post('/log', timeStampedLog);
7+
mutate(
8+
'/log',
9+
data => {
10+
return { ...data, data: [...data.data, logData] };
11+
},
12+
true,
13+
);
614
};
715

816
const logEventHandler = (handler, logData) => {

0 commit comments

Comments
Β (0)