Remove references to React context and replace with state where necessary
This commit is contained in:
parent
d1c602dd59
commit
a6b222000c
9 changed files with 31 additions and 46 deletions
|
|
@ -1,35 +1,25 @@
|
||||||
import React, { MouseEventHandler, useContext, useEffect, useState } from 'react'
|
import React, { MouseEventHandler, useContext, useEffect, useState } from 'react'
|
||||||
import { useCookies } from 'react-cookie'
|
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
|
import { useSnapshot } from 'valtio'
|
||||||
import AppContext from '~context/AppContext'
|
|
||||||
|
|
||||||
import * as AlertDialog from '@radix-ui/react-alert-dialog';
|
import * as AlertDialog from '@radix-ui/react-alert-dialog';
|
||||||
|
|
||||||
import Header from '~components/Header'
|
import Header from '~components/Header'
|
||||||
import Button from '~components/Button'
|
import Button from '~components/Button'
|
||||||
|
|
||||||
|
import { accountState } from '~utils/accountState'
|
||||||
|
import { appState } from '~utils/appState'
|
||||||
|
|
||||||
import { ButtonType } from '~utils/enums'
|
import { ButtonType } from '~utils/enums'
|
||||||
import CrossIcon from '~public/icons/Cross.svg'
|
import CrossIcon from '~public/icons/Cross.svg'
|
||||||
|
|
||||||
|
|
||||||
const BottomHeader = () => {
|
const BottomHeader = () => {
|
||||||
const { editable, setEditable, authenticated, setAuthenticated } = useContext(AppContext)
|
const account = useSnapshot(accountState)
|
||||||
|
const app = useSnapshot(appState)
|
||||||
const [username, setUsername] = useState(undefined)
|
|
||||||
const [cookies, _, removeCookie] = useCookies(['user'])
|
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (cookies.user) {
|
|
||||||
setAuthenticated(true)
|
|
||||||
setUsername(cookies.user.username)
|
|
||||||
} else {
|
|
||||||
setAuthenticated(false)
|
|
||||||
}
|
|
||||||
}, [cookies, setUsername, setAuthenticated])
|
|
||||||
|
|
||||||
function deleteTeam(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) {
|
function deleteTeam(event: React.MouseEvent<HTMLButtonElement, MouseEvent>) {
|
||||||
// TODO: Implement deleting teams
|
// TODO: Implement deleting teams
|
||||||
console.log("Deleting team...")
|
console.log("Deleting team...")
|
||||||
|
|
@ -42,7 +32,7 @@ const BottomHeader = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const rightNav = () => {
|
const rightNav = () => {
|
||||||
if (editable && router.route === '/p/[party]') {
|
if (app.party.editable && router.route === '/p/[party]') {
|
||||||
return (
|
return (
|
||||||
<AlertDialog.Root>
|
<AlertDialog.Root>
|
||||||
<AlertDialog.Trigger className="Button destructive">
|
<AlertDialog.Trigger className="Button destructive">
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
import './index.scss'
|
import React from 'react'
|
||||||
|
|
||||||
import React, { useContext } from 'react'
|
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
|
|
||||||
import LoginModal from '~components/LoginModal'
|
import LoginModal from '~components/LoginModal'
|
||||||
|
|
@ -12,6 +10,7 @@ import { useModal as useAboutModal } from '~utils/useModal'
|
||||||
|
|
||||||
import AboutModal from '~components/AboutModal'
|
import AboutModal from '~components/AboutModal'
|
||||||
|
|
||||||
|
import './index.scss'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
authenticated: boolean,
|
authenticated: boolean,
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
import React, { useContext, useState } from 'react'
|
import React, { useState } from 'react'
|
||||||
import { withCookies, Cookies } from 'react-cookie'
|
import { withCookies, Cookies } from 'react-cookie'
|
||||||
import { createPortal } from 'react-dom'
|
import { createPortal } from 'react-dom'
|
||||||
|
|
||||||
import AppContext from '~context/AppContext'
|
|
||||||
import api from '~utils/api'
|
import api from '~utils/api'
|
||||||
|
import { accountState } from '~utils/accountState'
|
||||||
|
|
||||||
import Button from '~components/Button'
|
import Button from '~components/Button'
|
||||||
import Fieldset from '~components/Fieldset'
|
import Fieldset from '~components/Fieldset'
|
||||||
|
|
@ -12,8 +12,6 @@ import Overlay from '~components/Overlay'
|
||||||
|
|
||||||
import './index.scss'
|
import './index.scss'
|
||||||
|
|
||||||
// import New from '../../../assets/new'
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
cookies: Cookies
|
cookies: Cookies
|
||||||
close: () => void
|
close: () => void
|
||||||
|
|
@ -28,8 +26,6 @@ interface ErrorMap {
|
||||||
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
||||||
|
|
||||||
const LoginModal = (props: Props) => {
|
const LoginModal = (props: Props) => {
|
||||||
const { setAuthenticated } = useContext(AppContext)
|
|
||||||
|
|
||||||
const emailInput: React.RefObject<HTMLInputElement> = React.createRef()
|
const emailInput: React.RefObject<HTMLInputElement> = React.createRef()
|
||||||
const passwordInput: React.RefObject<HTMLInputElement> = React.createRef()
|
const passwordInput: React.RefObject<HTMLInputElement> = React.createRef()
|
||||||
const form: React.RefObject<HTMLInputElement>[] = [emailInput, passwordInput]
|
const form: React.RefObject<HTMLInputElement>[] = [emailInput, passwordInput]
|
||||||
|
|
@ -102,7 +98,11 @@ const LoginModal = (props: Props) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
cookies.set('user', cookieObj, { path: '/'})
|
cookies.set('user', cookieObj, { path: '/'})
|
||||||
setAuthenticated(true)
|
accountState.account.authorized = true
|
||||||
|
accountState.account.user = {
|
||||||
|
id: cookieObj.user_id,
|
||||||
|
username: cookieObj.username
|
||||||
|
}
|
||||||
|
|
||||||
props.close()
|
props.close()
|
||||||
}, (error) => {
|
}, (error) => {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import React, { useContext } from 'react'
|
import React from 'react'
|
||||||
import './index.scss'
|
import './index.scss'
|
||||||
|
|
||||||
import { appState } from '~utils/appState'
|
import { appState } from '~utils/appState'
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
import React, { useContext, useState } from 'react'
|
import React, { useState } from 'react'
|
||||||
import { withCookies, Cookies } from 'react-cookie'
|
import { withCookies, Cookies } from 'react-cookie'
|
||||||
import { createPortal } from 'react-dom'
|
import { createPortal } from 'react-dom'
|
||||||
|
|
||||||
import AppContext from '~context/AppContext'
|
|
||||||
import api from '~utils/api'
|
import api from '~utils/api'
|
||||||
|
import { accountState } from '~utils/accountState'
|
||||||
|
|
||||||
import Button from '~components/Button'
|
import Button from '~components/Button'
|
||||||
import Fieldset from '~components/Fieldset'
|
import Fieldset from '~components/Fieldset'
|
||||||
|
|
@ -28,8 +28,6 @@ interface ErrorMap {
|
||||||
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
||||||
|
|
||||||
const SignupModal = (props: Props) => {
|
const SignupModal = (props: Props) => {
|
||||||
const { setAuthenticated } = useContext(AppContext)
|
|
||||||
|
|
||||||
const [formValid, setFormValid] = useState(false)
|
const [formValid, setFormValid] = useState(false)
|
||||||
const [errors, setErrors] = useState<ErrorMap>({
|
const [errors, setErrors] = useState<ErrorMap>({
|
||||||
username: '',
|
username: '',
|
||||||
|
|
@ -81,8 +79,12 @@ const SignupModal = (props: Props) => {
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
const cookies = props.cookies
|
const cookies = props.cookies
|
||||||
cookies.set('user', response.data.user, { path: '/'})
|
cookies.set('user', response.data.user, { path: '/'})
|
||||||
|
|
||||||
setAuthenticated(true)
|
accountState.account.authorized = true
|
||||||
|
accountState.account.user = {
|
||||||
|
id: response.data.user.id,
|
||||||
|
username: response.data.user.username
|
||||||
|
}
|
||||||
|
|
||||||
props.close()
|
props.close()
|
||||||
}, (error) => {
|
}, (error) => {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/* eslint-disable react-hooks/exhaustive-deps */
|
/* eslint-disable react-hooks/exhaustive-deps */
|
||||||
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react'
|
import React, { useCallback, useEffect, useMemo, useState } from 'react'
|
||||||
import { useCookies } from 'react-cookie'
|
import { useCookies } from 'react-cookie'
|
||||||
import { useSnapshot } from 'valtio'
|
import { useSnapshot } from 'valtio'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/* eslint-disable react-hooks/exhaustive-deps */
|
/* eslint-disable react-hooks/exhaustive-deps */
|
||||||
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react'
|
import React, { useCallback, useEffect, useMemo, useState } from 'react'
|
||||||
import { useCookies } from 'react-cookie'
|
import { useCookies } from 'react-cookie'
|
||||||
import { useSnapshot } from 'valtio'
|
import { useSnapshot } from 'valtio'
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,21 +4,15 @@ import { useState } from 'react'
|
||||||
import { CookiesProvider } from 'react-cookie'
|
import { CookiesProvider } from 'react-cookie'
|
||||||
|
|
||||||
import Layout from '~components/Layout'
|
import Layout from '~components/Layout'
|
||||||
import AppContext from '~context/AppContext'
|
|
||||||
|
|
||||||
import type { AppProps } from 'next/app'
|
import type { AppProps } from 'next/app'
|
||||||
|
|
||||||
function MyApp({ Component, pageProps }: AppProps) {
|
function MyApp({ Component, pageProps }: AppProps) {
|
||||||
const [authenticated, setAuthenticated] = useState(false)
|
|
||||||
const [editable, setEditable] = useState(false)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<CookiesProvider>
|
<CookiesProvider>
|
||||||
<AppContext.Provider value={{ authenticated, setAuthenticated, editable, setEditable }}>
|
<Layout>
|
||||||
<Layout>
|
<Component {...pageProps} />
|
||||||
<Component {...pageProps} />
|
</Layout>
|
||||||
</Layout>
|
|
||||||
</AppContext.Provider>
|
|
||||||
</CookiesProvider>
|
</CookiesProvider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import React, { useContext, useEffect, useState } from 'react'
|
import React from 'react'
|
||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
|
|
||||||
import Party from '~components/Party'
|
import Party from '~components/Party'
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue