Skip to content

Commit 77da1a9

Browse files
authored
Uploaded lesson 5 exercises (code+solution)
1 parent 54817c4 commit 77da1a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+30320
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2+
3+
## Available Scripts
4+
5+
In the project directory, you can run:
6+
7+
### `npm start`
8+
9+
Runs the app in the development mode.<br>
10+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11+
12+
The page will reload if you make edits.<br>
13+
You will also see any lint errors in the console.
14+
15+
### `npm test`
16+
17+
Launches the test runner in the interactive watch mode.<br>
18+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19+
20+
### `npm run build`
21+
22+
Builds the app for production to the `build` folder.<br>
23+
It correctly bundles React in production mode and optimizes the build for the best performance.
24+
25+
The build is minified and the filenames include the hashes.<br>
26+
Your app is ready to be deployed!
27+
28+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29+
30+
### `npm run eject`
31+
32+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33+
34+
If you aren’t 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.
35+
36+
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 you’re on your own.
37+
38+
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39+
40+
## Learn More
41+
42+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43+
44+
To learn React, check out the [React documentation](https://reactjs.org/).

course-04/exercises/lesson-5/client/package-lock.json

Lines changed: 17114 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "typescript-app",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@types/jest": "24.0.11",
7+
"@types/node": "11.13.4",
8+
"@types/react": "16.8.13",
9+
"@types/react-dom": "16.8.4",
10+
"@types/react-router-dom": "^4.3.2",
11+
"react": "^16.8.6",
12+
"react-dom": "^16.8.6",
13+
"react-router-dom": "^5.0.0",
14+
"react-scripts": "2.1.8",
15+
"semantic-ui-css": "^2.4.1",
16+
"semantic-ui-react": "^0.86.0",
17+
"styled-components": "^4.2.0",
18+
"typescript": "3.4.3"
19+
},
20+
"devDependencies": {
21+
"@types/styled-components": "^4.1.14",
22+
"auth0-js": "^9.10.1"
23+
},
24+
"scripts": {
25+
"start": "react-scripts start",
26+
"build": "react-scripts build",
27+
"test": "react-scripts test",
28+
"eject": "react-scripts eject"
29+
},
30+
"eslintConfig": {
31+
"extends": "react-app"
32+
},
33+
"browserslist": [
34+
">0.2%",
35+
"not dead",
36+
"not ie <= 11",
37+
"not op_mini all"
38+
]
39+
}
Binary file not shown.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta
7+
name="viewport"
8+
content="width=device-width, initial-scale=1, shrink-to-fit=no"
9+
/>
10+
<meta name="theme-color" content="#000000" />
11+
<!--
12+
manifest.json provides metadata used when your web app is installed on a
13+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
14+
-->
15+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
16+
<!--
17+
Notice the use of %PUBLIC_URL% in the tags above.
18+
It will be replaced with the URL of the `public` folder during the build.
19+
Only files inside the `public` folder can be referenced from the HTML.
20+
21+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
22+
work correctly both with client-side routing and a non-root public URL.
23+
Learn how to configure a non-root public URL by running `npm run build`.
24+
-->
25+
<title>React App</title>
26+
</head>
27+
<body>
28+
<noscript>You need to enable JavaScript to run this app.</noscript>
29+
<div id="root"></div>
30+
<!--
31+
This HTML file is a template.
32+
If you open it directly in the browser, you will see an empty page.
33+
34+
You can add webfonts, meta tags, or analytics to this file.
35+
The build step will place the bundled scripts into the <body> tag.
36+
37+
To begin the development, run `npm start` or `yarn start`.
38+
To create a production bundle, use `npm run build` or `yarn build`.
39+
-->
40+
</body>
41+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"short_name": "React App",
3+
"name": "Create React App Sample",
4+
"icons": [
5+
{
6+
"src": "favicon.ico",
7+
"sizes": "64x64 32x32 24x24 16x16",
8+
"type": "image/x-icon"
9+
}
10+
],
11+
"start_url": ".",
12+
"display": "standalone",
13+
"theme_color": "#000000",
14+
"background_color": "#ffffff"
15+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
.App {
2+
text-align: center;
3+
}
4+
5+
.App-logo {
6+
animation: App-logo-spin infinite 20s linear;
7+
height: 40vmin;
8+
pointer-events: none;
9+
}
10+
11+
.App-header {
12+
background-color: #282c34;
13+
min-height: 100vh;
14+
display: flex;
15+
flex-direction: column;
16+
align-items: center;
17+
justify-content: center;
18+
font-size: calc(10px + 2vmin);
19+
color: white;
20+
}
21+
22+
.App-link {
23+
color: #61dafb;
24+
}
25+
26+
@keyframes App-logo-spin {
27+
from {
28+
transform: rotate(0deg);
29+
}
30+
to {
31+
transform: rotate(360deg);
32+
}
33+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import React, { Component } from 'react'
2+
import { GroupsList } from './components/GroupsList'
3+
import { Router, Link, Route, Switch } from 'react-router-dom'
4+
import { Grid, Menu, Segment } from 'semantic-ui-react'
5+
import { ImagesList } from './components/ImagesList'
6+
import { NotFound } from './components/NotFound'
7+
import { CreateImage } from './components/CreateImage'
8+
import { CreateGroup } from './components/CreateGroup'
9+
import Auth from './auth/Auth'
10+
11+
export interface AppProps {}
12+
13+
export interface AppProps {
14+
auth: Auth
15+
history: any
16+
}
17+
18+
export interface AppState {}
19+
20+
export default class App extends Component<AppProps, AppState> {
21+
constructor(props: AppProps) {
22+
super(props)
23+
24+
this.handleLogin = this.handleLogin.bind(this)
25+
this.handleLogout = this.handleLogout.bind(this)
26+
}
27+
28+
handleLogin() {
29+
this.props.auth.login()
30+
}
31+
32+
handleLogout() {
33+
this.props.auth.logout()
34+
}
35+
36+
render() {
37+
return (
38+
<div>
39+
<Segment style={{ padding: '8em 0em' }} vertical>
40+
<Grid container stackable verticalAlign="middle">
41+
<Grid.Row>
42+
<Grid.Column width={16}>
43+
<Router history={this.props.history}>
44+
{this.generateMenu()}
45+
46+
{this.generateCurrentPage()}
47+
</Router>
48+
</Grid.Column>
49+
</Grid.Row>
50+
</Grid>
51+
</Segment>
52+
</div>
53+
)
54+
}
55+
56+
generateMenu() {
57+
return (
58+
<Menu>
59+
<Menu.Item name="home">
60+
<Link to="/">Home</Link>
61+
</Menu.Item>
62+
63+
<Menu.Menu position="right">{this.logInLogOutButton()}</Menu.Menu>
64+
</Menu>
65+
)
66+
}
67+
68+
logInLogOutButton() {
69+
if (this.props.auth.isAuthenticated()) {
70+
return (
71+
<Menu.Item name="logout" onClick={this.handleLogout}>
72+
Log Out
73+
</Menu.Item>
74+
)
75+
} else {
76+
return (
77+
<Menu.Item name="login" onClick={this.handleLogin}>
78+
Log In
79+
</Menu.Item>
80+
)
81+
}
82+
}
83+
84+
generateCurrentPage() {
85+
return (
86+
<Switch>
87+
<Route
88+
path="/groups/create"
89+
exact
90+
render={props => {
91+
return <CreateGroup {...props} auth={this.props.auth} />
92+
}}
93+
/>
94+
95+
<Route path="/images/:groupId" exact component={ImagesList} />
96+
97+
<Route
98+
path="/images/:groupId/create"
99+
exact
100+
render={props => {
101+
return <CreateImage {...props} auth={this.props.auth} />
102+
}}
103+
/>
104+
105+
<Route path="/" exact component={GroupsList} />
106+
107+
<Route component={NotFound} />
108+
</Switch>
109+
)
110+
}
111+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { GroupModel } from '../types/GroupModel'
2+
import { apiEndpoint } from '../config'
3+
import { GroupUploadInfo } from '../types/GroupUploadInfo'
4+
5+
export async function getGroups(): Promise<GroupModel[]> {
6+
console.log('Fetching groups')
7+
8+
const response = await fetch(`${apiEndpoint}/groups`)
9+
const result = await response.json()
10+
11+
return result.items
12+
}
13+
14+
export async function createGroup(
15+
idToken: string,
16+
newGroup: GroupUploadInfo
17+
): Promise<GroupModel> {
18+
const reply = await fetch(`${apiEndpoint}/groups`, {
19+
method: 'POST',
20+
headers: {
21+
'Content-Type': 'application/json',
22+
'Authorization': `Bearer ${idToken}`
23+
},
24+
body: JSON.stringify({
25+
name: newGroup.name,
26+
description: newGroup.description
27+
})
28+
})
29+
const result = await reply.json()
30+
return result.newItem
31+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { apiEndpoint } from '../config'
2+
import { ImageModel } from '../types/ImageModel'
3+
import { ImageUploadInfo } from '../types/ImageUploadInfo'
4+
import { ImageUploadResponse } from '../types/ImageUploadResponse'
5+
6+
export async function getImages(groupId: string): Promise<ImageModel[]> {
7+
console.log('Fetching images')
8+
const response = await fetch(`${apiEndpoint}/groups/${groupId}/images`)
9+
const result = await response.json()
10+
11+
return result.items
12+
}
13+
14+
export async function createImage(
15+
idToken: string,
16+
newImage: ImageUploadInfo
17+
): Promise<ImageUploadResponse> {
18+
19+
const reply = await fetch(
20+
`${apiEndpoint}/groups/${newImage.groupId}/images`,
21+
{
22+
method: 'POST',
23+
headers: {
24+
'Content-Type': 'application/json',
25+
'Authorization': `Bearer ${idToken}`
26+
},
27+
body: JSON.stringify({
28+
title: newImage.title
29+
})
30+
}
31+
)
32+
33+
return await reply.json()
34+
}
35+
36+
export async function uploadFile(uploadUrl: string, file: Buffer): Promise<void> {
37+
await fetch(uploadUrl, {
38+
method: 'PUT',
39+
body: file
40+
})
41+
}

0 commit comments

Comments
 (0)