Skip to content

Commit

Permalink
fix(frontend): logout from previous project when switching to a diffe…
Browse files Browse the repository at this point in the history
…rent project (#204)

* Handled confusion screens

* Add signout for home page

* Fixed index error in explore page

* Minor modifications

* Minor fixes

Co-authored-by: AshenI99 <[email protected]>
  • Loading branch information
AshenI99 and AshenI99 authored Jan 3, 2022
1 parent fa01318 commit d42bf05
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 24 deletions.
19 changes: 13 additions & 6 deletions examples/frontend/containers/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Head from 'next/head'
import { indexLoaderState } from '../state/recoil'
import { useRecoilValue } from 'recoil'
import { useFTMethod, useNearHooksContainer } from '@cura/hooks'
import { useEffect } from 'react'
import { useEffect, useState } from 'react'
import { useRouter } from 'next/router'
import { mapPathToProject } from 'utils/path-to-project'

Expand All @@ -26,11 +26,14 @@ export default function Layout({ children, project = 'share' }) {
{
account_id: accountId,
}
)
);

const [currentProject, setCurrentProject] = useState('');
const [signedProject, setSignedProject] = useState('');

const switchProject = () => {
const currentProject = mapPathToProject(router.asPath)
const signedProject = localStorage.getItem('contractAddress')
setCurrentProject(mapPathToProject(router.asPath));
setSignedProject(localStorage.getItem('contractAddress'))

if (
signedProject &&
Expand All @@ -41,8 +44,11 @@ export default function Layout({ children, project = 'share' }) {
}
}

const preSignIn = () => {
const preSignIn = async () => {
const project = mapPathToProject(router.asPath)
if(localStorage.getItem('contractAddress')){
await signOut();
}
localStorage.setItem('contractAddress', project)

signIn(
Expand Down Expand Up @@ -76,7 +82,7 @@ export default function Layout({ children, project = 'share' }) {
<Box style={{ minHeight: '100vh' }}>
<Header
base={project}
accountId={accountId}
accountId={currentProject == signedProject ? accountId : null}
onSignIn={preSignIn}
onSignOut={preSignOut}
title={project.toUpperCase()}
Expand All @@ -91,6 +97,7 @@ export default function Layout({ children, project = 'share' }) {
}}
>
<Menu
accountId={currentProject == signedProject ? accountId : null}
base={project}
nextLinkWrapper={(href, children) => (
<Link href={href}>{children}</Link>
Expand Down
6 changes: 3 additions & 3 deletions examples/frontend/containers/Menu.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// @ts-nocheck
/** @jsxImportSource theme-ui */

import { useNearHooksContainer } from '@cura/hooks'
import { Menu } from '@cura/components'

export default function MenuContainer({
accountId,
base,
nextLinkWrapper,
activeLink,
}: {
accountId: string
base: string
nextLinkWrapper: (link: string, children: JSX.Element) => JSX.Element
activeLink: string
}) {
const { accountId } = useNearHooksContainer()


return (
<Menu
accountId={accountId}
Expand Down
14 changes: 11 additions & 3 deletions examples/frontend/pages/cc/[project]/explore/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const Explore = () => {
)

// hook that contains all the logic of explore page
const { items, nextPage } = useNFTExplore(
const { items, nextPage, notLogged } = useNFTExplore(
contractAdress,
NFT_PER_PAGE
)
Expand All @@ -50,7 +50,7 @@ const Explore = () => {
project={project}
items={items}
loadMore={loadMore}
totalSupply={totalSupply}
totalSupply={notLogged ? 0 : totalSupply}
baseUrl={`/${project}/explore/`}
/>
)
Expand All @@ -62,6 +62,7 @@ function useNFTExplore(contractAdress: string, limitPerPage = 4) {
const [currentPage, setCurrentPage] = useState(0)
const [data, setData] = useState([])
const [isLoading, setIsLoading] = useState(true)
const [notLogged, setNotLogged] = useState(true)

const from_index = Math.floor(limitPerPage * currentPage)

Expand All @@ -81,13 +82,19 @@ function useNFTExplore(contractAdress: string, limitPerPage = 4) {

// Excute function if contract change or currentPage change
useEffect(() => {
let signedAddress = localStorage.getItem('contractAddress');

// make sure the user is logged in and the contract is the right contract
if (
contract?.account?.accountId &&
contract.contractId == contractAdress
contract.contractId == contractAdress &&
contract.contractId == signedAddress
) {
setIsLoading(true)
setNotLogged(false)
getData()
} else {
setNotLogged(true)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [contract, currentPage])
Expand All @@ -96,5 +103,6 @@ function useNFTExplore(contractAdress: string, limitPerPage = 4) {
items: data,
loading: isLoading,
nextPage: () => setCurrentPage(currentPage + 1),
notLogged: notLogged,
}
}
29 changes: 23 additions & 6 deletions examples/frontend/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { motion, AnimatePresence } from 'framer-motion'
import { ProjectCard } from '@cura/components'
import { useState, useEffect } from 'react'
import { useRouter } from 'next/router'
import { useNearHooksContainer } from '@cura/hooks'
import { mapPathToProject } from 'utils/path-to-project'

const slideDetails = [
{
Expand Down Expand Up @@ -75,6 +77,8 @@ const Index = () => {
const [pauseSlider, setPauseSlider] = useState(0)
const [loading, setLoading] = useState(true)

const { signOut } = useNearHooksContainer()


const slidify = (newDirection: number) => {
if (pauseSlider) return
Expand All @@ -94,6 +98,23 @@ const Index = () => {
setLoading(false)
}, [])

const handleClick = async (path, buttonType) =>{
if(localStorage.getItem('contractAddress') != mapPathToProject(path)){
localStorage.removeItem('contractAddress')
await signOut();
}

if(buttonType == 'create') {
router.push(
slideDetails[page].path + `/create`
)
} else {
router.push(
slideDetails[page].path + `/explore`
)
}
}

return (
<div
sx={{
Expand Down Expand Up @@ -150,14 +171,10 @@ const Index = () => {
description={slideDetails[page].description}
tags={slideDetails[page].tags}
onCreateClick={() =>
router.push(
slideDetails[page].path + `/create`
)
handleClick(slideDetails[page].path, 'create')
}
onExploreClick={() =>
router.push(
slideDetails[page].path + `/explore`
)
handleClick(slideDetails[page].path, 'explore')
}
/>
</motion.div>
Expand Down
14 changes: 11 additions & 3 deletions examples/frontend/pages/ml/[project]/explore/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const Explore = () => {
)

// hook that contains all the logic of explore page
const { items, nextPage } = useNFTExplore(contractAdress, NFT_PER_PAGE)
const { items, nextPage, notLogged } = useNFTExplore(contractAdress, NFT_PER_PAGE)

function loadMore() {
try {
Expand All @@ -47,7 +47,7 @@ const Explore = () => {
project={project}
items={items}
loadMore={loadMore}
totalSupply={totalSupply}
totalSupply={notLogged ? 0 : totalSupply}
baseUrl={`/${project}/explore/`}
type="image"
/>
Expand All @@ -60,6 +60,7 @@ function useNFTExplore(contractAdress: string, limitPerPage = 4) {
const [currentPage, setCurrentPage] = useState(0)
const [data, setData] = useState([])
const [isLoading, setIsLoading] = useState(true)
const [notLogged, setNotLogged] = useState(true)

const from_index = Math.floor(limitPerPage * currentPage)

Expand All @@ -79,13 +80,19 @@ function useNFTExplore(contractAdress: string, limitPerPage = 4) {

// Excute function if contract change or currentPage change
useEffect(() => {
let signedAddress = localStorage.getItem('contractAddress');

// make sure the user is logged in and the contract is the right contract
if (
contract?.account?.accountId &&
contract.contractId == contractAdress
contract.contractId == contractAdress &&
contract.contractId == signedAddress
) {
setIsLoading(true)
setNotLogged(false)
getData()
} else {
setNotLogged(true)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [contract, currentPage])
Expand All @@ -94,5 +101,6 @@ function useNFTExplore(contractAdress: string, limitPerPage = 4) {
items: data,
loading: isLoading,
nextPage: () => setCurrentPage(currentPage + 1),
notLogged: notLogged,
}
}
14 changes: 11 additions & 3 deletions examples/frontend/pages/share/explore/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const Explore = () => {
)

// hook that contains all the logic of explore page
const { items, nextPage } = useNFTExplore(contractAdress, NFT_PER_PAGE)
const { items, nextPage, notLogged } = useNFTExplore(contractAdress, NFT_PER_PAGE)

function loadMore() {
try {
Expand All @@ -47,7 +47,7 @@ const Explore = () => {
project={project}
items={items}
loadMore={loadMore}
totalSupply={totalSupply}
totalSupply={notLogged ? 0 : totalSupply}
baseUrl={`/${project}/explore/`}
/>
)
Expand All @@ -59,6 +59,7 @@ function useNFTExplore(contractAdress: string, limitPerPage = 4) {
const [currentPage, setCurrentPage] = useState(0)
const [data, setData] = useState([])
const [isLoading, setIsLoading] = useState(true)
const [notLogged, setNotLogged] = useState(true)

const from_index = Math.floor(limitPerPage * currentPage)

Expand All @@ -78,13 +79,19 @@ function useNFTExplore(contractAdress: string, limitPerPage = 4) {

// Excute function if contract change or currentPage change
useEffect(() => {
let signedAddress = localStorage.getItem('contractAddress');

// make sure the user is logged in and the contract is the right contract
if (
contract?.account?.accountId &&
contract.contractId == contractAdress
contract.contractId == contractAdress &&
contract.contractId == signedAddress
) {
setNotLogged(false)
setIsLoading(true)
getData()
} else {
setNotLogged(true)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [contract, currentPage])
Expand All @@ -93,5 +100,6 @@ function useNFTExplore(contractAdress: string, limitPerPage = 4) {
items: data,
loading: isLoading,
nextPage: () => setCurrentPage(currentPage + 1),
notLogged: notLogged,
}
}

1 comment on commit d42bf05

@vercel
Copy link

@vercel vercel bot commented on d42bf05 Jan 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.