initial commit

This commit is contained in:
Dmitriy Shishkov 2020-05-12 23:58:10 +05:00
commit 46e7083302
28 changed files with 11965 additions and 0 deletions

23
.gitignore vendored Normal file
View File

@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*

68
README.md Normal file
View File

@ -0,0 +1,68 @@
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
## Available Scripts
In the project directory, you can run:
### `yarn start`
Runs the app in the development mode.<br />
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
The page will reload if you make edits.<br />
You will also see any lint errors in the console.
### `yarn test`
Launches the test runner in the interactive watch mode.<br />
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
### `yarn build`
Builds the app for production to the `build` folder.<br />
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.<br />
Your app is ready to be deployed!
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
### `yarn eject`
**Note: this is a one-way operation. Once you `eject`, you cant go back!**
If you arent satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point youre on your own.
You dont have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldnt feel obligated to use this feature. However we understand that this tool wouldnt be useful if you couldnt customize it when you are ready for it.
## Learn More
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
To learn React, check out the [React documentation](https://reactjs.org/).
### Code Splitting
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
### Analyzing the Bundle Size
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
### Making a Progressive Web App
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
### Advanced Configuration
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
### Deployment
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
### `yarn build` fails to minify
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify

46
package.json Normal file
View File

@ -0,0 +1,46 @@
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"@apollo/react-hooks": "^3.1.5",
"@material-ui/core": "^4.9.12",
"@material-ui/icons": "^4.9.1",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"@types/jest": "^25.2.1",
"@types/node": "^13.13.4",
"@types/react": "^16.9.34",
"@types/react-dom": "^16.9.7",
"@types/react-router-dom": "^5.1.5",
"apollo-boost": "^0.4.7",
"graphql": "^15.0.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1",
"typescript": "^3.8.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

BIN
public/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

30
public/index.html Normal file
View File

@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root" style="min-height: 100vh"></div>
</body>
</html>

BIN
public/logo192.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

BIN
public/logo512.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

25
public/manifest.json Normal file
View File

@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}

3
public/robots.txt Normal file
View File

@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:

View File

@ -0,0 +1,19 @@
import React from 'react'
import { BrowserRouter } from 'react-router-dom'
import TopBar from '../TopBar'
import Navbar from '../Navbar'
import Router from '../../utils/router'
const App: React.FC = () => {
return (
<BrowserRouter>
<TopBar />
<Router />
<Navbar />
</BrowserRouter>
);
}
export default App;

View File

@ -0,0 +1,23 @@
import React from 'react'
import { useQuery } from '@apollo/react-hooks'
import { FETCH_NOTES } from '../../graphql/queries'
const ListNotes: React.FC = () => {
const { loading, error, data } = useQuery(FETCH_NOTES)
if (loading) return <>Loading...</>
if (error) return <>Error: {error.message}</>
return (
<ul>
{data.notes.map((note: any) => (
<li key={note.id}>
<h1>{note.title}</h1>
<p>{note.text}</p>
</li>
))}
</ul>
)
}
export default ListNotes

View File

@ -0,0 +1,72 @@
import React, { useState, ChangeEvent } from 'react'
import { useMutation } from '@apollo/react-hooks'
import { useHistory } from 'react-router-dom'
import { TextField, Grid, Button } from '@material-ui/core'
import { LOGIN_USER } from '../../graphql/mutations'
const LoginForm: React.FC = () => {
const history = useHistory()
const [email, setEmail] = useState<string>('')
const [password, setPassword] = useState<string>('')
const [login] = useMutation(LOGIN_USER, {
onCompleted({ login: user }) {
localStorage.setItem('token', user.token)
console.log(user)
history.push("/")
}
})
const handleSubmit = async () => {
try {
const {data} = await login({ variables: { email, password } })
console.log(data)
} catch (e) {
console.log(e)
}
}
return (
<form className="Form" onSubmit={e => {
e.preventDefault()
handleSubmit()
}}>
<Grid container>
<Grid item xs={12} sm={8}>
<TextField
variant="outlined"
type="email"
value={email}
label="Email"
onChange={
(e: ChangeEvent<HTMLInputElement>): void => {
setEmail(e.target.value)
}
}
/>
<TextField
variant="outlined"
type="password"
value={password}
label="Password"
onChange={
(e: ChangeEvent<HTMLInputElement>): void => {
setPassword(e.target.value)
}
}
/>
</Grid>
<Grid item xs={12} sm={4}>
<Button type="submit" variant="contained" color="primary">
Log in
</Button>
</Grid>
</Grid>
</form>
)
}
export default LoginForm

