import { ApolloError } from "apollo-client"; import { DocumentNode } from "graphql"; import { MutationFunction, MutationResult, useMutation as useBaseMutation } from "react-apollo"; import { useIntl } from "react-intl"; import { commonMessages } from "@saleor/intl"; import { maybe } from "@saleor/misc"; import useNotifier from "./useNotifier"; export type UseMutation = [ MutationFunction, MutationResult ]; export type UseMutationCbs = Partial<{ onCompleted: (data: TData) => void; onError: (error: ApolloError) => void; }>; export type UseMutationHook = ( cbs: UseMutationCbs ) => UseMutation; function makeMutation( mutation: DocumentNode ): UseMutationHook { function useMutation({ onCompleted, onError }: UseMutationCbs): UseMutation { const notify = useNotifier(); const intl = useIntl(); const [mutateFn, result] = useBaseMutation(mutation, { onCompleted, onError: (err: ApolloError) => { if ( maybe( () => err.graphQLErrors[0].extensions.exception.code === "ReadOnlyException" ) ) { notify({ text: intl.formatMessage(commonMessages.readOnly) }); } else { notify({ text: intl.formatMessage(commonMessages.somethingWentWrong) }); } if (onError) { onError(err); } } }); return [mutateFn, result]; } return useMutation; } export default makeMutation;