From 01a1876b3db89b1e8470976d6bec6c93ab8a8735 Mon Sep 17 00:00:00 2001 From: Elijah Lucian Date: Thu, 15 Jul 2021 23:06:36 -0600 Subject: [PATCH] motorin along --- frontend/src/CoreLayout.tsx | 17 ++-- frontend/src/contexts/AppContext.tsx | 3 +- frontend/src/contexts/UserContext.tsx | 43 +++++--- frontend/src/elements/FormItem/index.tsx | 8 ++ frontend/src/elements/Input/index.tsx | 7 ++ frontend/src/elements/InputNumber/index.tsx | 7 ++ frontend/src/forms/AccountForm.tsx | 2 +- frontend/src/forms/UserForm.tsx | 99 +++++-------------- frontend/src/hooks/getMany/useAccounts.ts | 2 +- frontend/src/hooks/getMany/useStacks.ts | 2 +- frontend/src/hooks/getMany/useTransactions.ts | 2 +- frontend/src/hooks/util/useGet.ts | 4 +- frontend/src/layout/AppFooter/index.tsx | 2 +- frontend/src/layout/AppHeader/index.tsx | 47 ++++----- frontend/src/layout/AppHeader/style.scss | 17 +++- frontend/src/layout/Modal/index.tsx | 9 ++ frontend/src/layout/Modal/style.scss | 16 +++ frontend/src/pages/Dashboard/index.tsx | 2 +- frontend/src/pages/NewTransaction/index.tsx | 62 ------------ frontend/src/pages/NewTransaction/style.scss | 50 ---------- frontend/src/pages/NewUser/index.tsx | 16 +-- frontend/src/pages/TransactionForm/index.tsx | 55 +++++++++++ frontend/src/types/index.ts | 14 ++- 23 files changed, 236 insertions(+), 250 deletions(-) create mode 100644 frontend/src/elements/FormItem/index.tsx create mode 100644 frontend/src/elements/Input/index.tsx create mode 100644 frontend/src/elements/InputNumber/index.tsx create mode 100644 frontend/src/layout/Modal/index.tsx create mode 100644 frontend/src/layout/Modal/style.scss delete mode 100644 frontend/src/pages/NewTransaction/index.tsx delete mode 100644 frontend/src/pages/NewTransaction/style.scss create mode 100644 frontend/src/pages/TransactionForm/index.tsx diff --git a/frontend/src/CoreLayout.tsx b/frontend/src/CoreLayout.tsx index 52cc4ee..d1a2f9c 100644 --- a/frontend/src/CoreLayout.tsx +++ b/frontend/src/CoreLayout.tsx @@ -1,6 +1,6 @@ import { useUserContext } from './contexts/UserContext' import { Route, Switch } from 'react-router' -import { Link } from 'react-router-dom' +import { Link, useHistory } from 'react-router-dom' import { Dashboard } from './pages/Dashboard' import { NewUser } from './pages/NewUser' import { TransactionList } from './pages/TransactionList' @@ -8,29 +8,32 @@ import { AccountForm } from './forms/AccountForm' import { Login } from './pages/Login' import { AppHeader } from './layout/AppHeader' import { AppFooter } from './layout/AppFooter' +import { TransactionForm } from './pages/TransactionForm' +import { Button } from 'antd' export const CoreLayout = () => { const { user } = useUserContext() + const history = useHistory() return (
- Home - Select Budget - Budget Details + Accounts + Transactions - New User +
{!user ? ( - + +
loading...
) : ( - + diff --git a/frontend/src/contexts/AppContext.tsx b/frontend/src/contexts/AppContext.tsx index be6c32f..2f1530b 100644 --- a/frontend/src/contexts/AppContext.tsx +++ b/frontend/src/contexts/AppContext.tsx @@ -12,6 +12,7 @@ type AppContextInterface = { post: (path: string, body: Partial) => Promise create: (path: string, body: Partial) => Promise destroy: (path: string) => Promise + baseURL?: string } const AppContext = createContext(null) @@ -52,7 +53,7 @@ export const AppContextProvider = ({ children, baseURL }: Props) => { } return ( - + {children} ) diff --git a/frontend/src/contexts/UserContext.tsx b/frontend/src/contexts/UserContext.tsx index c7495ef..3aa6c92 100644 --- a/frontend/src/contexts/UserContext.tsx +++ b/frontend/src/contexts/UserContext.tsx @@ -3,7 +3,8 @@ import { message } from 'antd' import { useHistory } from 'react-router' import { User } from '../types' import { useAppContext } from './AppContext' -import { logOut } from '../api' +import { useEffect } from 'react' +import { useRef } from 'react' type Props = { children: React.ReactNode @@ -18,14 +19,36 @@ type Context = { const UserContext = createContext(null) export const UserContextProvider = ({ children }: Props) => { - const api = useAppContext() - const history = useHistory() + const api = useRef(useAppContext()) + const history = useRef(useHistory()) const [user, setUser] = useState(null) + useEffect(() => { + const get = async () => { + try { + const token = window.localStorage.getItem('cash-stacks-token') + if (!token) throw new Error() + + // const valid = await api.current.post('/dj-rest-auth/token/verify/', { + // token, + // }) + // console.log('token valid?', token) + const user = await api.current.get(`/dj-rest-auth/user/`) + setUser(user) + } catch (err) { + console.log(`couldn't get user, token possibly expired`) + // window.localStorage.removeItem('cash-stacks-token') + history.current.push('/login') + } + } + + get() + }, []) + const handleLogin = async (username: string, password: string) => { try { - const { key } = await api.post( + const { key } = await api.current.post( '/dj-rest-auth/login/', { username, @@ -34,24 +57,18 @@ export const UserContextProvider = ({ children }: Props) => { ) if (!key) throw new Error('Problem logging in!') window.localStorage.setItem('cash-stacks-token', key) - const user = await api.get(`/dj-rest-auth/user/`) - + const user = await api.current.get(`/dj-rest-auth/user/`) console.log('user', user) - - if (!user) message.error(`Couldn't find user`) setUser(user) - - message.success(`logged in as ${user?.name}`, 0.5) } catch (err) { message.error('Login Failed!') } } const handleLogout = async () => { - await logOut() + await api.current.post('/dj-rest-auth/logout/', {}) setUser(null) - message.success('logged out!', 0.5) - history.push('/') + history.current.push('/') } return ( diff --git a/frontend/src/elements/FormItem/index.tsx b/frontend/src/elements/FormItem/index.tsx new file mode 100644 index 0000000..05c853d --- /dev/null +++ b/frontend/src/elements/FormItem/index.tsx @@ -0,0 +1,8 @@ +import { FormItemProps } from 'antd' +import AntFormItem from 'antd/lib/form/FormItem' + +type Props = FormItemProps + +export const FormItem = (props: Props) => { + return +} diff --git a/frontend/src/elements/Input/index.tsx b/frontend/src/elements/Input/index.tsx new file mode 100644 index 0000000..83acb58 --- /dev/null +++ b/frontend/src/elements/Input/index.tsx @@ -0,0 +1,7 @@ +import { Input as AntInput, InputProps } from 'antd' + +type Props = InputProps + +export const Input = (props: Props) => { + return +} diff --git a/frontend/src/elements/InputNumber/index.tsx b/frontend/src/elements/InputNumber/index.tsx new file mode 100644 index 0000000..8cbc351 --- /dev/null +++ b/frontend/src/elements/InputNumber/index.tsx @@ -0,0 +1,7 @@ +import { InputNumber as AntInputNumber, InputNumberProps } from 'antd' + +type Props = InputNumberProps + +export const InputNumber = (props: Props) => { + return +} diff --git a/frontend/src/forms/AccountForm.tsx b/frontend/src/forms/AccountForm.tsx index c8b125c..c9e32e5 100644 --- a/frontend/src/forms/AccountForm.tsx +++ b/frontend/src/forms/AccountForm.tsx @@ -9,7 +9,7 @@ type Props = { } export const AccountForm = ({ account }: Props) => { - const stacks = useStacks('') + const stacks = useStacks() const [name, setName] = useState(account?.name || '') const [details, setDetails] = useState(account?.details || '') diff --git a/frontend/src/forms/UserForm.tsx b/frontend/src/forms/UserForm.tsx index 1d886a8..b7c2b25 100644 --- a/frontend/src/forms/UserForm.tsx +++ b/frontend/src/forms/UserForm.tsx @@ -1,88 +1,39 @@ -import { useState, useEffect } from 'react' - -import { Password, User } from '../types' +import { User } from '../types' import { useUserContext } from '../contexts/UserContext' -import { message } from 'antd' import { Button } from '../elements/Button' +import { Form } from '../elements/Form' +import { useForm } from 'antd/lib/form/Form' +import { FormItem } from '../elements/FormItem' +import { Input } from '../elements/Input' export const UserForm = () => { const { user } = useUserContext() + if (!user) throw new Error('User should be gotten by this point') - console.log(user) + const [form] = useForm() - const [name, setName] = useState(user?.name) - const [email, setEmail] = useState(user?.email) - const [password, setPassword] = useState('') - const [passwordConfirm, setPasswordConfirm] = useState('') - const [valid, setValid] = useState(false) - - useEffect(() => { - passwordConfirm.length > 4 && password === passwordConfirm && name && email - ? setValid(true) - : setValid(false) - }, [password, passwordConfirm, name, email]) - - // @ts-ignore - const handleSubmit = async (e) => { - e.preventDefault() - - if (!name || !email) { - message.error('Name and email required!') - return - } - - const body: Omit & Password = { - name, - email, - password, - passwordConfirm, - } - - try { - // user?.id - // ? await api.updateUser(user.id, body) - // : await api.createUser(body) - console.log('User form wtf') - // if (!user?.id) history.push('/login') - } catch (err) { - message.error('Something went wrong') - } + const handleFinish = (values: User) => { + console.log(values) } return ( <> -

{user?.id ? 'Edit' : 'Create'} Profile

-
- - - - - -
+
+

User Profile

+ + + + + + + + + + + + + +
) } diff --git a/frontend/src/hooks/getMany/useAccounts.ts b/frontend/src/hooks/getMany/useAccounts.ts index 3b9cb4b..7b99941 100644 --- a/frontend/src/hooks/getMany/useAccounts.ts +++ b/frontend/src/hooks/getMany/useAccounts.ts @@ -1,4 +1,4 @@ import { Account } from '../../types' import { useGet } from '../util/useGet' -export const useAccounts = () => useGet(`/accounts`) +export const useAccounts = () => useGet(`/accounts/`) diff --git a/frontend/src/hooks/getMany/useStacks.ts b/frontend/src/hooks/getMany/useStacks.ts index 4c5dab7..81477d5 100644 --- a/frontend/src/hooks/getMany/useStacks.ts +++ b/frontend/src/hooks/getMany/useStacks.ts @@ -1,4 +1,4 @@ import { Stack } from '../../types' import { useGet } from '../util/useGet' -export const useStacks = (accountId: string) => useGet(`/stacks`) +export const useStacks = () => useGet(`/stacks/`) diff --git a/frontend/src/hooks/getMany/useTransactions.ts b/frontend/src/hooks/getMany/useTransactions.ts index 8fb00a9..031faa3 100644 --- a/frontend/src/hooks/getMany/useTransactions.ts +++ b/frontend/src/hooks/getMany/useTransactions.ts @@ -2,4 +2,4 @@ import { Transaction } from '../../types' import { useGet } from '../util/useGet' export const useTransactions = (accountId: string) => - useGet(`/accounts/${accountId}/transactions`) + useGet(`/transactions/`) diff --git a/frontend/src/hooks/util/useGet.ts b/frontend/src/hooks/util/useGet.ts index 1b3c4e6..bab732c 100644 --- a/frontend/src/hooks/util/useGet.ts +++ b/frontend/src/hooks/util/useGet.ts @@ -8,8 +8,8 @@ export const useGet = (path: string) => { const get = useCallback(async () => { const data = await appContext.current.get(path) if (!data) return - - setData(data) + // @ts-ignore + setData(data.results) }, [path]) useEffect(() => { diff --git a/frontend/src/layout/AppFooter/index.tsx b/frontend/src/layout/AppFooter/index.tsx index 58f00cd..b72cada 100644 --- a/frontend/src/layout/AppFooter/index.tsx +++ b/frontend/src/layout/AppFooter/index.tsx @@ -7,7 +7,7 @@ export const AppFooter = () => { return (