Files
Compass/web/hooks/use-mutation.ts
Martin Braquet ba9b3cfb06 Add pretty formatting (#29)
* Test

* Add pretty formatting

* Fix Tests

* Fix Tests

* Fix Tests

* Fix

* Add pretty formatting fix

* Fix

* Test

* Fix tests

* Clean typeckech

* Add prettier check

* Fix api tsconfig

* Fix api tsconfig

* Fix tsconfig

* Fix

* Fix

* Prettier
2026-02-20 17:32:27 +01:00

37 lines
926 B
TypeScript

import {useCallback, useState} from 'react'
// simplified version of react-query useMutation
export const useMutation = <T extends Array<unknown>, R>(
fn: (...props: T) => Promise<R>,
options?: {
onSuccess?: (data: R) => void
onError?: (error: unknown) => void
},
) => {
const {onSuccess, onError} = options ?? {}
const [isLoading, setLoading] = useState(false)
const [error, setError] = useState<unknown | null>(null)
const [data, setData] = useState<R | null>(null)
const mutate = useCallback(
async (...props: T) => {
setLoading(true)
setError(null)
try {
const data = await fn(...props)
setData(data)
onSuccess?.(data)
return data
} catch (error) {
setError(error)
onError?.(error)
} finally {
setLoading(false)
}
},
[fn, onError, onSuccess],
)
return {mutate, data, isLoading, error}
}