feat: context factory
This commit is contained in:
@ -1,9 +1,10 @@
|
||||
"use client";
|
||||
|
||||
import React, { createContext, FC, useContext } from "react";
|
||||
import React from "react";
|
||||
import { StatusesCrud, useStatusesCrud } from "@/hooks/cruds/useStatusesCrud";
|
||||
import useStatusesList from "@/hooks/lists/useStatusesList";
|
||||
import { BoardSchema, StatusSchema } from "@/lib/client";
|
||||
import makeContext from "@/lib/contextFactory/contextFactory";
|
||||
|
||||
type BoardStatusesContextState = {
|
||||
board: BoardSchema;
|
||||
@ -13,15 +14,13 @@ type BoardStatusesContextState = {
|
||||
statusesCrud: StatusesCrud;
|
||||
};
|
||||
|
||||
const BoardStatusesContext = createContext<
|
||||
BoardStatusesContextState | undefined
|
||||
>(undefined);
|
||||
|
||||
type Props = {
|
||||
board: BoardSchema;
|
||||
};
|
||||
|
||||
const useBoardStatusesContextState = ({ board }: Props) => {
|
||||
const useBoardStatusesContextState = ({
|
||||
board,
|
||||
}: Props): BoardStatusesContextState => {
|
||||
const {
|
||||
statuses,
|
||||
setStatuses,
|
||||
@ -46,28 +45,8 @@ const useBoardStatusesContextState = ({ board }: Props) => {
|
||||
};
|
||||
};
|
||||
|
||||
type BoardStatusesContextProviderProps = {
|
||||
children: React.ReactNode;
|
||||
} & Props;
|
||||
|
||||
export const BoardStatusesContextProvider: FC<
|
||||
BoardStatusesContextProviderProps
|
||||
> = ({ children, ...props }) => {
|
||||
const state = useBoardStatusesContextState(props);
|
||||
|
||||
return (
|
||||
<BoardStatusesContext.Provider value={state}>
|
||||
{children}
|
||||
</BoardStatusesContext.Provider>
|
||||
export const [BoardStatusesContextProvider, useBoardStatusesContext] =
|
||||
makeContext<BoardStatusesContextState, Props>(
|
||||
useBoardStatusesContextState,
|
||||
"BoardStatuses"
|
||||
);
|
||||
};
|
||||
|
||||
export const useBoardStatusesContext = () => {
|
||||
const context = useContext(BoardStatusesContext);
|
||||
if (!context) {
|
||||
throw new Error(
|
||||
"useBoardStatusesContext must be used within a BoardStatusesContextProvider"
|
||||
);
|
||||
}
|
||||
return context;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user