2022-02-21 13:32:38 +00:00
|
|
|
import {
|
|
|
|
ApolloError,
|
|
|
|
MutationFunction,
|
2022-03-09 08:56:55 +00:00
|
|
|
MutationHookOptions as BaseMutationHookOptions,
|
2022-02-21 13:32:38 +00:00
|
|
|
MutationResult,
|
|
|
|
useMutation as useBaseMutation
|
|
|
|
} from "@apollo/client";
|
2022-03-04 10:52:58 +00:00
|
|
|
import {
|
|
|
|
handleNestedMutationErrors,
|
|
|
|
showAllErrors,
|
|
|
|
useUser
|
|
|
|
} from "@saleor/auth";
|
2020-05-14 09:30:32 +00:00
|
|
|
import { isJwtError } from "@saleor/auth/errors";
|
|
|
|
import { commonMessages } from "@saleor/intl";
|
2021-04-13 09:59:16 +00:00
|
|
|
import { getMutationStatus } from "@saleor/misc";
|
2020-05-14 09:30:32 +00:00
|
|
|
import { MutationResultAdditionalProps } from "@saleor/types";
|
2021-04-13 09:59:16 +00:00
|
|
|
import { GqlErrors, hasError } from "@saleor/utils/api";
|
2019-11-13 16:46:08 +00:00
|
|
|
import { DocumentNode } from "graphql";
|
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
|
|
|
|
import useNotifier from "./useNotifier";
|
|
|
|
|
2021-08-16 13:44:00 +00:00
|
|
|
export type MutationResultWithOpts<TData> = MutationResult<TData> &
|
|
|
|
MutationResultAdditionalProps;
|
|
|
|
|
2019-11-22 15:39:20 +00:00
|
|
|
export type UseMutation<TData, TVariables> = [
|
2019-11-13 16:46:08 +00:00
|
|
|
MutationFunction<TData, TVariables>,
|
2021-08-16 13:44:00 +00:00
|
|
|
MutationResultWithOpts<TData>
|
2019-11-13 16:46:08 +00:00
|
|
|
];
|
2019-11-22 15:39:20 +00:00
|
|
|
export type UseMutationHook<TData, TVariables> = (
|
2022-03-09 08:56:55 +00:00
|
|
|
cbs: MutationHookOptions<TData, TVariables>
|
2019-11-13 16:46:08 +00:00
|
|
|
) => UseMutation<TData, TVariables>;
|
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
export type MutationHookOptions<TData, TVariables> = BaseMutationHookOptions<
|
|
|
|
TData,
|
|
|
|
TVariables
|
|
|
|
>;
|
2020-05-07 11:04:15 +00:00
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
export function useMutation<TData, TVariables>(
|
|
|
|
mutation: DocumentNode,
|
|
|
|
{ onCompleted, onError, ...opts }: MutationHookOptions<TData, TVariables>
|
|
|
|
): UseMutation<TData, TVariables> {
|
|
|
|
const notify = useNotifier();
|
|
|
|
const intl = useIntl();
|
|
|
|
const user = useUser();
|
2022-03-04 10:52:58 +00:00
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
const [mutateFn, result] = useBaseMutation(mutation, {
|
|
|
|
...opts,
|
|
|
|
onCompleted: data => {
|
|
|
|
handleNestedMutationErrors({
|
|
|
|
data,
|
|
|
|
intl,
|
|
|
|
notify
|
|
|
|
});
|
|
|
|
|
|
|
|
onCompleted(data);
|
|
|
|
},
|
|
|
|
onError: (err: ApolloError) => {
|
|
|
|
if (err.graphQLErrors) {
|
|
|
|
if (hasError(err, GqlErrors.ReadOnlyException)) {
|
|
|
|
notify({
|
|
|
|
status: "error",
|
|
|
|
text: intl.formatMessage(commonMessages.readOnly)
|
|
|
|
});
|
|
|
|
} else if (err.graphQLErrors.some(isJwtError)) {
|
|
|
|
user.logout();
|
|
|
|
notify({
|
|
|
|
status: "error",
|
|
|
|
text: intl.formatMessage(commonMessages.sessionExpired)
|
|
|
|
});
|
|
|
|
} else if (!hasError(err, GqlErrors.LimitReachedException)) {
|
|
|
|
err.graphQLErrors.map(graphQLError => {
|
2022-03-04 10:52:58 +00:00
|
|
|
notify({
|
|
|
|
status: "error",
|
2022-03-09 08:56:55 +00:00
|
|
|
apiMessage: graphQLError.message
|
2022-03-04 10:52:58 +00:00
|
|
|
});
|
2022-03-09 08:56:55 +00:00
|
|
|
});
|
2019-11-13 16:46:08 +00:00
|
|
|
}
|
2022-03-09 08:56:55 +00:00
|
|
|
} else {
|
|
|
|
showAllErrors({ notify, error: err });
|
2019-11-13 16:46:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
if (onError) {
|
|
|
|
onError(err);
|
2019-12-06 17:11:28 +00:00
|
|
|
}
|
2022-03-09 08:56:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return [
|
|
|
|
mutateFn,
|
|
|
|
{
|
|
|
|
...result,
|
|
|
|
status: getMutationStatus(result)
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
2019-11-13 16:46:08 +00:00
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
function makeMutation<TData, TVariables>(
|
|
|
|
mutation: DocumentNode
|
|
|
|
): UseMutationHook<TData, TVariables> {
|
|
|
|
return (opts: MutationHookOptions<TData, TVariables>) =>
|
|
|
|
useMutation<TData, TVariables>(mutation, opts);
|
2019-11-13 16:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default makeMutation;
|