Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW][TR] Add support for key value abstraction #169

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 3 additions & 25 deletions app/components/auth/firebase/ui/FirebaseLoginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down Expand Up @@ -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));
Expand Down
28 changes: 3 additions & 25 deletions app/components/auth/firebase/ui/FirebaseSignupForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down Expand Up @@ -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();
Expand Down
68 changes: 68 additions & 0 deletions app/components/superprofile/SuperMutate.js
Original file line number Diff line number Diff line change
@@ -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} ];
};
35 changes: 35 additions & 0 deletions app/components/superprofile/SuperQuery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { gql, 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 = FIND_USER_UID;
}
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} ];
};
25 changes: 12 additions & 13 deletions app/components/wallet/NFTprofile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 { superProMutate } from "../superprofile/SuperMutate";

const NFTProfile = ({ limit }) => {
const [assets, setAssets] = useState(null);
Expand Down Expand Up @@ -109,21 +99,30 @@ const GalleryModal = ({
errMess,
setErrMess,
}) => {
const [upsertNFT, { data, loading, error, reset }] = useMutation(UPSERT_NFT);
const [ superMutate, {data, loading, error, reset} ] = superProMutate("nft");

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,
};

superMutate("nft", data);
};

if (error) {
Expand Down
103 changes: 48 additions & 55 deletions app/pages/profile/[uid].js
Original file line number Diff line number Diff line change
@@ -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(
<>
<div className="my-3" style={{display:"flex", alignItems:"center", flexDirection:"column"}}>{
user?.photoURL ?
<img src={user.photoURL}
alt={user.displayName}
className="rounded-circle"
height="130px"
width="130px" />
:
if (error) console.log(error);

if (data?.findUserByUid) {
const user = data.findUserByUid;
return (
<>
<div
className="my-3"
style={{
display: "flex",
alignItems: "center",
flexDirection: "column",
}}
>
{user?.photoURL ? (
<img
src={user.photoURL}
alt={user.displayName}
className="rounded-circle"
height="130px"
width="130px"
/>
) : (
<NoUserAvatar name={user?.displayName} size="130" />
}
<h2 className="my-3">{user.displayName}</h2>
</div>
</>
)
}
)}
<h2 className="my-3">{user.displayName}</h2>
</div>
</>
);
}

return <></>
}
export default Profile
return <></>;
};
export default Profile;