View File

@ -0,0 +1,32 @@
import React, { useState, useEffect } from 'react'
import { useHistory } from 'react-router-dom'
import { BottomNavigation, BottomNavigationAction, Paper } from '@material-ui/core'
import { Notes } from '@material-ui/icons'
import useStyles from '../../styles'
const routes = ['/']
const Navbar: React.FC = () => {
const [position, setPosition] = useState(0)
const history = useHistory()
useEffect(() => {
history.push(routes[position])
}, [position, history])
const classes = useStyles()
return (
<Paper elevation={5} className={classes.bottomBarPosition}>
<BottomNavigation
value={position}
onChange={(e, newPos) => {
setPosition(newPos)
}}
>
<BottomNavigationAction label="Notes" icon={<Notes />} />
</BottomNavigation>
</Paper>
)
}
export default Navbar

View File

@ -0,0 +1,23 @@
import React, { useState } from 'react'
import { InputBase } from '@material-ui/core'
import './styles.css'
import useStyles from '../../styles'
const Search: React.FC = () => {
const [query, setQuery] = useState<string>('')
const classes = useStyles()
return (
<InputBase
value={query}
onChange={
e => setQuery(e.target.value)
}
className={classes.searchBar}
placeholder="Search..."
/>
)
}
export default Search

View File

@ -0,0 +1,6 @@
.searchBar {
background-color: #ffffff;
width: 100%;
border-radius: 99999px;
padding: 0 16px
}

View File

@ -0,0 +1,33 @@
import React, { useState } from 'react'
import { AppBar, Toolbar, useScrollTrigger, Slide, IconButton } from '@material-ui/core'
import { AccountCircle } from '@material-ui/icons'
import Search from '../Search'
import UserDialog from '../userDialog'
import useStyles from '../../styles'
const TopBar: React.FC = () => {
const hideOnScroll: boolean = useScrollTrigger()
const [openDialog, setOpenDialog] = useState(false)
const classes = useStyles()
return (
<>
<Slide in={!hideOnScroll} >
<AppBar position="fixed">
<Toolbar className={classes.navBarPadding}>
<Search />
<IconButton className={classes.userIconPosition} aria-label="account of current user" onClick={() => setOpenDialog(true)}>
<AccountCircle />
</IconButton>
<UserDialog open={openDialog} setOpen={setOpenDialog} />
</Toolbar>
</AppBar>
</Slide>
{/* Toolbar is used for top padding for navbar */}
<Toolbar />
</>
)
}
export default TopBar

View File

@ -0,0 +1,53 @@
import React, { Dispatch, SetStateAction } from 'react'
import { Dialog, DialogTitle, Avatar, Button, DialogContent, DialogActions, ListItem, ListItemAvatar, ListItemText } from '@material-ui/core'
import { Person } from '@material-ui/icons'
import { useHistory } from 'react-router-dom'
export interface UserDialogProps {
open: boolean;
setOpen: Dispatch<SetStateAction<boolean>>;
}
const UserDialog: React.FC<UserDialogProps> = props => {
const { open, setOpen } = props
const history = useHistory()
const logOut = () => {
localStorage.removeItem('token')
history.push("/login")
}
return (
<Dialog open={open} fullWidth={true} onClose={setOpen}>
<DialogTitle>Account</DialogTitle>
<DialogContent className="accountContainer">
<ListItem>
<ListItemAvatar>
<Avatar>
<Person />
</Avatar>
</ListItemAvatar>
<ListItemText>
Lol
</ListItemText>
</ListItem>
</DialogContent>
<DialogActions>
<Button onClick={() => setOpen(false)} color="primary" autoFocus>
Close
</Button>
<Button onClick={
() => {
setOpen(false)
logOut()
}
} color="primary" >
Log out
</Button>
</DialogActions>
</Dialog>
)
}
export default UserDialog

