You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
927 B
35 lines
927 B
import {createContext, useCallback, useEffect, useState} from "react";
|
|
import {WithChildren} from "../primitives/Shared";
|
|
import {Program} from "../models/Programs";
|
|
import {unimplemented} from "../helpers/misc";
|
|
import programRepo from "../actions/programs";
|
|
|
|
interface ProgramContextValue {
|
|
programs: Program[] | null
|
|
refreshPrograms(): void
|
|
}
|
|
|
|
const ProgramContext = createContext<ProgramContextValue>({
|
|
programs: null,
|
|
refreshPrograms: unimplemented,
|
|
});
|
|
|
|
export function ProgramContextProvider({children}: WithChildren) {
|
|
const [programs, setPrograms] = useState<Program[] | null>(null);
|
|
|
|
const refreshPrograms = useCallback(() => {
|
|
programRepo().fetchAll().then(setPrograms);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
refreshPrograms();
|
|
}, []);
|
|
|
|
return (
|
|
<ProgramContext.Provider value={{programs, refreshPrograms}}>
|
|
{children}
|
|
</ProgramContext.Provider>
|
|
);
|
|
}
|
|
|
|
export default ProgramContext;
|