Let user know that he's in demo

This commit is contained in:
dominik-zeglen 2019-10-25 14:35:48 +02:00 committed by Krzysztof Wolski
parent 47bd37e6ad
commit b673c12eea

View file

@ -1,10 +1,13 @@
import { maybe } from "@saleor/misc"; import { maybe } from "@saleor/misc";
import React from "react";
import { useIntl } from "react-intl";
import useNotifier from "@saleor/hooks/useNotifier";
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 from "react";
import { MutationFunction, MutationResult } from "react-apollo"; import { MutationFunction, MutationResult } from "react-apollo";
import { UserContext } from "./"; import { UserContext } from "./";
@ -30,7 +33,19 @@ interface AuthProviderOperationsProps {
} }
const AuthProviderOperations: React.FC<AuthProviderOperationsProps> = ({ const AuthProviderOperations: React.FC<AuthProviderOperationsProps> = ({
children children
}) => ( }) => {
const intl = useIntl();
const notify = useNotifier();
const handleLogin = () =>
notify({
text: intl.formatMessage({
defaultMessage:
"Just to let you know... You're in demo mode. You can play around with the dashboard but can't save changes."
})
});
return (
<TypedTokenAuthMutation> <TypedTokenAuthMutation>
{(...tokenAuth) => ( {(...tokenAuth) => (
<TypedVerifyTokenMutation> <TypedVerifyTokenMutation>
@ -41,6 +56,7 @@ const AuthProviderOperations: React.FC<AuthProviderOperationsProps> = ({
tokenAuth={tokenAuth} tokenAuth={tokenAuth}
tokenVerify={tokenVerify} tokenVerify={tokenVerify}
tokenRefresh={tokenRefresh} tokenRefresh={tokenRefresh}
onLogin={handleLogin}
> >
{children} {children}
</AuthProvider> </AuthProvider>
@ -50,7 +66,8 @@ const AuthProviderOperations: React.FC<AuthProviderOperationsProps> = ({
</TypedVerifyTokenMutation> </TypedVerifyTokenMutation>
)} )}
</TypedTokenAuthMutation> </TypedTokenAuthMutation>
); );
};
interface AuthProviderProps { interface AuthProviderProps {
children: (props: { children: (props: {
@ -72,6 +89,7 @@ interface AuthProviderProps {
MutationFunction<RefreshToken, RefreshTokenVariables>, MutationFunction<RefreshToken, RefreshTokenVariables>,
MutationResult<RefreshToken> MutationResult<RefreshToken>
]; ];
onLogin: () => void;
} }
interface AuthProviderState { interface AuthProviderState {
@ -130,11 +148,12 @@ class AuthProvider extends React.Component<
} }
login = async (email: string, password: string) => { login = async (email: string, password: string) => {
const { tokenAuth } = this.props; const { tokenAuth, onLogin } = this.props;
const [tokenAuthFn] = tokenAuth; const [tokenAuthFn] = tokenAuth;
tokenAuthFn({ variables: { email, password } }).then(result => { tokenAuthFn({ variables: { email, password } }).then(result => {
if (result && !result.data.tokenCreate.errors.length) { if (result && !result.data.tokenCreate.errors.length) {
onLogin();
saveCredentials(result.data.tokenCreate.user, password); saveCredentials(result.data.tokenCreate.user, password);
} }
}); });