Skip to content

Commit

Permalink
Remove unwanted files and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wryonik committed May 26, 2024
1 parent db6a076 commit 091d087
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 288 deletions.
8 changes: 7 additions & 1 deletion packages/app/src/components/EmailInputMethod.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { useState } from "react";
import { Button, OutlinedButton } from "./Button";

const EmailInputMethod = ({ onClickGoogle, onClickEMLFile }) => {
const EmailInputMethod = ({
onClickGoogle,
onClickEMLFile,
}: {
onClickGoogle: () => void;
onClickEMLFile: () => void;
}) => {
return (
<div
style={{
Expand Down
30 changes: 0 additions & 30 deletions packages/app/src/contexts/Account/AccountContext.ts

This file was deleted.

172 changes: 0 additions & 172 deletions packages/app/src/contexts/Account/AccountProvider.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions packages/app/src/contexts/Account/index.ts

This file was deleted.

93 changes: 33 additions & 60 deletions packages/app/src/contexts/GoogleAuth/GoogleAuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import React, { useEffect, useState, ReactNode } from 'react'
import React, { useEffect, useState, ReactNode } from "react";
import {
hasGrantedAllScopesGoogle,
useGoogleLogin,
googleLogout,
} from '@react-oauth/google';
} from "@react-oauth/google";

// import GoogleAuthContext from './GoogleAuthContext'
import { fetchProfile } from '../../hooks/useGmailClient';
import useAccount from '../../hooks/useAccount';
import { esl } from '../../constants';
import GoogleAuthContext from './GoogleAuthContext';

import { fetchProfile } from "../../hooks/useGmailClient";
import { esl } from "../../constants";
import GoogleAuthContext from "./GoogleAuthContext";

interface ProvidersProps {
children: ReactNode;
Expand All @@ -21,72 +19,44 @@ const GoogleAuthProvider = ({ children }: ProvidersProps) => {
* Contexts
*/

const { loggedInEthereumAddress } = useAccount()

/*
* State Keys
*/

const getGoogleAuthTokenKey = () => {
return `googleAuthToken_${loggedInEthereumAddress}`;
}

/*
* State
*/

const [googleAuthToken, setGoogleAuthToken] = useState<any | null>(
() => {
const cachedToken = localStorage.getItem(getGoogleAuthTokenKey());
return cachedToken ? JSON.parse(cachedToken) : null;
}
);
const [googleAuthToken, setGoogleAuthToken] = useState<any | null>();

const [isGoogleAuthed, setIsGoogleAuthed] = useState<boolean>(false);
const [isScopesApproved, setIsScopesApproved] = useState<boolean>(false);
const [loggedInGmail, setLoggedInGmail] = useState<string | null>(null);

/*
* Hooks
*/

useEffect(() => {
esl && console.log('logoOutGoogleAuth');
esl && console.log('checking loggedInEthereumAddress', loggedInEthereumAddress);

if (!loggedInEthereumAddress) {
esl && console.log('logoOutGoogleAuth_1');

googleLogOut();
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [loggedInEthereumAddress]);

useEffect(() => {
esl && console.log('googleAuthScopes_1');
esl && console.log('checking googleAuthToken', googleAuthToken);
esl && console.log("googleAuthScopes_1");
esl && console.log("checking googleAuthToken", googleAuthToken);

if (googleAuthToken) {
esl && console.log('googleAuthScopes_2');
esl && console.log("googleAuthScopes_2");

const allScope = hasGrantedAllScopesGoogle(
googleAuthToken,
'email',
'profile',
'https://www.googleapis.com/auth/gmail.readonly',
"email",
"profile",
"https://www.googleapis.com/auth/gmail.readonly"
);

setIsScopesApproved(allScope);
}
}, [googleAuthToken]);

useEffect(() => {
esl && console.log('googleProfile_1');
esl && console.log('checking googleAuthToken', googleAuthToken);
esl && console.log("googleProfile_1");
esl && console.log("checking googleAuthToken", googleAuthToken);

if (googleAuthToken) {
esl && console.log('googleProfile_2');
esl && console.log("googleProfile_2");

const fetchData = async () => {
try {
Expand All @@ -95,43 +65,46 @@ const GoogleAuthProvider = ({ children }: ProvidersProps) => {
if (email) {
setLoggedInGmail(email);

localStorage.setItem('loggedInEmail', email);
localStorage.setItem("loggedInEmail", email);
}
} catch (error) {
console.error('Error in fetching profile data:', error);
console.error("Error in fetching profile data:", error);
}
};

fetchData();
};
}
}, [googleAuthToken]);

/*
* Helpers
*/

const googleLogIn = useGoogleLogin({
onSuccess: tokenResponse => {
onSuccess: (tokenResponse) => {
setGoogleAuthToken(tokenResponse);
setIsGoogleAuthed(true);

localStorage.setItem(getGoogleAuthTokenKey(), JSON.stringify(tokenResponse));
// localStorage.setItem(
// getGoogleAuthTokenKey(),
// JSON.stringify(tokenResponse)
// );
},
scope: 'email profile https://www.googleapis.com/auth/gmail.readonly',
scope: "email profile https://www.googleapis.com/auth/gmail.readonly",
});

const googleLogOut = () => {
setIsScopesApproved(false);

setGoogleAuthToken(null);
localStorage.removeItem(getGoogleAuthTokenKey());
// localStorage.removeItem(getGoogleAuthTokenKey());

setIsGoogleAuthed(false);
localStorage.removeItem('isGoogleAuthed');
localStorage.removeItem("isGoogleAuthed");

setLoggedInGmail(null);
localStorage.removeItem('loggedInGmail');
localStorage.removeItem("loggedInGmail");

googleLogout();
};

Expand All @@ -151,4 +124,4 @@ const GoogleAuthProvider = ({ children }: ProvidersProps) => {
);
};

export default GoogleAuthProvider
export default GoogleAuthProvider;
Empty file.
Loading

0 comments on commit 091d087

Please sign in to comment.