import { useCallback } from 'react' ; import { useSWRConfig } from 'swr' ; import { User } from 'types/User' ; import usePost from '../_rest/usePost' ; import { UpdateUserRequest , UpdateUserResponse } from './types' ; import { getUserCacheKey } from './useUser' ; export default function useUpdateUser ( ) { const updateUser = usePost ( ) ; const { mutate } = useSWRConfig ( ) ; return useCallback ( async ( userId , username , bio ) => { await updateUser < UpdateUserResponse , UpdateUserRequest > ( '/users/update/info' , 'update user' , { username , bio , } ) ; await mutate ( getUserCacheKey ( { username } ) , ( user ) => ( { ... user , username , bio } ) , false ) ; await mutate ( getUserCacheKey ( { id : userId } ) , ( user ) => ( { ... user , username , bio } ) , false ) ; } , [ mutate , updateUser ] ) ; }