origin/apps/web/src/components/models/trainPlan/TrainPlanContext.tsx

47 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-03-12 10:02:41 +08:00
import { api } from "@nice/client";
2025-03-12 11:36:31 +08:00
import { useAuth } from "@web/src/providers/auth-provider";
2025-03-12 19:38:28 +08:00
import { Department, UserProfile } from "@nice/common";
2025-03-12 11:43:17 +08:00
import { createContext, ReactNode, useContext, useEffect, useState } from "react";
2025-03-12 10:02:41 +08:00
interface DailyContextProviderProps{
2025-03-12 19:38:28 +08:00
staffs:UserProfile[],
2025-03-12 11:43:17 +08:00
staffsLoading:boolean,
2025-03-12 19:38:28 +08:00
depts:Department[],
deptsLoading:boolean
2025-03-12 10:02:41 +08:00
}
interface DailyContextProps{
children:ReactNode
}
const DailyContextProvider = createContext<DailyContextProviderProps>(null)
export default function DailyContext({children}:DailyContextProps){
2025-03-12 19:38:28 +08:00
const {user,isAuthenticated} = useAuth()
2025-03-12 11:36:31 +08:00
// 获取当前员工的单位下的所有staff的记录
2025-03-12 19:38:28 +08:00
const {data:staffs,isLoading:staffsLoading} = isAuthenticated?api.staff.findByDept.useQuery({
2025-03-12 11:36:31 +08:00
deptId:user.deptId
2025-03-12 19:38:28 +08:00
}):{data:null,isLoading:false}
2025-03-12 11:43:17 +08:00
2025-03-12 19:38:28 +08:00
const {data:depts,isLoading:deptsLoading} = isAuthenticated?api.department.getChildSimpleTree.useQuery({
rootId:user.deptId
}):{data:null,isLoading:false}
2025-03-12 11:43:17 +08:00
2025-03-12 19:38:28 +08:00
useEffect(()=>{
console.log(user)
},[user])
2025-03-12 10:02:41 +08:00
return <DailyContextProvider.Provider value={{
2025-03-12 19:38:28 +08:00
staffs:staffs as any as UserProfile[],
2025-03-12 11:36:31 +08:00
staffsLoading,
2025-03-12 19:38:28 +08:00
depts:depts as any as Department[],
deptsLoading
2025-03-12 10:02:41 +08:00
}}>
{children}
</DailyContextProvider.Provider>
}
export function useDaily(){
const daily = useContext(DailyContextProvider)
if(!daily) {throw new Error("useParams must be used within a ParamsProvider");}
return daily
}