(
table: string,
config: UseDeleteConfig<Data> = { options: {} },
)
| 30 | } |
| 31 | |
| 32 | export function useDelete<Data = any>( |
| 33 | table: string, |
| 34 | config: UseDeleteConfig<Data> = { options: {} }, |
| 35 | ): UseDeleteResponse<Data> { |
| 36 | const client = useClient() |
| 37 | const isMounted = useRef(false) |
| 38 | const [state, setState] = useState<UseDeleteState>(initialState) |
| 39 | |
| 40 | /* eslint-disable react-hooks/exhaustive-deps */ |
| 41 | const execute = useCallback( |
| 42 | async (filter?: Filter<Data>, options?: UseDeleteOptions) => { |
| 43 | const refine = filter ?? config.filter |
| 44 | if (refine === undefined) |
| 45 | throw Error('delete() should always be combined with `filter`') |
| 46 | |
| 47 | setState({ ...initialState, fetching: true }) |
| 48 | const source = client |
| 49 | .from<Data>(table) |
| 50 | .delete(options ?? config.options) |
| 51 | const { count, data, error } = await refine(source) |
| 52 | |
| 53 | const res = { count, data, error } |
| 54 | if (isMounted.current) setState({ ...res, fetching: false }) |
| 55 | return res |
| 56 | }, |
| 57 | [client], |
| 58 | ) |
| 59 | /* eslint-enable react-hooks/exhaustive-deps */ |
| 60 | |
| 61 | useEffect(() => { |
| 62 | isMounted.current = true |
| 63 | return () => { |
| 64 | isMounted.current = false |
| 65 | } |
| 66 | }, []) |
| 67 | |
| 68 | return [state, execute] |
| 69 | } |
no test coverage detected