Skip to content

Commit

Permalink
[ZK] added create task component
Browse files Browse the repository at this point in the history
  • Loading branch information
zkwzk committed Sep 12, 2022
1 parent 9a36342 commit 7065257
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 68 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ it will run at port `3001`, url: `http://localhost:3001`
## Frontend project

`cd frontend`

`npm install`

`npm start`

it will run at port `3000`, url: `http://localhost:3000`, will send request to backend when start
23 changes: 2 additions & 21 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,10 @@
import './App.css';
import styled from 'styled-components'
import ItemList from "./components/ItemList";
import ToDoList from "./components/ToDoList";

const TaskListContainer = styled.div`
max-width: 800px;
color: palevioletred;
background-color: white;
padding: 10px 20px;
`
const Title = styled.div`
font-size: 26px;
margin: 20px;
line-height: 50px;
height: 50px;
text-align: center;
`

function App() {
return (
<>
<Title>To-Do List</Title>
<TaskListContainer className="App">
<ItemList />
</TaskListContainer>
</>
<ToDoList/>
);
}

Expand Down
62 changes: 62 additions & 0 deletions frontend/src/components/AddToDoItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {Container} from "./Container";
import styled from "styled-components";
import {useState} from "react";

const ToDoItemContainer = styled(Container)`
margin-bottom: 20px;
align-item: middle;
justify-content: space-between;
input {
color: palevioletred;
height: 30px;
font-size: 20px;
font-weight: 500;
border-radius: 5px;
border: 0 solid palevioletred;
max-width: 600px;
width: 600px;
padding-left: 5px;
::placeholder { /* Chrome, Firefox, Opera, Safari 10.1+ */
color: palevioletred;
opacity: 0.7; /* Firefox */
}
:focus{
outline: none;
}
}
button {
text-align: right;
color: white;
background: palevioletred;
border: none;
border-radius: 8px;
max-width: 200px;
min-width: 100px;
text-align: center;
font-weight: 600;
:hover {
background: #ffa6c9;
cursor: pointer;
}
}
`;
export default function AddToDoItem(props) {
const {onChange} = props;
const [inputValue, setInputValue] = useState('');
const buttonOnClick = () => {
onChange && onChange(inputValue);
setInputValue('');
}
return (<ToDoItemContainer>
<input type="text" placeholder="Add a task" autoFocus={true} value={inputValue}
onChange={(event) =>
setInputValue(event.target.value)
}/>
<button onClick={buttonOnClick}>ADD</button>
</ToDoItemContainer>)
}
9 changes: 9 additions & 0 deletions frontend/src/components/Container.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import styled from "styled-components";

export const Container = styled.div`
margin: 0 auto;
color: palevioletred;
background-color: white;
padding: 10px 20px;
display: flex;
`;
5 changes: 4 additions & 1 deletion frontend/src/components/Item.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React from 'react';
import styled from 'styled-components'

const ItemContainer = styled.div`
max-width: 800px;
margin: 10px auto;
text-align: left;
padding: 20px 0;
Expand All @@ -16,6 +15,10 @@ const ItemContainer = styled.div`
const CheckBoxContainer = styled.div`
flex: 1;
max-width: 30px;
input[type="checkbox"] {
cursor: pointer;
}
`

const ItemDescriptionContainer = styled.div`
Expand Down
46 changes: 8 additions & 38 deletions frontend/src/components/ItemList.jsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,13 @@
import React, {useEffect, useState} from "react";
import React from "react";
import Item from "./Item";

export default function ItemList() {
const [items, setItems] = useState([]);
const onChange = (id) => {
fetch('http://localhost:3001/done', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({id: id})
}).then(res => res.json())
.then(
(result) => {
setItems(result);
},
(error) => {
console.log(error);
}
)
};

useEffect(() => {
fetch("http://localhost:3001")
.then(res => res.json())
.then(
(result) => {
console.log(result);
setItems(result);
},
(error) => {
console.log(error);
}
)
}, []);
export default function ItemList(props) {
const {items, onChange} = props;
return (<>
{items.length > 0 ? items.map((item) => {
return (<Item key={item.id} item={item} onChange={() => onChange(item.id)}/>);
}) : <div>You have done everything!</div>}
{items.length > 0
? items.map((item) =>
(<Item key={item.id} item={item} onChange={() => onChange(item.id)}/>)
)
: <div>You have done everything!</div>}
</>);
}
80 changes: 80 additions & 0 deletions frontend/src/components/ToDoList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import styled from "styled-components";
import AddToDoItem from "./AddToDoItem";
import ItemList from "./ItemList";
import {useEffect, useState} from "react";

const TaskListContainer = styled.div`
color: palevioletred;
background-color: white;
padding: 10px 20px;
`
const Title = styled.div`
font-size: 26px;
margin: 20px;
line-height: 50px;
height: 50px;
text-align: center;
`

export default function ToDoList() {
const [items, setItems] = useState([]);

useEffect(() => {
fetch("http://localhost:3001")
.then(res => res.json())
.then(
(result) => {
console.log(result);
setItems(result);
},
(error) => {
console.log(error);
}
)
}, []);

const onAdd = (task) => {
fetch('http://localhost:3001/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({task: task})
}).then(res => res.json())
.then(
(result) => {
setItems(result);
},
(error) => {
console.log(error);
}
)
}
const onChecked = (id) => {
fetch('http://localhost:3001/done', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({id: id})
}).then(res => res.json())
.then(
(result) => {
setItems(result);
},
(error) => {
console.log(error);
}
)
};

return (<>
<Title>To-Do List</Title>
<AddToDoItem onChange={onAdd}/>
<TaskListContainer className="App">
<ItemList items={items} onChange={onChecked}/>
</TaskListContainer>
</>);
}
2 changes: 1 addition & 1 deletion frontend/src/index.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
body {
margin: 0 auto;
max-width: 800px;
max-width: 700px;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
Expand Down
8 changes: 1 addition & 7 deletions frontend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();
);

0 comments on commit 7065257

Please sign in to comment.