-
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.
- Loading branch information
1 parent
fef556e
commit 6a6716b
Showing
2 changed files
with
60 additions
and
2 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,58 @@ | ||
import { create } from "zustand"; | ||
import { fetchServer, HTTPError, ServerCloseError } from "@common/dataFetch/fetchServer.js"; | ||
import { getQuery, getQuerySuspense } from "@common/dataFetch/getQuery.js"; | ||
import { getServerPresiseTime } from "@common/utils.js"; | ||
import { EVENT_DRAW_ID, EVENT_START_DATE } from "@common/constants.js"; | ||
|
||
function getJoinDataEvent() | ||
{ | ||
return fetchServer(`/api/v1/event/draw/${EVENT_DRAW_ID}/participation`) | ||
.then(({ dates }) => { | ||
let newJoinedList = [false, false, false, false, false]; | ||
dates.forEach((date) => { | ||
const day = getDayDifference(EVENT_START_DATE, new Date(date)); | ||
newJoinedList[day] = true; | ||
}); | ||
return newJoinedList; | ||
}) | ||
.catch(() => [false, false, false, false, false]); | ||
} | ||
|
||
const drawEventStore = create( (set, get)=>({ | ||
joinStatus : [false, false, false, false, false], | ||
openBaseDate : new Date("9999-12-31"), | ||
currentJoined: false, | ||
getJoinData: ()=>{ | ||
async function promiseFn() { | ||
try { | ||
const [serverTime, joinStatus] = Promise.all([ | ||
getQuery("server-time", getServerPresiseTime), | ||
getJoinDataEvent() | ||
]); | ||
const currentDay = getDayDifference(EVENT_START_DATE, serverTime); | ||
let currentJoined = false; | ||
if(currentDay >= 0 && currentDay < joinStatus.length) { | ||
currentJoined = joinStatus[currentDay]; | ||
} | ||
set({joinStatus, openBaseDate: serverTime, currentJoined }); | ||
return joinStatus; | ||
} | ||
catch { | ||
set({joinStatus: [false, false, false, false, false], openBaseDate: new Date("9999-12-31")}); | ||
return [false, false, false, false, false]; | ||
} | ||
} | ||
return getQuerySuspense("draw-info-data", promiseFn, [set]); | ||
}, | ||
setCurrentJoin: (value)=>{ | ||
set({currentJoined: value}); | ||
}, | ||
getJoinStatus: (index)=>{ | ||
return get().joinStatus[index]; | ||
}, | ||
getOpenStatus: (index)=>{ | ||
return get().openBaseDate >= EVENT_START_DATE.getTime() + index * DAY_MILLISEC; | ||
} | ||
}) ); | ||
|
||
export default drawEventStore; |
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