diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..e69de29 diff --git a/.eslintrc.js b/.eslintrc.js index 64dee9b..6bad519 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,7 +1,10 @@ module.exports = { extends: ['airbnb-typescript-prettier'], + ignorePatterns: ['.eslintrc.js'], // Rules can be here to override the preset of eslint from airbnb, if they are too strict. rules: { + camelcase: 'warn', + '@typescript-eslint/no-empty-function': 'warn', '@typescript-eslint/ban-ts-comment': 'off', '@typescript-eslint/no-explicit-any': 'off', '@typescript-eslint/explicit-function-return-type': 'off', @@ -22,6 +25,8 @@ module.exports = { 'no-console': 'off', 'react/destructuring-assignment': 0, + 'react/jsx-props-no-spreading': 'off', + 'import/prefer-default-export': 0, // 'react/prop-types': 0, -> this is an example }, overrides: [ diff --git a/package-lock.json b/package-lock.json index fe60949..e020b20 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1273,6 +1273,11 @@ "@hapi/hoek": "^8.3.0" } }, + "@hookform/resolvers": { + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/@hookform/resolvers/-/resolvers-1.3.4.tgz", + "integrity": "sha512-K56VLSInXNIT/r14pkzRn1FJclqzGOWqpe3Bf0kz2Hf98ZOmRRFh4fhB7F3ofqCQ03CEQQkV44CTg7ql6nEvEg==" + }, "@istanbuljs/load-nyc-config": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz", @@ -2026,6 +2031,11 @@ "resolved": "https://registry.npmjs.org/@types/json5/-/json5-0.0.29.tgz", "integrity": "sha1-7ihweulOEdK4J7y+UnC86n8+ce4=" }, + "@types/lodash": { + "version": "4.14.168", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.168.tgz", + "integrity": "sha512-oVfRvqHV/V6D1yifJbVRU3TMp8OT6o6BG+U9MkwuJ3U8/CsDHvalRpsxBqivn71ztOFZBTfJMvETbqHiaNSj7Q==" + }, "@types/minimatch": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.3.tgz", @@ -2872,6 +2882,14 @@ "resolved": "https://registry.npmjs.org/axe-core/-/axe-core-4.1.1.tgz", "integrity": "sha512-5Kgy8Cz6LPC9DJcNb3yjAXTu3XihQgEdnIg50c//zOC/MyLP0Clg+Y8Sh9ZjjnvBrDZU4DgXS9C3T9r4/scGZQ==" }, + "axios": { + "version": "0.21.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", + "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", + "requires": { + "follow-redirects": "^1.10.0" + } + }, "axobject-query": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-2.2.0.tgz", @@ -7638,9 +7656,9 @@ "integrity": "sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw==" }, "immer": { - "version": "7.0.9", - "resolved": "https://registry.npmjs.org/immer/-/immer-7.0.9.tgz", - "integrity": "sha512-Vs/gxoM4DqNAYR7pugIxi0Xc8XAun/uy7AQu4fLLqaTBHxjOP9pJ266Q9MWA/ly4z6rAFZbvViOtihxUZ7O28A==" + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/immer/-/immer-8.0.1.tgz", + "integrity": "sha512-aqXhGP7//Gui2+UrEtvxZxSquQVXTpZ7KDxfCcKAF3Vysvw0CViVaW9RZ1j1xlIYqaaaipBoqdqeibkc18PNvA==" }, "import-cwd": { "version": "2.1.0", @@ -10031,6 +10049,11 @@ "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.2.tgz", "integrity": "sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ==" }, + "nanoclone": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/nanoclone/-/nanoclone-0.2.1.tgz", + "integrity": "sha512-wynEP02LmIbLpcYw8uBKpcfF6dmg2vcpKqxeH5UcoKEYdExslsdUA4ugFauuaeYdTB76ez6gJW8XAZ6CgkXYxA==" + }, "nanoid": { "version": "3.1.20", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.20.tgz", @@ -10585,9 +10608,9 @@ } }, "open": { - "version": "7.4.0", - "resolved": "https://registry.npmjs.org/open/-/open-7.4.0.tgz", - "integrity": "sha512-PGoBCX/lclIWlpS/R2PQuIR4NJoXh6X5AwVzE7WXnWRGvHg7+4TBCgsujUgiPpm0K1y4qvQeWnCWVTpTKZBtvA==", + "version": "7.4.2", + "resolved": "https://registry.npmjs.org/open/-/open-7.4.2.tgz", + "integrity": "sha512-MVHddDVweXZF3awtlAS+6pgKLlm/JgxZ90+/NBurBoQctVOOB/zDdVjcyPzQ+0laDGbsWgrRkflI65sQeOgT9Q==", "requires": { "is-docker": "^2.0.0", "is-wsl": "^2.1.1" @@ -12289,6 +12312,11 @@ "react-is": "^16.8.1" } }, + "property-expr": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/property-expr/-/property-expr-2.0.4.tgz", + "integrity": "sha512-sFPkHQjVKheDNnPvotjQmm3KD3uk1fWKUN7CrpdbwmUx3CrG3QiM8QpTSimvig5vTXmTvjz7+TDvXOI9+4rkcg==" + }, "proxy-addr": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz", @@ -12473,9 +12501,9 @@ } }, "react-dev-utils": { - "version": "11.0.1", - "resolved": "https://registry.npmjs.org/react-dev-utils/-/react-dev-utils-11.0.1.tgz", - "integrity": "sha512-rlgpCupaW6qQqvu0hvv2FDv40QG427fjghV56XyPcP5aKtOAPzNAhQ7bHqk1YdS2vpW1W7aSV3JobedxuPlBAA==", + "version": "11.0.3", + "resolved": "https://registry.npmjs.org/react-dev-utils/-/react-dev-utils-11.0.3.tgz", + "integrity": "sha512-4lEA5gF4OHrcJLMUV1t+4XbNDiJbsAWCH5Z2uqlTqW6dD7Cf5nEASkeXrCI/Mz83sI2o527oBIFKVMXtRf1Vtg==", "requires": { "@babel/code-frame": "7.10.4", "address": "1.1.2", @@ -12490,13 +12518,13 @@ "global-modules": "2.0.0", "globby": "11.0.1", "gzip-size": "5.1.1", - "immer": "7.0.9", + "immer": "8.0.1", "is-root": "2.1.0", "loader-utils": "2.0.0", "open": "^7.0.2", "pkg-up": "3.1.0", "prompts": "2.4.0", - "react-error-overlay": "^6.0.8", + "react-error-overlay": "^6.0.9", "recursive-readdir": "2.2.2", "shell-quote": "1.7.2", "strip-ansi": "6.0.0", @@ -12614,15 +12642,20 @@ } }, "react-error-overlay": { - "version": "6.0.8", - "resolved": "https://registry.npmjs.org/react-error-overlay/-/react-error-overlay-6.0.8.tgz", - "integrity": "sha512-HvPuUQnLp5H7TouGq3kzBeioJmXms1wHy9EGjz2OURWBp4qZO6AfGEcnxts1D/CbwPLRAgTMPCEgYhA3sEM4vw==" + "version": "6.0.9", + "resolved": "https://registry.npmjs.org/react-error-overlay/-/react-error-overlay-6.0.9.tgz", + "integrity": "sha512-nQTTcUu+ATDbrSD1BZHr5kgSD4oF8OFjxun8uAaL8RwPBacGBNPf/yAuVVdx17N8XNzRDMrZ9XcKZHCjPW+9ew==" }, "react-fast-compare": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/react-fast-compare/-/react-fast-compare-3.2.0.tgz", "integrity": "sha512-rtGImPZ0YyLrscKI9xTpV8psd6I8VAtjKCzQDlzyDvqJA8XOW78TXYQwNRNd8g8JZnDu8q9Fu/1v4HPAVwVdHA==" }, + "react-hook-form": { + "version": "6.15.1", + "resolved": "https://registry.npmjs.org/react-hook-form/-/react-hook-form-6.15.1.tgz", + "integrity": "sha512-bL0LQuQ3OlM3JYfbacKtBPLOHhmgYz8Lj6ivMrvu2M6e1wnt4sbGRtPEPYCc/8z3WDbjrMwfAfLX92OsB65pFA==" + }, "react-icons": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/react-icons/-/react-icons-4.2.0.tgz", @@ -15145,6 +15178,11 @@ "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==" }, + "toposort": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/toposort/-/toposort-2.0.2.tgz", + "integrity": "sha1-riF2gXXRVZ1IvvNUILL0li8JwzA=" + }, "tough-cookie": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-3.0.1.tgz", @@ -15317,9 +15355,9 @@ } }, "typescript": { - "version": "4.1.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz", - "integrity": "sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==" + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.2.2.tgz", + "integrity": "sha512-tbb+NVrLfnsJy3M59lsDgrzWIflR4d4TIUjz+heUnHZwdF7YsrMTKoRERiIvI2lvBG95dfpLxB21WZhys1bgaQ==" }, "unicode-canonical-property-names-ecmascript": { "version": "1.0.4", @@ -17175,6 +17213,20 @@ "version": "0.1.0", "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==" + }, + "yup": { + "version": "0.32.8", + "resolved": "https://registry.npmjs.org/yup/-/yup-0.32.8.tgz", + "integrity": "sha512-SZulv5FIZ9d5H99EN5tRCRPXL0eyoYxWIP1AacCrjC9d4DfP13J1dROdKGfpfRHT3eQB6/ikBl5jG21smAfCkA==", + "requires": { + "@babel/runtime": "^7.10.5", + "@types/lodash": "^4.14.165", + "lodash": "^4.17.20", + "lodash-es": "^4.17.11", + "nanoclone": "^0.2.1", + "property-expr": "^2.0.4", + "toposort": "^2.0.2" + } } } } diff --git a/package.json b/package.json index 4180365..09b83f1 100644 --- a/package.json +++ b/package.json @@ -3,22 +3,26 @@ "version": "0.1.0", "private": true, "dependencies": { + "@hookform/resolvers": "^1.3.4", "@testing-library/jest-dom": "^5.11.4", "@testing-library/react": "^11.1.0", "@testing-library/user-event": "^12.1.10", + "axios": "^0.21.1", "brace": "^0.11.1", "jsoneditor": "^9.1.9", "jsoneditor-react": "^3.1.0", "node-sass": "^4.14.1", "react": "^17.0.1", "react-dom": "^17.0.1", + "react-hook-form": "^6.15.1", "react-icons": "^4.2.0", "react-router-dom": "^5.2.0", "react-scripts": "4.0.1", "semantic-ui-css": "^2.4.1", "semantic-ui-react": "^2.0.3", - "typescript": "^4.1.3", - "web-vitals": "^0.2.4" + "typescript": "^4.2.2", + "web-vitals": "^0.2.4", + "yup": "^0.32.8" }, "scripts": { "start": "react-scripts start", @@ -71,7 +75,7 @@ ] }, "lint-staged": { - "*.{js,tsx}": "eslint --fix" + "*.{js,ts,tsx}": "eslint --fix" }, "husky": { "hooks": { diff --git a/src/App.tsx b/src/App.tsx index ed84c33..60383bb 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,21 +1,32 @@ -import React from 'react'; -import { BrowserRouter as Router, Switch, Route } from 'react-router-dom'; +import React, { useState } from 'react'; +import { BrowserRouter as Router, Redirect, Route, Switch } from 'react-router-dom'; import BaseLayout from './components/layouts/BaseLayout'; -import IpfsList from './pages/ipfs/IpfsList'; -import Ipfs from './pages/ipfs/Ipfs'; - +import Login from './components/pages/auth/Login'; +import Page from './components/Page'; +import { ERROR_404_PAGE, LOGIN_PAGE, USER_PAGES } from './components/urls'; +import Error404 from './components/pages/errors/Error404'; +import UserContext, { emptyUser } from './state/user'; import './App.scss'; function App(): JSX.Element { + const [user, setUser] = useState({ ...emptyUser }); + + const login = (email: string): void => setUser({ email, isLoggedIn: true }); + const logout = (): void => setUser({ ...emptyUser }); + return ( - - - - - - - - + + + + + + + + } /> + + + + ); } diff --git a/src/api/api.ts b/src/api/api.ts new file mode 100644 index 0000000..98561c4 --- /dev/null +++ b/src/api/api.ts @@ -0,0 +1,70 @@ +import axios from 'axios'; + +const OBTAIN_TOKEN = '/token/obtain/'; +// const REFRESH_TOKEN_URL = '/token/refresh/'; + +export const ACCESS_TOKEN = 'access_token'; +export const REFRESH_TOKEN = 'refresh_token'; + +interface JWTTokenResponse { + refresh: string; + access: string; +} + +export const axiosInstance = axios.create({ + baseURL: 'http://127.0.0.1:7424/api/', + timeout: 5000, + headers: { + // 'Authorization': localStorage.getItem('access_token') ? 'JWT ' + localStorage.getItem('access_token') : null, + 'Content-Type': 'application/json', + accept: 'application/json', + }, +}); + +export const saveTokens = (jwtToken: JWTTokenResponse): JWTTokenResponse => { + localStorage.setItem(ACCESS_TOKEN, jwtToken.access); + localStorage.setItem(REFRESH_TOKEN, jwtToken.refresh); + return jwtToken; +}; + +const localGet = (key: string, defaultValue = ''): string => { + const value = localStorage.getItem(key); + if (value === null) return defaultValue; + return value; +}; + +export const getAccessToken = (): string => localGet(ACCESS_TOKEN); +export const getRefreshToken = (): string => localGet(REFRESH_TOKEN); + +axiosInstance.interceptors.response.use( + (res) => res, + (err) => { + // const originalRequest = err.config; + // If refresh tokens is expired redirect to login page + // if (err.response.status === 401 && originalRequest.url === REFRESH_TOKEN_URL) { + // window.location.href = '/login/'; + // return Promise.reject(err); + // } + + // // If access token is expired update it + // if (err.response.status === 401 && err.response.statusText === 'Unauthorized') { + // return axiosInstance + // .post(REFRESH_TOKEN_URL, {refresh: localStorage.getItem(REFRESH_TOKEN)}) + // .then(res => res.data) + // .then(saveTokens) + // .then(res => { + // axiosInstance.defaults.headers['Authorization'] = 'JWT ' + res.access; + // originalRequest.headers['Authorization'] = 'JWT ' + res.access; + + // return axiosInstance(originalRequest); + // }) + // } + + return Promise.reject(err); + }, +); + +export const obtainTokenApi = async (email: string, password: string): Promise => + axiosInstance + .post(OBTAIN_TOKEN, { email, password }) + .then((res) => res.data); diff --git a/src/api/auth/auth.ts b/src/api/auth/auth.ts new file mode 100644 index 0000000..7c4cd48 --- /dev/null +++ b/src/api/auth/auth.ts @@ -0,0 +1,24 @@ +// import { axiosInstance, obtainTokenApi, saveTokens } from "../api" + +// const LOGIN_URL = ''; + +export interface UserAuthenticationData { + email: string; + password: string; +} + +const loginUser = (data: UserAuthenticationData): Promise => { + const { email, password } = data; + + if (email !== 'test@test.com') throw new Error('Email does not exist!'); + if (password !== 'geslo123') throw new Error('Password is incorrect!'); + // axiosInstance.post<{}>(LOGIN_URL, data) + // .then((res) => res.data); + return Promise.resolve(); +}; + +export const authenticateUser = async (data: UserAuthenticationData): Promise => { + await loginUser(data); + // const jwtTokens = await obtainTokenApi(data.email, data.password); + // saveTokens(jwtTokens); +}; diff --git a/src/components/Page.tsx b/src/components/Page.tsx new file mode 100644 index 0000000..b518d41 --- /dev/null +++ b/src/components/Page.tsx @@ -0,0 +1,21 @@ +import React, { useContext } from 'react'; +import { Redirect, Route, Switch } from 'react-router-dom'; +import Ipfs from '../pages/ipfs/Ipfs'; +import IpfsList from '../pages/ipfs/IpfsList'; +import UserContext from '../state/user'; +import { IPFS_BASE_PAGE, IPFS_PAGE, LOGIN_PAGE } from './urls'; + +const Page = (): JSX.Element => { + const { user } = useContext(UserContext); + + if (!user.isLoggedIn) return ; + + return ( + + + + + ); +}; + +export default Page; diff --git a/src/components/layouts/BaseLayout/BaseLayout.tsx b/src/components/layouts/BaseLayout/BaseLayout.tsx index 9d43d7c..d36ee55 100644 --- a/src/components/layouts/BaseLayout/BaseLayout.tsx +++ b/src/components/layouts/BaseLayout/BaseLayout.tsx @@ -1,19 +1,15 @@ import React from 'react'; import { Container } from 'semantic-ui-react'; -import NavigationBar from '../NavigationBar'; +import { NavigationBar } from '../NavigationBar'; -interface BaseLayoutProps { - children: any; -} +const BaseLayout: React.FC<{}> = ({ children }) => ( +
+ -export default function BaseLayout(props: BaseLayoutProps): JSX.Element { - return ( -
- + + {children} + +
+); - - {props.children} - -
- ); -} +export default BaseLayout; diff --git a/src/components/layouts/NavigationBar/NavigationBar.tsx b/src/components/layouts/NavigationBar/NavigationBar.tsx index 8083009..f967549 100644 --- a/src/components/layouts/NavigationBar/NavigationBar.tsx +++ b/src/components/layouts/NavigationBar/NavigationBar.tsx @@ -1,34 +1,41 @@ -import React from 'react'; +import React, { useContext } from 'react'; import { FaUserCircle } from 'react-icons/fa'; import { Link } from 'react-router-dom'; - +import UserContext from '../../../state/user'; +import { HOME_PAGE, IPFS_BASE_PAGE } from '../../urls'; import './NavigationBar.scss'; const NavigationBar = (): JSX.Element => { + const { user, logout } = useContext(UserContext); + return (
); diff --git a/src/components/layouts/NavigationBar/index.ts b/src/components/layouts/NavigationBar/index.ts new file mode 100644 index 0000000..d4fd1e8 --- /dev/null +++ b/src/components/layouts/NavigationBar/index.ts @@ -0,0 +1 @@ +export { default as NavigationBar } from './NavigationBar'; diff --git a/src/components/layouts/NavigationBar/index.tsx b/src/components/layouts/NavigationBar/index.tsx deleted file mode 100644 index 513a881..0000000 --- a/src/components/layouts/NavigationBar/index.tsx +++ /dev/null @@ -1,3 +0,0 @@ -import NavigationBar from './NavigationBar'; - -export default NavigationBar; diff --git a/src/components/pages/auth/Login.scss b/src/components/pages/auth/Login.scss new file mode 100644 index 0000000..d12c6ab --- /dev/null +++ b/src/components/pages/auth/Login.scss @@ -0,0 +1,83 @@ +$elevation-card: 0 0 1px 0 rgba(8, 11, 14, 0.6), 0 16px 16px -1px rgba(8, 11, 14, 0.1); +$color-secondary: #e1e4e8; +$error-color: red; + +.login { + &__card { + max-width: 400px; + margin: auto; + box-shadow: $elevation-card; + padding: 30px; + background: white; + } + + &__title { + font-size: 22px; + line-height: 32px; + font-weight: 500; + text-align: center; + margin: 8px 0 20px; + font-weight: bold; + } + + &__input-container { + position: relative; + margin-top: 20px; + } + + &__input { + border-radius: 100px; + border: 1px solid $color-secondary; + font-size: 14px; + line-height: 24px; + font-weight: 500; + height: 40px; + width: 100%; + outline: none; + padding: 0 15px; + + &--error { + border-color: $error-color; + } + } + + &__input-error-msg { + font-size: 12px; + margin-left: 15px; + color: $error-color; + } + + &__input-icon { + position: absolute; + right: 10px; + top: 50%; + transform: translateY(-50%); + background: transparent; + border: none; + outline: none; + display: inline-block; + padding: 10px; + color: $color-secondary; + } + + &__password-icon { + color: black; + cursor: pointer; + } + + &__button { + border-radius: 100px; + font-size: 14px; + line-height: 24px; + background: black; + color: white; + padding: 0 10px; + height: 40px; + width: 100%; + border: none; + margin-top: 40px; + cursor: pointer; + padding: 0 20px; + outline: none; + } +} diff --git a/src/components/pages/auth/Login.tsx b/src/components/pages/auth/Login.tsx new file mode 100644 index 0000000..ee3df83 --- /dev/null +++ b/src/components/pages/auth/Login.tsx @@ -0,0 +1,86 @@ +import { yupResolver } from '@hookform/resolvers/yup'; +import React, { useContext, useState } from 'react'; +import { useForm } from 'react-hook-form'; +import { FaEnvelope, FaEye, FaEyeSlash } from 'react-icons/fa'; +import { useHistory } from 'react-router-dom'; +import * as yup from 'yup'; +import './Login.scss'; +import { authenticateUser, UserAuthenticationData } from '../../../api/auth/auth'; +import { HOME_PAGE } from '../../urls'; +import UserContext from '../../../state/user'; + +const loginSchema = yup.object().shape({ + email: yup.string().email().required(), + password: yup.string().required(), // We could define rules here for password if there are +}); + +const Login = (): JSX.Element => { + const history = useHistory(); + const userContext = useContext(UserContext); + const [seePassword, setSeePasword] = useState(false); + const { register, handleSubmit, errors } = useForm({ resolver: yupResolver(loginSchema) }); + + const togglePassword = (): void => setSeePasword(!seePassword); + + const login = (data: UserAuthenticationData): void => { + // Avoid the page to refresh from submitting the form + // event.preventDefault(); + // TODO: put like a loading icon on the login button + + // TODO: do like a safe check on the email & password here + + authenticateUser(data) + .then(() => { + userContext.login(data.email); + history.push(HOME_PAGE); + }) + .catch((error) => { + console.error(error); + }); + }; + + // TODO: handle pressing enter to submit ( maybe having it as a form instead and using submit ) + return ( +
+
Login into your account
+
+
+ +
+ +
+
+ {errors.email?.message ? {errors.email?.message} : ''} + +
+ + + +
+ {errors.password?.message ? {errors.password?.message} : ''} + +
+
+ ); +}; + +export default Login; diff --git a/src/components/pages/errors/Error404.tsx b/src/components/pages/errors/Error404.tsx new file mode 100644 index 0000000..48c7256 --- /dev/null +++ b/src/components/pages/errors/Error404.tsx @@ -0,0 +1,10 @@ +import React from 'react'; + +const Error404 = (): JSX.Element => ( +
+ Error 404 + Page does not exist! +
+); + +export default Error404; diff --git a/src/components/urls.ts b/src/components/urls.ts new file mode 100644 index 0000000..50ef1b5 --- /dev/null +++ b/src/components/urls.ts @@ -0,0 +1,14 @@ +export const HOME_PAGE = '/'; + +// Authentication pages +export const LOGIN_PAGE = '/login'; +export const REGISTER_PAGE = '/register'; + +// IPFS pages +export const IPFS_BASE_PAGE = '/ipfs'; +export const IPFS_PAGE = `${IPFS_BASE_PAGE}/:hash`; + +// Error pages +export const ERROR_404_PAGE = '/error404'; + +export const USER_PAGES = [HOME_PAGE, IPFS_BASE_PAGE, IPFS_PAGE]; diff --git a/src/config/api.ts b/src/config/api.ts new file mode 100644 index 0000000..9df0189 --- /dev/null +++ b/src/config/api.ts @@ -0,0 +1,5 @@ +export const apis = { + // TODO: Replace this by the BE api urls we would be using. Probably storing this in a config file + url: 'https://jsonplaceholder.typicode.com', + timeout: 2 * 60 * 1000, // 2 minutes timeout +}; diff --git a/src/config/index.ts b/src/config/index.ts new file mode 100644 index 0000000..f8cb76b --- /dev/null +++ b/src/config/index.ts @@ -0,0 +1,2 @@ +export { apis } from './api'; +export { storageNames } from './storage-names'; diff --git a/src/config/storage-names.ts b/src/config/storage-names.ts new file mode 100644 index 0000000..b36ebf7 --- /dev/null +++ b/src/config/storage-names.ts @@ -0,0 +1,3 @@ +export const storageNames = { + user: 'pl11', +}; diff --git a/src/state/user.ts b/src/state/user.ts new file mode 100644 index 0000000..d218a9e --- /dev/null +++ b/src/state/user.ts @@ -0,0 +1,27 @@ +import { createContext } from 'react'; + +interface User { + email: string; + isLoggedIn: boolean; +} + +interface UserContextState { + user: User; + login: (email: string) => void; + logout: () => void; +} + +export const emptyUser: User = { + isLoggedIn: false, + email: '', +}; + +export const emptyUserState: UserContextState = { + user: { ...emptyUser }, + login: () => {}, + logout: () => {}, +}; + +const UserContext = createContext({ ...emptyUserState }); + +export default UserContext;