2022-02-07 18:06:37 +01:00
|
|
|
import { Credentials } from 'models'
|
2022-01-18 18:25:18 +01:00
|
|
|
import useSWR from 'swr'
|
2022-02-07 18:06:37 +01:00
|
|
|
import { sendRequest } from 'utils'
|
2022-01-18 18:25:18 +01:00
|
|
|
import { fetcher } from './utils'
|
|
|
|
|
|
|
|
export const useCredentials = ({
|
|
|
|
userId,
|
|
|
|
onError,
|
|
|
|
}: {
|
|
|
|
userId?: string
|
|
|
|
onError: (error: Error) => void
|
|
|
|
}) => {
|
|
|
|
const { data, error, mutate } = useSWR<{ credentials: Credentials[] }, Error>(
|
|
|
|
userId ? `/api/users/${userId}/credentials` : null,
|
|
|
|
fetcher
|
|
|
|
)
|
|
|
|
if (error) onError(error)
|
|
|
|
return {
|
|
|
|
credentials: data?.credentials,
|
|
|
|
isLoading: !error && !data,
|
|
|
|
mutate,
|
|
|
|
}
|
|
|
|
}
|
2022-02-07 18:06:37 +01:00
|
|
|
|
|
|
|
export const createCredentials = async (
|
|
|
|
userId: string,
|
|
|
|
credentials: Omit<Credentials, 'ownerId' | 'id' | 'iv'>
|
|
|
|
) =>
|
|
|
|
sendRequest<{
|
|
|
|
credentials: Credentials
|
|
|
|
}>({
|
|
|
|
url: `/api/users/${userId}/credentials`,
|
|
|
|
method: 'POST',
|
|
|
|
body: credentials,
|
|
|
|
})
|