From 0187cf61d11678c0a02c267f81a9e43e3b6a7bc5 Mon Sep 17 00:00:00 2001 From: Dnouv Date: Thu, 16 Jun 2022 01:29:15 +0530 Subject: [PATCH 1/3] Addsupport for key value abstraction --- .../superprofile/nftSuperAbstract.js | 26 +++++++++++++++++++ app/components/wallet/NFTprofile.js | 25 +++++++++--------- 2 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 app/components/superprofile/nftSuperAbstract.js diff --git a/app/components/superprofile/nftSuperAbstract.js b/app/components/superprofile/nftSuperAbstract.js new file mode 100644 index 00000000..3f31e360 --- /dev/null +++ b/app/components/superprofile/nftSuperAbstract.js @@ -0,0 +1,26 @@ +import { gql, useMutation } from "@apollo/client"; + +const UPSERT_NFT = gql` +mutation UpsertNFT($id: String!, $address: String!, $token: String!) { + upsertNFT(id: $id, address: $address, token: $token) { + _id + address + token + } +} +`; + +export const superProfile = () => { + //the different useMutation function goes here + const [upsertNFT, { data, loading, error, reset }] = useMutation(UPSERT_NFT); + + const callSuper = (key, prop) => { + if (key == "nft") { + upsertNFT({ variables: { id: prop.uid, address: prop.address, token: prop.token } }) + } + // Add other cases eg. if (key == "user") + } + + return {callSuper, data, loading, error, reset} + +} \ No newline at end of file diff --git a/app/components/wallet/NFTprofile.js b/app/components/wallet/NFTprofile.js index 85bd779f..696c244f 100644 --- a/app/components/wallet/NFTprofile.js +++ b/app/components/wallet/NFTprofile.js @@ -3,18 +3,8 @@ import { Alert, Button, Image, Modal, Spinner } from "react-bootstrap"; import { connectAccount, fetchAssets } from "../../lib/walletAPI"; import { ErrorModal } from "./connectMeta"; import styles from "../../styles/meta.module.css"; -import { gql, useMutation } from "@apollo/client"; import Cookies from "js-cookie"; - -const UPSERT_NFT = gql` - mutation UpsertNFT($id: String!, $address: String!, $token: String!) { - upsertNFT(id: $id, address: $address, token: $token) { - _id - address - token - } - } -`; +import { superProfile } from "../superprofile/nftSuperAbstract"; const NFTProfile = ({ limit }) => { const [assets, setAssets] = useState(null); @@ -109,21 +99,30 @@ const GalleryModal = ({ errMess, setErrMess, }) => { - const [upsertNFT, { data, loading, error, reset }] = useMutation(UPSERT_NFT); + const { callSuper, data, loading, error, reset } = superProfile(); + useEffect(() => { if (data) { setLoad(false); } }, [data]); + if (loading) { setLoad(true); } + const handleSubmit = (e) => { e.preventDefault(); const assetSelected = assets[select.split("_")[1]]; const address = assetSelected.asset_contract.address; const token = assetSelected.token_id; - upsertNFT({ variables: { id: uid, address: address, token: token } }); + const data = { + uid: uid, + address: address, + token: token, + }; + + callSuper("nft", data); }; if (error) { From 1198aa01a08043bc4fc43ff13bee8efc7481d4c7 Mon Sep 17 00:00:00 2001 From: Dnouv Date: Thu, 14 Jul 2022 18:43:42 +0530 Subject: [PATCH 2/3] abstract user profile and distribute based on query and mutataion --- .../auth/firebase/ui/FirebaseLoginForm.js | 28 +---- .../auth/firebase/ui/FirebaseSignupForm.js | 28 +---- app/components/superprofile/SuperMutate.js | 68 ++++++++++++ app/components/superprofile/SuperQuery.js | 35 ++++++ .../superprofile/nftSuperAbstract.js | 26 ----- app/components/wallet/NFTprofile.js | 6 +- app/pages/profile/[uid].js | 103 ++++++++---------- 7 files changed, 160 insertions(+), 134 deletions(-) create mode 100644 app/components/superprofile/SuperMutate.js create mode 100644 app/components/superprofile/SuperQuery.js delete mode 100644 app/components/superprofile/nftSuperAbstract.js diff --git a/app/components/auth/firebase/ui/FirebaseLoginForm.js b/app/components/auth/firebase/ui/FirebaseLoginForm.js index e4baa7b7..97355506 100644 --- a/app/components/auth/firebase/ui/FirebaseLoginForm.js +++ b/app/components/auth/firebase/ui/FirebaseLoginForm.js @@ -13,25 +13,11 @@ import {getApp} from 'firebase/app'; import { useState } from "react"; import { FormControl, Alert, Button } from "react-bootstrap"; import {FB_APP_NAME} from '../lib/constants'; -import { useMutation, gql } from '@apollo/client' import Cookies from "js-cookie"; - - -const UPSERT_USER = gql` - mutation UpsertUser($uid: String!, $email: String!, $displayName: String!, $phoneNumber: String, $photoURL: String ) { - upsertUser(uid: $uid, email: $email, displayName: $displayName, phoneNumber: $phoneNumber, photoURL: $photoURL) { - _id - uid - email - displayName - phoneNumber - photoURL - } - } -`; +import { superProMutate } from "../../../superprofile/SuperMutate"; export default function FirebaseLoginForm({onSignupClick}){ - const [upsertUserFunc, { data, loading, error }] = useMutation(UPSERT_USER); + const [upsertUserFunc, { data, loading, error }] = superProMutate("user"); const [email,setEmail] = useState(""); const [password,setPassword] = useState(""); const [errorMessage,setError] = useState(""); @@ -81,15 +67,7 @@ export default function FirebaseLoginForm({onSignupClick}){ try { const userCred = await signInWithPopup(auth,provider); Cookies.set('user', userCred.user.uid); - await upsertUserFunc({ - variables: { - uid: userCred.user.uid, - email: userCred.user.email, - displayName: userCred.user.displayName, - phoneNumber: userCred.user.phoneNumber, - photoURL: userCred.user.photoURL - }, - }) + await upsertUserFunc("user", userCred) if(diffCredError){ // The signin was requested to link new credentials with the account await linkWithCredential(userCred.user,OAuthProvider.credentialFromError(diffCredError.error)); diff --git a/app/components/auth/firebase/ui/FirebaseSignupForm.js b/app/components/auth/firebase/ui/FirebaseSignupForm.js index 7b274d53..d797b9d5 100644 --- a/app/components/auth/firebase/ui/FirebaseSignupForm.js +++ b/app/components/auth/firebase/ui/FirebaseSignupForm.js @@ -4,25 +4,11 @@ import {reload} from 'firebase/auth'; import { useState, useEffect } from "react"; import { FormControl, Alert, Button } from "react-bootstrap"; import {FB_APP_NAME} from '../lib/constants'; -import { useMutation, gql } from '@apollo/client' import Cookies from "js-cookie"; - -const UPSERT_USER = gql` - mutation UpsertUser($uid: String!, $email: String!, $displayName: String!, $phoneNumber: String, $photoURL: String ) { - upsertUser(uid: $uid, email: $email, displayName: $displayName, phoneNumber: $phoneNumber, photoURL: $photoURL) { - _id - uid - email - displayName - phoneNumber - photoURL - } - } -`; - +import { superProMutate } from "../../../superprofile/SuperMutate"; export default function FirebaseSignupForm({onSignupComplete}){ - const [upsertUserFunc, { data, loading, error }] = useMutation(UPSERT_USER); + const [upsertUserFunc, { data, loading, error }] = superProMutate("user"); const [email,setEmail] = useState(""); const [name,setName] = useState(""); const [password1,setPassword1] = useState(""); @@ -70,15 +56,7 @@ export default function FirebaseSignupForm({onSignupComplete}){ const userCred = await createUserWithEmailAndPassword(getAuth(fbApp),email,password1); await updateProfile(userCred.user,{displayName: name}); await reload(userCred.user); - upsertUserFunc({ - variables: { - uid: userCred.user.uid, - email: userCred.user.email, - displayName: userCred.user.displayName, - phoneNumber: userCred.user.phoneNumber, - photoURL: userCred.user.photoURL - }, - }) + upsertUserFunc("user", userCred) Cookies.set('user', userCred.user.uid); await sendEmailVerification(userCred.user); onSignupComplete && onSignupComplete(); diff --git a/app/components/superprofile/SuperMutate.js b/app/components/superprofile/SuperMutate.js new file mode 100644 index 00000000..80d624a9 --- /dev/null +++ b/app/components/superprofile/SuperMutate.js @@ -0,0 +1,68 @@ +import { gql, useMutation } from "@apollo/client"; + +const UPSERT_NFT = gql` + mutation UpsertNFT($id: String!, $address: String!, $token: String!) { + upsertNFT(id: $id, address: $address, token: $token) { + _id + address + token + } + } +`; +const UPSERT_USER = gql` + mutation UpsertUser( + $uid: String! + $email: String! + $displayName: String! + $phoneNumber: String + $photoURL: String + ) { + upsertUser( + uid: $uid + email: $email + displayName: $displayName + phoneNumber: $phoneNumber + photoURL: $photoURL + ) { + _id + uid + email + displayName + phoneNumber + photoURL + } + } +`; + +export const superProMutate = (target) => { + //the different useMutation function goes here + let mutationSchema = null; + if (target == "nft") { + mutationSchema = UPSERT_NFT; + } + if (target === "user") { + mutationSchema = UPSERT_USER; + } + const callSuper = (key, prop) => { + if (key === "nft") { + upsert({ + variables: { id: prop.uid, address: prop.address, token: prop.token }, + }); + } + if (key === "user") { + upsert({ + variables: { + uid: prop.user.uid, + email: prop.user.email, + displayName: prop.user.displayName, + phoneNumber: prop.user.phoneNumber, + photoURL: prop.user.photoURL, + }, + }); + } + // Add other cases eg. if (key == "user") + }; + const [upsert, { data, loading, error, reset }] = useMutation(mutationSchema); + + return [ callSuper, {data, loading, error, reset} ]; +}; diff --git a/app/components/superprofile/SuperQuery.js b/app/components/superprofile/SuperQuery.js new file mode 100644 index 00000000..cfd1f341 --- /dev/null +++ b/app/components/superprofile/SuperQuery.js @@ -0,0 +1,35 @@ +import { useLazyQuery } from "@apollo/client"; + +const FIND_USER_UID = gql` + query findbyUid($uid: String!) { + findUserByUid(uid: $uid) { + _id + uid + displayName + email + photoURL + phoneNumber + } + } +`; + +export const superProQuery = (target) => { + //the different useMutation function goes here + let querySchema = null; + if (target === "user") { + querySchema = UPSERT_USER; + } + const callSuper = (key, prop) => { + if (key === "user") { + query({ + variables: { + uid: prop.uid + } + }); + } + // Add other cases eg. if (key == "user") + }; + const [query, { data, error, loading }] = useLazyQuery(querySchema); + + return [ callSuper, {data, loading, error} ]; + }; \ No newline at end of file diff --git a/app/components/superprofile/nftSuperAbstract.js b/app/components/superprofile/nftSuperAbstract.js deleted file mode 100644 index 3f31e360..00000000 --- a/app/components/superprofile/nftSuperAbstract.js +++ /dev/null @@ -1,26 +0,0 @@ -import { gql, useMutation } from "@apollo/client"; - -const UPSERT_NFT = gql` -mutation UpsertNFT($id: String!, $address: String!, $token: String!) { - upsertNFT(id: $id, address: $address, token: $token) { - _id - address - token - } -} -`; - -export const superProfile = () => { - //the different useMutation function goes here - const [upsertNFT, { data, loading, error, reset }] = useMutation(UPSERT_NFT); - - const callSuper = (key, prop) => { - if (key == "nft") { - upsertNFT({ variables: { id: prop.uid, address: prop.address, token: prop.token } }) - } - // Add other cases eg. if (key == "user") - } - - return {callSuper, data, loading, error, reset} - -} \ No newline at end of file diff --git a/app/components/wallet/NFTprofile.js b/app/components/wallet/NFTprofile.js index 696c244f..d8600bde 100644 --- a/app/components/wallet/NFTprofile.js +++ b/app/components/wallet/NFTprofile.js @@ -4,7 +4,7 @@ import { connectAccount, fetchAssets } from "../../lib/walletAPI"; import { ErrorModal } from "./connectMeta"; import styles from "../../styles/meta.module.css"; import Cookies from "js-cookie"; -import { superProfile } from "../superprofile/nftSuperAbstract"; +import { superProMutate } from "../superprofile/SuperMutate"; const NFTProfile = ({ limit }) => { const [assets, setAssets] = useState(null); @@ -99,7 +99,7 @@ const GalleryModal = ({ errMess, setErrMess, }) => { - const { callSuper, data, loading, error, reset } = superProfile(); + const [ superMutate, {data, loading, error, reset} ] = superProMutate("nft"); useEffect(() => { if (data) { @@ -122,7 +122,7 @@ const GalleryModal = ({ token: token, }; - callSuper("nft", data); + superMutate("nft", data); }; if (error) { diff --git a/app/pages/profile/[uid].js b/app/pages/profile/[uid].js index 84afcb29..9acb7aef 100644 --- a/app/pages/profile/[uid].js +++ b/app/pages/profile/[uid].js @@ -1,62 +1,55 @@ -import { useRouter } from 'next/router' -import { useLazyQuery, gql } from '@apollo/client' -import { useEffect } from 'react'; -import Cookies from 'js-cookie'; -import { NoUserAvatar } from '../../components/auth/NoUserAvatar'; +import { useRouter } from "next/router"; +import { useEffect } from "react"; +import Cookies from "js-cookie"; +import { NoUserAvatar } from "../../components/auth/NoUserAvatar"; -const FindUserByUid = gql` - query findbyUid($uid: String!) { - findUserByUid(uid: $uid) { - _id - uid - displayName - email - photoURL - phoneNumber - } - } -`; - const Profile = () => { - const router = useRouter() - const { uid } = router.query - const cookies = Cookies.get('user'); - const [getCurrentUser, { data, error, loading }] = useLazyQuery(FindUserByUid); + const router = useRouter(); + const { uid } = router.query; + const cookies = Cookies.get("user"); + const [getCurrentUser, { data, error, loading }] = superProQuery("user"); - useEffect(() => { - if(!cookies) { - router.push('/') - } - getCurrentUser({ - variables: { - uid: uid - } - }) - }, []) + useEffect(() => { + if (!cookies) { + router.push("/"); + } + getCurrentUser("user", { + uid: uid, + }); + }, []); - if(error) console.log(error) - - if(data?.findUserByUid){ - const user = data.findUserByUid - return( - <> -
{ - user?.photoURL ? - {user.displayName} - : + if (error) console.log(error); + + if (data?.findUserByUid) { + const user = data.findUserByUid; + return ( + <> +
+ {user?.photoURL ? ( + {user.displayName} + ) : ( - } -

{user.displayName}

-
- - ) - } + )} +

{user.displayName}

+
+ + ); + } - return <> - } - export default Profile \ No newline at end of file + return <>; +}; +export default Profile; From 475914f72117f5f621701d7cfee98aa21e5239a7 Mon Sep 17 00:00:00 2001 From: Dnouv Date: Thu, 14 Jul 2022 18:53:27 +0530 Subject: [PATCH 3/3] fix lgtm warning --- app/components/superprofile/SuperQuery.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/components/superprofile/SuperQuery.js b/app/components/superprofile/SuperQuery.js index cfd1f341..edee5c56 100644 --- a/app/components/superprofile/SuperQuery.js +++ b/app/components/superprofile/SuperQuery.js @@ -1,4 +1,4 @@ -import { useLazyQuery } from "@apollo/client"; +import { gql, useLazyQuery } from "@apollo/client"; const FIND_USER_UID = gql` query findbyUid($uid: String!) { @@ -17,7 +17,7 @@ export const superProQuery = (target) => { //the different useMutation function goes here let querySchema = null; if (target === "user") { - querySchema = UPSERT_USER; + querySchema = FIND_USER_UID; } const callSuper = (key, prop) => { if (key === "user") {