import {useMutation, useQueryClient} from 'react-query';
import {ACCESS_TOKEN_KEY, fetchToken, REFRESH_TOKEN_KEY} from '../api/auth';

export function useLoginMutation() {
  const queryClient = useQueryClient();

  function onSuccess({access, refresh}) {
    localStorage.setItem(ACCESS_TOKEN_KEY, access);
    localStorage.setItem(REFRESH_TOKEN_KEY, refresh);
    queryClient.invalidateQueries('user');
  }

  return useMutation(fetchToken, {onSuccess});
}
