parent
855083719f
commit
e8c557cd2f
8 changed files with 109 additions and 16 deletions
@ -0,0 +1,62 @@ |
|||||||
|
import { createContext, useState, useEffect, useContext } from "react"; |
||||||
|
import moment from "moment"; |
||||||
|
|
||||||
|
const userContext=createContext(); |
||||||
|
|
||||||
|
const SesstionTime={ |
||||||
|
A:["12:00", "13:30"], |
||||||
|
B:["13:30", "15:00"], |
||||||
|
C:["15:00", "17:30"], |
||||||
|
D:["17:30", "19:00"], |
||||||
|
E:["19:00", "20:30"], |
||||||
|
F:["20:30", "22:00"] |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
export function UserProvider({children}) { |
||||||
|
|
||||||
|
const [userId, setUserId] = useState(null); |
||||||
|
const [sessionId, setSessionId] = useState(null); |
||||||
|
|
||||||
|
|
||||||
|
function getFileId(){ |
||||||
|
if(!userId) return `test-${moment().format("YYYYMMDDmmss")}-${sessionId}`; |
||||||
|
return `${moment().format("YYYYMMDD").substring(2)}-${sessionId}-${userId}`; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
useEffect(() => { |
||||||
|
|
||||||
|
let symbol=''; |
||||||
|
|
||||||
|
for(const [key, value] of Object.entries(SesstionTime)){ |
||||||
|
const start=moment(value[0], "HH:mm"); |
||||||
|
const end=moment(value[1], "HH:mm"); |
||||||
|
|
||||||
|
if(moment().isBetween(start, end)){ |
||||||
|
symbol=key; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
setSessionId(symbol); |
||||||
|
|
||||||
|
|
||||||
|
}, [userId]); |
||||||
|
|
||||||
|
return ( |
||||||
|
<userContext.Provider value={{ userId, setUserId, getFileId }}> |
||||||
|
{children} |
||||||
|
</userContext.Provider> |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
export function useUser() { |
||||||
|
const context = useContext(userContext); |
||||||
|
if (!context) { |
||||||
|
throw new Error("useUser must be used within a UserProvider"); |
||||||
|
} |
||||||
|
return context; |
||||||
|
} |
||||||
|
|
||||||
|
export { userContext }; |
||||||
Loading…
Reference in new issue