15
src/graphql/index.ts Normal file
View File

@ -0,0 +1,15 @@
import ApolloClient from 'apollo-boost'
const client = new ApolloClient({
uri: "http://localhost:4000/graphql",
request: (operation) => {
const token: string | null = localStorage.getItem('token')
operation.setContext({
headers: {
authorization: token ? `Bearer ${token}` : ''
}
})
}
})
export default client

21
src/graphql/mutations.ts Normal file
View File

@ -0,0 +1,21 @@
import { gql } from "apollo-boost";
const LOGIN_USER = gql`
mutation(
$email: String!,
$password: String!
) {
login(input: {
email: $email,
password: $password
}) {
userName,
email,
token,
}
}
`
export {
LOGIN_USER
}

15
src/graphql/queries.ts Normal file
View File

@ -0,0 +1,15 @@
import { gql } from "apollo-boost"
const FETCH_NOTES = gql`
query {
notes {
title
text
id
}
}
`
export {
FETCH_NOTES
}

20
src/index.tsx Normal file
View File

@ -0,0 +1,20 @@
import React from 'react'
import ReactDOM from 'react-dom'
import { ApolloProvider } from '@apollo/react-hooks'
import { CssBaseline, ThemeProvider } from '@material-ui/core'
import App from './components/App'
import client from './graphql'
import theme from './theme'
ReactDOM.render(
<React.StrictMode>
<ThemeProvider theme={theme}>
<ApolloProvider client={client}>
<CssBaseline />
<App />
</ApolloProvider>
</ThemeProvider>
</React.StrictMode>,
document.getElementById('root')
)

1
src/react-app-env.d.ts vendored Normal file
View File

@ -0,0 +1 @@
/// <reference types="react-scripts" />

24
src/styles.ts Normal file
View File

@ -0,0 +1,24 @@
import { makeStyles, createStyles, Theme } from "@material-ui/core";
const useStyles = makeStyles((theme: Theme) => createStyles({
topPadding: theme.mixins.toolbar,
bottomBarPosition: {
position: 'fixed',
bottom: 0,
width: "100vw"
},
navBarPadding: {
paddingRight: '0 !important'
},
userIconPosition: {
margin: '0 4px !important'
},
searchBar: {
backgroundColor: '#ffffff',
width: '100%',
borderRadius: '99999px',
padding: '0 16px'
}
}))
export default useStyles

14
src/theme.ts Normal file
View File

@ -0,0 +1,14 @@
import { createMuiTheme } from "@material-ui/core/styles";
const theme = createMuiTheme({
palette: {
primary: {
main: '#edd340'
},
secondary: {
main: '#FA8343'
}
}
})
export default theme

32
src/utils/router.tsx Normal file
View File

@ -0,0 +1,32 @@
import React from 'react'
import { Switch, Route, Redirect } from 'react-router-dom'
import { Grid, useTheme, Box } from '@material-ui/core'
import LoginForm from '../components/LoginForm'
import ListNotes from '../components/ListNotes'
import useStyles from '../styles'
const Router: React.FC = () => {
const theme = useTheme()
const classes = useStyles(theme)
return (
<Box my={4}>
<Grid container>
<Grid item xs={false} sm={2} />
<Grid container item xs={12} sm={8} className={classes.topPadding}>
<Switch>
<Route path='/login' render={
(): JSX.Element => (localStorage.getItem('token')) ? <Redirect to='/' /> : <LoginForm />
} />
<Route exact path='/' render={
(): JSX.Element => (!localStorage.getItem('token')) ? <Redirect to='/login' /> : <ListNotes />
} />
</Switch>
</Grid>
</Grid>
</Box>
)
}
export default Router

10
src/utils/store.ts Normal file
View File

@ -0,0 +1,10 @@
import { createContext } from 'react'
type User = null | {
userName: string;
email: string;
}
const user: User = null
export const StoreContext = createContext(user)

25
tsconfig.json Normal file
View File

@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"allowJs": true
},
"include": [
"src"
]
}

11332
yarn.lock Normal file

File diff suppressed because it is too large Load Diff