34 lines
880 B
TypeScript
34 lines
880 B
TypeScript
import { createContext, useContext, useState, type ReactNode } from 'react'
|
|
|
|
import { type Role } from '@/schemas'
|
|
|
|
import type { Action } from '../constants'
|
|
|
|
type CrudContextType<T> = {
|
|
action: Action
|
|
setAction: (action: Action) => void
|
|
currentRow: T | null
|
|
setCurrentRow: (row: T | null) => void
|
|
}
|
|
|
|
export const CrudContext = createContext<CrudContextType<Role> | null>(null)
|
|
|
|
export const CrudProvider = ({ children }: { children: ReactNode }) => {
|
|
const [action, setAction] = useState<Action>(null)
|
|
const [currentRow, setCurrentRow] = useState<Role | null>(null)
|
|
|
|
return (
|
|
<CrudContext value={{ action, setAction, currentRow, setCurrentRow }}>{children}</CrudContext>
|
|
)
|
|
}
|
|
|
|
export const useCrud = () => {
|
|
const context = useContext(CrudContext)
|
|
|
|
if (!context) {
|
|
throw new Error('context must be used within a CrudProvider')
|
|
}
|
|
|
|
return context
|
|
}
|