chore: initial commit
This commit is contained in:
33
src/pages/system/role/components/crud-provider.tsx
Normal file
33
src/pages/system/role/components/crud-provider.tsx
Normal file
@@ -0,0 +1,33 @@
|
||||
import { createContext, useContext, useState, type ReactNode } from 'react'
|
||||
|
||||
import { type SysRole } 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<SysRole> | null>(null)
|
||||
|
||||
export const CrudProvider = ({ children }: { children: ReactNode }) => {
|
||||
const [action, setAction] = useState<Action>(null)
|
||||
const [currentRow, setCurrentRow] = useState<SysRole | 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
|
||||
}
|
||||
Reference in New Issue
Block a user