-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#723 이벤트 hook atom 업데이트
- Loading branch information
Showing
8 changed files
with
206 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Quest, QuestId } from "@/types/event2024spring"; | ||
|
||
import { atom } from "recoil"; | ||
|
||
export type Event2024SpringInfoType = Nullable<{ | ||
isAgreeOnTermsOfEvent: boolean; | ||
completedQuests: QuestId[]; | ||
creditAmount: number; | ||
ticket1Amount: number; | ||
ticket2Amount: number; | ||
quests: Quest[]; | ||
}>; | ||
|
||
const event2024SpringInfoAtom = atom<Event2024SpringInfoType>({ | ||
key: "event2024SpringInfoAtom", | ||
default: null, | ||
}); | ||
|
||
export default event2024SpringInfoAtom; |
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,41 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
import type { QuestId } from "@/types/event2024spring"; | ||
|
||
import { useValueRecoilState } from "@/hooks/useFetchRecoilState"; | ||
|
||
import toast from "@/tools/toast"; | ||
|
||
export const useEvent2024SpringEffect = () => { | ||
const { completedQuests, quests } = | ||
useValueRecoilState("event2024SpringInfo") || {}; | ||
|
||
const prevEventStatusRef = useRef<QuestId[]>(); | ||
|
||
useEffect(() => { | ||
if (!completedQuests || !quests) return; | ||
prevEventStatusRef.current = prevEventStatusRef.current || completedQuests; | ||
if (prevEventStatusRef.current.length === completedQuests.length) return; | ||
|
||
const questsForCompare = [...completedQuests]; | ||
prevEventStatusRef.current.forEach((questId) => { | ||
const index = questsForCompare.indexOf(questId); | ||
if (index < 0) return; | ||
questsForCompare.splice(index, 1); | ||
}); | ||
questsForCompare.forEach((questId) => { | ||
const quest = quests.find(({ id }) => id === questId); | ||
if (!quest) return; | ||
const notificationValue = { | ||
type: "default" as const, | ||
imageUrl: quest.imageUrl, | ||
title: "퀘스트 완료", | ||
subtitle: "새터반 택시대제전", | ||
content: `축하합니다! "${quest.name}" 퀘스트를 달성하여 넙죽코인 ${quest.reward.credit}개를 획득하셨습니다.`, | ||
button: { text: "확인하기", path: "/event/2024spring-missions" }, | ||
}; | ||
toast(notificationValue); | ||
}); | ||
prevEventStatusRef.current = completedQuests; | ||
}, [completedQuests]); | ||
}; |
42 changes: 42 additions & 0 deletions
42
packages/web/src/hooks/event/useEvent2024SpringQuestComplete.ts
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,42 @@ | ||
import { useCallback } from "react"; | ||
|
||
import type { QuestId } from "@/types/event2023fall"; | ||
|
||
import { | ||
useFetchRecoilState, | ||
useValueRecoilState, | ||
} from "@/hooks/useFetchRecoilState"; | ||
import { useAxios } from "@/hooks/useTaxiAPI"; | ||
|
||
export const useEvent2024SpringQuestComplete = () => { | ||
const axios = useAxios(); | ||
const fetchEvent2024SpringInfo = useFetchRecoilState("event2024SpringInfo"); | ||
const { completedQuests, quests } = | ||
useValueRecoilState("event2024SpringInfo") || {}; | ||
|
||
return useCallback( | ||
(id: QuestId) => { | ||
if (!completedQuests || !quests) return; | ||
const questMaxCount = | ||
quests?.find((quest) => quest.id === id)?.maxCount || 0; | ||
const questCompletedCount = completedQuests?.filter( | ||
(questId) => questId === id | ||
).length; | ||
if (questCompletedCount >= questMaxCount) return; | ||
if ( | ||
[ | ||
"roomSharing", | ||
"eventSharingOnInstagram", | ||
"purchaseSharingOnInstagram", | ||
].includes(id) | ||
) { | ||
axios({ | ||
url: `/events/2023fall/quests/complete/${id}`, | ||
method: "post", | ||
onSuccess: () => fetchEvent2024SpringInfo(), | ||
}); | ||
} else fetchEvent2024SpringInfo(); | ||
}, | ||
[completedQuests, fetchEvent2024SpringInfo, quests] | ||
); | ||
}; |
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
31 changes: 31 additions & 0 deletions
31
packages/web/src/hooks/useFetchRecoilState/useFetchEvent2024SpringInfo.tsx
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,31 @@ | ||
import { useCallback } from "react"; | ||
|
||
import { useAxios } from "@/hooks/useTaxiAPI"; | ||
import { AxiosOption } from "@/hooks/useTaxiAPI/useAxios"; | ||
|
||
import event2024SpringInfoAtom from "@/atoms/event2024SpringInfo"; | ||
import { useRecoilValue, useSetRecoilState } from "recoil"; | ||
|
||
import { eventMode } from "@/tools/loadenv"; | ||
|
||
export const useValueEvent2024SpringInfo = () => | ||
useRecoilValue(event2024SpringInfoAtom); | ||
export const useSetEvent2024SpringInfo = () => | ||
useSetRecoilState(event2024SpringInfoAtom); | ||
export const useFetchEvent2024SpringInfo = () => { | ||
const setEvent2024SpringInfo = useSetEvent2024SpringInfo(); | ||
const axios = useAxios(); | ||
|
||
return useCallback((onError?: AxiosOption["onError"]) => { | ||
if (eventMode === "2024spring") { | ||
axios({ | ||
url: "/events/2024spring/global-state/", | ||
method: "get", | ||
onSuccess: (data) => setEvent2024SpringInfo(data), | ||
onError: onError, | ||
}); | ||
} else { | ||
setEvent2024SpringInfo(null); | ||
} | ||
}, []); | ||
}; |
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,51 @@ | ||
export type EventItem = { | ||
_id: string; | ||
name: string; | ||
imageUrl: string; | ||
instagramStoryStickerImageUrl?: string; | ||
price: number; | ||
description: string; | ||
isDisabled: boolean; | ||
stock: number; | ||
itemType: number; | ||
}; | ||
|
||
export type Transaction = { | ||
_id: string; | ||
amount: number; | ||
comment: string; | ||
createAt: Date; | ||
} & ( | ||
| { | ||
type: "get"; | ||
questId: string; | ||
item: never; | ||
} | ||
| { | ||
type: "use"; | ||
item: EventItem; | ||
questId: never; | ||
} | ||
); | ||
|
||
export type Quest = { | ||
description: string; | ||
id: QuestId; | ||
imageUrl: string; | ||
maxCount: number; | ||
name: string; | ||
reward: { credit: number }; | ||
}; | ||
|
||
export type QuestId = | ||
| "firstLogin" | ||
| "payingAndSending" | ||
| "firstRoomCreation" | ||
| "roomSharing" | ||
| "paying" | ||
| "sending" | ||
| "nicknameChanging" | ||
| "accountChanging" | ||
| "adPushAgreement" | ||
| "eventSharing" | ||
| "eventSharing5"; |