Skip to content

Commit

Permalink
022
Browse files Browse the repository at this point in the history
  • Loading branch information
fevi committed Aug 2, 2020
1 parent 4be52af commit d7f3432
Show file tree
Hide file tree
Showing 22 changed files with 4,400 additions and 3,641 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.DS_Store


.idea/

# Logs
Expand Down
4,269 changes: 2,722 additions & 1,547 deletions client/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"axios": "^0.19.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1"
"react-scripts": "3.4.0"
},
"scripts": {
"start": "react-scripts start",
Expand Down
53 changes: 29 additions & 24 deletions client/public/index.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
<!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"/>
<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"
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png"/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json"/>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
crossorigin="anonymous"
/>

<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Expand All @@ -27,19 +32,19 @@
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
18 changes: 10 additions & 8 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import PostCreate from './PostCreate';
import PostList from './PostList';

export default () => {
return <div className="container">
<h1>Create Post</h1>
<PostCreate/>
<hr/>
<h1>Posts</h1>
<PostList/>
</div>;
};
return (
<div className="container">
<h1>Create Post</h1>
<PostCreate />
<hr />
<h1>Posts</h1>
<PostList />
</div>
);
};
55 changes: 30 additions & 25 deletions client/src/CommentCreate.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
import React, {useState} from 'react';
import React, { useState } from 'react';
import axios from 'axios';

export default ({postId}) => {
const [content, setContent] = useState('');
const onSubmit = async (event) => {
event.preventDefault();
await axios.post(`http://localhost:4001/posts/${postId}/comments`, {
content
});
setContent('');
};
return <div>
<form onSubmit={onSubmit}>
<div className="form-group">
<label>New Comment</label>
<input
value={content}
onChange={e => setContent(e.target.value)}
className="form-control"
>
</input>
</div>
<button className="btn btn-primary">Submit</button>
</form>
</div>;
};
export default ({ postId }) => {
const [content, setContent] = useState('');

const onSubmit = async event => {
event.preventDefault();

await axios.post(`http://localhost:4001/posts/${postId}/comments`, {
content
});

setContent('');
};

return (
<div>
<form onSubmit={onSubmit}>
<div className="form-group">
<label>New Comment</label>
<input
value={content}
onChange={e => setContent(e.target.value)}
className="form-control"
/>
</div>
<button className="btn btn-primary">Submit</button>
</form>
</div>
);
};
41 changes: 21 additions & 20 deletions client/src/CommentList.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import React, {useState, useEffect} from 'react';
import React, { useState, useEffect } from 'react';
import axios from 'axios';

export default ({postId}) => {
const [comments, setComments] = useState([]);
const fetchData = async () => {
const res = await axios.get(`http://localhost:4001/posts/${postId}/comments`);
setComments(res.data);
};
useEffect(
() => {
fetchData();
},
[]
export default ({ postId }) => {
const [comments, setComments] = useState([]);

const fetchData = async () => {
const res = await axios.get(
`http://localhost:4001/posts/${postId}/comments`
);
const renderedComments = comments.map(comment => {
console.log(comment);
return <li key={comment.id}>{comment.content}</li>;
});
return <ul>
{renderedComments}
</ul>
};

setComments(res.data);
};

useEffect(() => {
fetchData();
}, []);

const renderedComments = comments.map(comment => {
return <li key={comment.id}>{comment.content}</li>;
});

return <ul>{renderedComments}</ul>;
};
48 changes: 29 additions & 19 deletions client/src/PostCreate.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import React, {useState} from 'react';
import React, { useState } from 'react';
import axios from 'axios';

export default () => {
const [title, setTitle] = useState('');
const onSubmit = async (event) => {
event.preventDefault();
await axios.post('http://localhost:4000/posts', {
title
});
setTitle('');
};
return <div>
<form onSubmit={onSubmit}>
<div className="form-group">
<label>Title</label>
<input value={title} onChange={e => setTitle(e.target.value)} className="form-control"/>
</div>
<button className="btn btn-primary">Submit</button>
</form>
</div>;
};
const [title, setTitle] = useState('');

const onSubmit = async event => {
event.preventDefault();

await axios.post('http://localhost:4000/posts', {
title
});

setTitle('');
};

return (
<div>
<form onSubmit={onSubmit}>
<div className="form-group">
<label>Title</label>
<input
value={title}
onChange={e => setTitle(e.target.value)}
className="form-control"
/>
</div>
<button className="btn btn-primary">Submit</button>
</form>
</div>
);
};
67 changes: 35 additions & 32 deletions client/src/PostList.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
import React, {useState, useEffect} from 'react';
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import CommentCreate from './CommentCreate';
import CommentList from './CommentList';

export default () => {
const [posts, setPosts] = useState({});
const fetchPosts = async () => {
const res = await axios.get('http://localhost:4000/posts');
setPosts(res.data);
};
useEffect(() => {
fetchPosts();
}, []);
const renderedPosts = Object.values(posts).map(post => {
console.log(post);
return (
<div
className="card"
style={{
width: '30%',
marginBottom: '20%'
}}
key={post.id}
>
<div className="card-body">
<h3>{post.title}</h3>
<CommentList postId={post.id}/>
<CommentCreate postId={post.id}/>
</div>
</div>
)
});
return <div className="d-flex flex-row flex-wrap justify-content-between">
{renderedPosts}
</div>;
}
const [posts, setPosts] = useState({});

const fetchPosts = async () => {
const res = await axios.get('http://localhost:4000/posts');

setPosts(res.data);
};

useEffect(() => {
fetchPosts();
}, []);

const renderedPosts = Object.values(posts).map(post => {
return (
<div
className="card"
style={{ width: '30%', marginBottom: '20px' }}
key={post.id}
>
<div className="card-body">
<h3>{post.title}</h3>
<CommentList postId={post.id} />
<CommentCreate postId={post.id} />
</div>
</div>
);
});

return (
<div className="d-flex flex-row flex-wrap justify-content-between">
{renderedPosts}
</div>
);
};
5 changes: 1 addition & 4 deletions client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(
<App/>,
document.getElementById('root')
);
ReactDOM.render(<App />, document.getElementById('root'));
Loading

0 comments on commit d7f3432

Please sign in to comment.