use Apollo useMutation instead of custom hook

This commit is contained in:
Dawid Tarasiuk 2020-07-14 16:31:41 +02:00 committed by dominik-zeglen
parent 9b97e3279b
commit dd4022dcd7
2 changed files with 30 additions and 24 deletions

View file

@ -1,21 +1,25 @@
import { DEMO_MODE } from "@saleor/config"; import { DEMO_MODE } from "@saleor/config";
import { User } from "@saleor/fragments/types/User"; import { User } from "@saleor/fragments/types/User";
import useNotifier from "@saleor/hooks/useNotifier"; import useNotifier from "@saleor/hooks/useNotifier";
import { maybe } from "@saleor/misc"; import { getMutationStatus, maybe } from "@saleor/misc";
import { import {
isSupported as isCredentialsManagementAPISupported, isSupported as isCredentialsManagementAPISupported,
login as loginWithCredentialsManagementAPI, login as loginWithCredentialsManagementAPI,
saveCredentials saveCredentials
} from "@saleor/utils/credentialsManagement"; } from "@saleor/utils/credentialsManagement";
import React, { useContext, useEffect, useState } from "react"; import React, { useContext, useEffect, useState } from "react";
import { useMutation } from "react-apollo";
import { useIntl } from "react-intl"; import { useIntl } from "react-intl";
import { UserContext } from "./"; import { UserContext } from "./";
import { import {
useTokenAuthMutation, tokenAuthMutation,
useTokenRefreshMutation, tokenRefreshMutation,
useTokenVerifyMutation tokenVerifyMutation
} from "./mutations"; } from "./mutations";
import { RefreshToken, RefreshTokenVariables } from "./types/RefreshToken";
import { TokenAuth, TokenAuthVariables } from "./types/TokenAuth";
import { VerifyToken, VerifyTokenVariables } from "./types/VerifyToken";
import { import {
displayDemoMessage, displayDemoMessage,
getAuthToken, getAuthToken,
@ -31,9 +35,26 @@ const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
const intl = useIntl(); const intl = useIntl();
const notify = useNotifier(); const notify = useNotifier();
const [tokenAuth, tokenAuthOpts] = useTokenAuthMutation({}); const [tokenAuth, tokenAuthResult] = useMutation<
const [tokenRefresh] = useTokenRefreshMutation({}); TokenAuth,
const [tokenVerify, tokenVerifyOpts] = useTokenVerifyMutation({}); TokenAuthVariables
>(tokenAuthMutation);
const [tokenRefresh] = useMutation<RefreshToken, RefreshTokenVariables>(
tokenRefreshMutation
);
const [tokenVerify, tokenVerifyResult] = useMutation<
VerifyToken,
VerifyTokenVariables
>(tokenVerifyMutation);
const tokenAuthOpts = {
...tokenAuthResult,
status: getMutationStatus(tokenAuthResult)
};
const tokenVerifyOpts = {
...tokenVerifyResult,
status: getMutationStatus(tokenVerifyResult)
};
const [userContext, setUserContext] = useState<undefined | User>(undefined); const [userContext, setUserContext] = useState<undefined | User>(undefined);
const [persistToken] = useState<boolean>(false); const [persistToken] = useState<boolean>(false);
@ -130,7 +151,7 @@ const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
export const useAuth = () => { export const useAuth = () => {
const user = useContext(UserContext); const user = useContext(UserContext);
const isAuthenticated = !!user; const isAuthenticated = !!user.user;
return { return {
hasToken: !!getAuthToken(), hasToken: !!getAuthToken(),

View file

@ -1,17 +1,13 @@
import { fragmentUser } from "@saleor/fragments/auth"; import { fragmentUser } from "@saleor/fragments/auth";
import { accountErrorFragment } from "@saleor/fragments/errors"; import { accountErrorFragment } from "@saleor/fragments/errors";
import makeMutation from "@saleor/hooks/makeMutation";
import gql from "graphql-tag"; import gql from "graphql-tag";
import { TypedMutation } from "../mutations"; import { TypedMutation } from "../mutations";
import { RefreshToken, RefreshTokenVariables } from "./types/RefreshToken";
import { import {
RequestPasswordReset, RequestPasswordReset,
RequestPasswordResetVariables RequestPasswordResetVariables
} from "./types/RequestPasswordReset"; } from "./types/RequestPasswordReset";
import { SetPassword, SetPasswordVariables } from "./types/SetPassword"; import { SetPassword, SetPasswordVariables } from "./types/SetPassword";
import { TokenAuth, TokenAuthVariables } from "./types/TokenAuth";
import { VerifyToken, VerifyTokenVariables } from "./types/VerifyToken";
export const tokenAuthMutation = gql` export const tokenAuthMutation = gql`
${fragmentUser} ${fragmentUser}
@ -28,9 +24,6 @@ export const tokenAuthMutation = gql`
} }
} }
`; `;
export const useTokenAuthMutation = makeMutation<TokenAuth, TokenAuthVariables>(
tokenAuthMutation
);
export const tokenVerifyMutation = gql` export const tokenVerifyMutation = gql`
${fragmentUser} ${fragmentUser}
@ -43,22 +36,14 @@ export const tokenVerifyMutation = gql`
} }
} }
`; `;
export const useTokenVerifyMutation = makeMutation<
VerifyToken,
VerifyTokenVariables
>(tokenVerifyMutation);
const refreshToken = gql` export const tokenRefreshMutation = gql`
mutation RefreshToken($token: String!) { mutation RefreshToken($token: String!) {
tokenRefresh(csrfToken: $token) { tokenRefresh(csrfToken: $token) {
token token
} }
} }
`; `;
export const useTokenRefreshMutation = makeMutation<
RefreshToken,
RefreshTokenVariables
>(refreshToken);
export const requestPasswordReset = gql` export const requestPasswordReset = gql`
${accountErrorFragment} ${accountErrorFragment}