Merge branch 'main' into contract-page

This commit is contained in:
jahooma 2021-12-10 00:33:11 -06:00
commit 7309096854
9 changed files with 34 additions and 40 deletions

View File

@ -1,10 +1,7 @@
module.exports = {
parser: '@typescript-eslint/parser',
extends: [
'plugin:react-hooks/recommended',
'plugin:@next/next/recommended',
],
extends: ['plugin:react-hooks/recommended', 'plugin:@next/next/recommended'],
rules: {
// Add or disable rules here.
}
// Add or disable rules here.
},
}

3
web/.prettierignore Normal file
View File

@ -0,0 +1,3 @@
# Ignore Next artifacts
.next/
out/

View File

@ -1,7 +1,7 @@
import { Popover } from '@headlessui/react'
import Link from 'next/link'
import { useEffect, useState } from 'react'
import { firebaseLogin, listenForLogin, User } from '../lib/firebase/users'
import { useUser } from '../hooks/use-user'
import { firebaseLogin } from '../lib/firebase/users'
const navigation = [
{
@ -12,12 +12,11 @@ const navigation = [
]
function SignInLink() {
const [user, setUser] = useState<User | null>(null)
useEffect(() => listenForLogin(setUser), [])
const user = useUser()
return (
<>
{user && user.id ? (
{user ? (
<Link href="/account">
<a className="text-base font-medium text-green-400 hover:text-gray-300">
{user.name}

View File

@ -2,12 +2,13 @@ import { useEffect, useState } from 'react'
import { Contract, listenForContract } from '../lib/firebase/contracts'
export const useContract = (contractId: string) => {
const [contract, setContract] = useState<Contract | null | 'loading'>('loading')
const [contract, setContract] = useState<Contract | null | 'loading'>(
'loading'
)
useEffect(() => {
if (contractId)
return listenForContract(contractId, setContract)
}, [contractId])
useEffect(() => {
if (contractId) return listenForContract(contractId, setContract)
}, [contractId])
return contract
}
return contract
}

View File

@ -2,11 +2,7 @@ import { useEffect, useState } from 'react'
import { listenForLogin, User } from '../lib/firebase/users'
export const useUser = () => {
const [user, setUser] = useState<User | null>(null)
useEffect(() => {
return listenForLogin(setUser)
}, [])
return user
}
const [user, setUser] = useState<User | null>(null)
useEffect(() => listenForLogin(setUser), [])
return user
}

View File

@ -7,7 +7,7 @@ const firebaseConfig = {
storageBucket: 'mantic-markets.appspot.com',
messagingSenderId: '128925704902',
appId: '1:128925704902:web:f61f86944d8ffa2a642dc7',
measurementId: '${config.measurementId}',
measurementId: 'G-SSFK1Q138D',
}
// Initialize Firebase

View File

@ -32,14 +32,14 @@ export async function setUser(userId: string, user: User) {
}
const CACHED_USER_KEY = 'CACHED_USER_KEY'
export function listenForLogin(onUser: (user: User) => void) {
export function listenForLogin(onUser: (_user: User | null) => void) {
// Immediately load any persisted user object from browser cache.
const cachedUser = localStorage.getItem(CACHED_USER_KEY)
if (cachedUser) {
onUser(JSON.parse(cachedUser))
}
onAuthStateChanged(auth, async (user) => {
return onAuthStateChanged(auth, async (user) => {
if (user) {
let fetchedUser = await getUser(user.uid)
if (!fetchedUser) {
@ -63,8 +63,8 @@ export function listenForLogin(onUser: (user: User) => void) {
// Note: Cap on localStorage size is ~5mb
localStorage.setItem(CACHED_USER_KEY, JSON.stringify(fetchedUser))
} else {
// User logged out; reset to the empty object
onUser({} as User)
// User logged out; reset to null
onUser(null)
}
})
}
@ -76,7 +76,6 @@ export async function firebaseLogin() {
export async function firebaseLogout() {
auth.signOut()
localStorage.removeItem(CACHED_USER_KEY)
}
const storage = getStorage(app)

View File

@ -11,12 +11,12 @@
"dependencies": {
"@headlessui/react": "1.4.2",
"@heroicons/react": "1.0.5",
"chart.js": "^3.6.1",
"daisyui": "^1.16.2",
"firebase": "^9.6.0",
"chart.js": "3.6.1",
"daisyui": "1.16.2",
"firebase": "9.6.0",
"next": "12.0.4",
"react": "17.0.2",
"react-chartjs-2": "^4.0.0",
"react-chartjs-2": "4.0.0",
"react-dom": "17.0.2"
},
"devDependencies": {

View File

@ -1,11 +1,10 @@
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
import { firebaseLogout, listenForLogin, User } from '../lib/firebase/users'
import { firebaseLogout } from '../lib/firebase/users'
import { Header } from '../components/header'
import { useUser } from '../hooks/use-user'
export default function Account() {
const [user, setUser] = useState<User | null>(null)
useEffect(() => listenForLogin(setUser), [])
const user = useUser()
const router = useRouter()
return (