(
table: string,
config: UseInsertConfig = { options: {} },
)
| 29 | } |
| 30 | |
| 31 | export function useInsert<Data = any>( |
| 32 | table: string, |
| 33 | config: UseInsertConfig = { options: {} }, |
| 34 | ): UseInsertResponse<Data> { |
| 35 | const client = useClient() |
| 36 | const isMounted = useRef(false) |
| 37 | const [state, setState] = useState<UseInsertState>(initialState) |
| 38 | |
| 39 | /* eslint-disable react-hooks/exhaustive-deps */ |
| 40 | const execute = useCallback( |
| 41 | async ( |
| 42 | values: Partial<Data> | Partial<Data>[], |
| 43 | options?: UseInsertOptions, |
| 44 | ) => { |
| 45 | setState({ ...initialState, fetching: true }) |
| 46 | const { count, data, error } = await client |
| 47 | .from<Data>(table) |
| 48 | .insert(values, options ?? config.options) |
| 49 | const res = { count, data, error } |
| 50 | if (isMounted.current) setState({ ...res, fetching: false }) |
| 51 | return res |
| 52 | }, |
| 53 | [client], |
| 54 | ) |
| 55 | /* eslint-enable react-hooks/exhaustive-deps */ |
| 56 | |
| 57 | useEffect(() => { |
| 58 | isMounted.current = true |
| 59 | return () => { |
| 60 | isMounted.current = false |
| 61 | } |
| 62 | }, []) |
| 63 | |
| 64 | return [state, execute] |
| 65 | } |
no test coverage detected