-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: item select count 0으로 나오는 버그 수정 * feat: 아이템 선택 api 추가 * feat: 게임 만들때 text 빈값일때 에러처리
- Loading branch information
Showing
10 changed files
with
204 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { axiosInstance } from '.'; | ||
import END_POINTS from '../constants/api'; | ||
|
||
export default async function selectItem(gameId, itemId) { | ||
const { data } = await axiosInstance.post(END_POINTS.SELECT_ITEM(gameId, itemId)); | ||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,46 @@ | ||
import useSelectItem from '../../hooks/queries/useSelectItem'; | ||
import calculatePercent from '../../utils/calculatePercent'; | ||
import * as S from './Choice.styled'; | ||
|
||
export default function Choice(props) { | ||
const { game } = props; | ||
console.log(game?.firstItem.item_id); | ||
const { mutate: selectItemMutate } = useSelectItem(game?.firstItem.game_id); | ||
const isColored = (firstItemSelected, secondItemSelected) => { | ||
return !firstItemSelected && !secondItemSelected ? true : firstItemSelected ? true : false; | ||
}; | ||
|
||
return ( | ||
<S.ItemContainer> | ||
<S.ItemList>{game?.firstItem.item_text}</S.ItemList> | ||
<S.ItemList2>{game?.secondItem.item_text}</S.ItemList2> | ||
<S.ItemList | ||
onClick={() => selectItemMutate(game?.firstItem?.item_id)} | ||
iscolored={isColored(game?.firstItem.isSelected, game?.secondItem.isSelected)} | ||
> | ||
<S.itemText>{game?.firstItem.item_text}</S.itemText> | ||
<S.percentBox | ||
first | ||
percent={calculatePercent( | ||
game?.firstItem.selected_count, | ||
game?.firstItem.selected_count + game?.secondItem.selected_count, | ||
)} | ||
isselected={game?.firstItem.isSelected} | ||
isshow={game?.firstItem.isSelected || game?.secondItem.isSelected} | ||
/> | ||
</S.ItemList> | ||
<S.ItemList2 | ||
onClick={() => selectItemMutate(game?.secondItem?.item_id)} | ||
iscolored={isColored(game?.secondItem.isSelected, game?.firstItem.isSelected)} | ||
> | ||
<S.itemText>{game?.secondItem.item_text}</S.itemText> | ||
<S.percentBox | ||
percent={calculatePercent( | ||
game?.secondItem.selected_count, | ||
game?.firstItem.selected_count + game?.secondItem.selected_count, | ||
)} | ||
isselected={game?.secondItem.isSelected} | ||
isshow={game?.firstItem.isSelected || game?.secondItem.isSelected} | ||
/> | ||
</S.ItemList2> | ||
</S.ItemContainer> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
import selectItem from '../../apis/selectItem'; | ||
import QUERY_KEYS from '../../constants/queryKeys'; | ||
import { toast } from 'react-toastify'; | ||
|
||
export default function useSelectItem(gameId) { | ||
const queryClient = useQueryClient(); | ||
|
||
return useMutation({ | ||
mutationFn: async (itemId) => { | ||
const response = await selectItem(gameId, itemId); | ||
return response; | ||
}, | ||
onMutate: async (itemId) => { | ||
await queryClient.cancelQueries([QUERY_KEYS.GAMES_ITEMS]); | ||
|
||
const previousData = queryClient.getQueryData([QUERY_KEYS.GAMES_ITEMS]); | ||
|
||
queryClient.setQueryData([QUERY_KEYS.GAMES_ITEMS], (oldData) => { | ||
if (!oldData) return oldData; | ||
|
||
const firstItemSelected = oldData.firstItem.item_id === itemId; | ||
const secondItemSelected = oldData.secondItem.item_id === itemId; | ||
|
||
// 이미 선택된 아이템을 다시 눌러서 선택을 취소하는 경우 | ||
if (firstItemSelected && oldData.firstItem.isSelected) { | ||
return { | ||
...oldData, | ||
firstItem: { | ||
...oldData.firstItem, | ||
isSelected: false, | ||
selected_count: oldData.firstItem.selected_count - 1, | ||
}, | ||
}; | ||
} | ||
|
||
if (secondItemSelected && oldData.secondItem.isSelected) { | ||
return { | ||
...oldData, | ||
secondItem: { | ||
...oldData.secondItem, | ||
isSelected: false, | ||
selected_count: oldData.secondItem.selected_count - 1, | ||
}, | ||
}; | ||
} | ||
|
||
// 반대쪽 아이템을 선택했을 때 기존 선택한 아이템의 선택 해제 및 카운트 감소 | ||
if (firstItemSelected && !oldData.firstItem.isSelected) { | ||
return { | ||
...oldData, | ||
firstItem: { | ||
...oldData.firstItem, | ||
isSelected: true, | ||
selected_count: oldData.firstItem.selected_count + 1, | ||
}, | ||
secondItem: oldData.secondItem.isSelected | ||
? { | ||
...oldData.secondItem, | ||
isSelected: false, | ||
selected_count: oldData.secondItem.selected_count - 1, | ||
} | ||
: oldData.secondItem, | ||
}; | ||
} | ||
|
||
if (secondItemSelected && !oldData.secondItem.isSelected) { | ||
return { | ||
...oldData, | ||
secondItem: { | ||
...oldData.secondItem, | ||
isSelected: true, | ||
selected_count: oldData.secondItem.selected_count + 1, | ||
}, | ||
firstItem: oldData.firstItem.isSelected | ||
? { | ||
...oldData.firstItem, | ||
isSelected: false, | ||
selected_count: oldData.firstItem.selected_count - 1, | ||
} | ||
: oldData.firstItem, | ||
}; | ||
} | ||
|
||
return oldData; | ||
}); | ||
|
||
return { previousData }; | ||
}, | ||
|
||
onError: (err, variables, context) => { | ||
console.log(err); | ||
toast.error('다시 시도해주세요.'); | ||
queryClient.setQueryData([QUERY_KEYS.GAMES_ITEMS], context.previousData); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function calculatePercent(value, total) { | ||
return total > 0 ? ((value / total) * 100).toFixed(0) : 0; | ||
} |