Skip to content

Commit 0de8834

Browse files
authored
Merge pull request #9 from NisanurBulut/dev_eros
Dev eros refreshed for contextApi
2 parents a7e24e4 + aebb2aa commit 0de8834

File tree

11 files changed

+91
-77
lines changed

11 files changed

+91
-77
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ Hestia is a reactjs application that stores a list of books using localStorage.
117117
<br>
118118
<hr>
119119
20. <b>Eros</b> <br>
120-
This work is practical views of reactJs Hooks.
120+
This work is practical views of reactJs Hooks. Using hooks: useEffect, useState, useContext.
121121

122122
![App-Eros](https://github.com/NisanurBulut/MythologyOfReactJs/blob/master/Trailers/Trailer_Eros.gif)
123123
<br>
@@ -130,7 +130,7 @@ This work is practical views of reactJs tutorials.
130130
<br>
131131
<hr>
132132

133-
<hr>
133+
134134
### Helpfull Websites
135135
[uidesigndaily](https://uidesigndaily.com/posts/sketch-birthdays-list-card-widget-day-1042)
136136
[Devto](https://dev.to/) [popmotion](https://popmotion.io) [json-server](https://github.com/typicode/json-server)

Trailers/Trailer_Eros.gif

66.2 KB
Loading

Trailers/Trailer_Eros_v1.gif

502 KB
Loading

eros/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<b>Eros</b> <br>
2-
This work is practical views of reactJs Hooks.
2+
This work is practical views of reactJs Hooks. Using hooks: useEffect, useState, useContext.
33

44
![App-Eros](https://github.com/NisanurBulut/MythologyOfReactJs/blob/master/Trailers/Trailer_Eros.gif)
55
<br>

eros/src/App.js

+39-40
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, { useState, useEffect, useContext } from 'react';
2+
import FormContextProvider, { FormContext } from './contexts/FormContext';
23
import logo from './logo.svg';
34
import Header from './components/Header';
45
import './App.css';
@@ -7,7 +8,7 @@ import AddContact from './components/AddContact';
78

89
function App() {
910
const url = 'http://localhost:5000/contacts';
10-
const [showAddContact, setShowAddContact] = useState(false);
11+
1112
const [contacts, setContacts] = useState([]);
1213
const fetchContacts = async () => {
1314
const res = await fetch(url);
@@ -29,66 +30,64 @@ function App() {
2930

3031
const deleteContact = async (id) => {
3132
await fetch(`${url}/${id}`, {
32-
method:'DELETE'
33+
method: 'DELETE',
3334
});
3435
setContacts(contacts.filter((a) => a.id !== id));
3536
};
3637
const toggleReminder = async (id) => {
3738
const reminderContact = await fetchContact(id);
38-
const updateContact = {...reminderContact, reminder:!reminderContact.reminder}
39-
const res = await fetch (`${url}/${id}`, {
40-
method:'PUT',
41-
headers:{'Content-type':'application/json'},
42-
body:JSON.stringify(updateContact)
43-
})
39+
const updateContact = {
40+
...reminderContact,
41+
reminder: !reminderContact.reminder,
42+
};
43+
const res = await fetch(`${url}/${id}`, {
44+
method: 'PUT',
45+
headers: { 'Content-type': 'application/json' },
46+
body: JSON.stringify(updateContact),
47+
});
4448
const data = await res.json();
4549

4650
setContacts(
4751
contacts.map((contact) =>
48-
contact.id === id
49-
? { ...contact, reminder: data.reminder }
50-
: contact
52+
contact.id === id ? { ...contact, reminder: data.reminder } : contact
5153
)
5254
);
5355
};
5456
const addContact = async (contact) => {
5557
console.log(JSON.stringify(contact));
56-
const res = await fetch (`${url}`, {
57-
method:'POST',
58-
headers:{'Content-type':'application/json'},
59-
body:JSON.stringify(contact)
60-
})
58+
const res = await fetch(`${url}`, {
59+
method: 'POST',
60+
headers: { 'Content-type': 'application/json' },
61+
body: JSON.stringify(contact),
62+
});
6163
const data = await res.json();
6264
setContacts([...contacts, data]);
63-
setShowAddContact(false);
65+
toggleForm();
6466
// const id = Math.floor(Math.random() * 1000) + 1;
6567
// const newContact = { id, ...contact };
6668
// setContacts([...contacts, newContact]);
6769
};
70+
const { isFormOpen, toggleForm } = useContext(FormContext);
6871
return (
69-
<div className="App">
70-
<header className="App-header">
71-
<h2>Love Tracker</h2>
72-
<div className="container">
73-
<Header
74-
title="Eros"
75-
onAdd={() => setShowAddContact(!showAddContact)}
76-
showAddContact={showAddContact}
77-
/>
7872

79-
{contacts.length > 0 ? (
80-
<Contacts
81-
contacts={contacts}
82-
onDelete={deleteContact}
83-
onToggleReminder={toggleReminder}
84-
/>
85-
) : (
86-
'No contacts'
87-
)}
88-
{showAddContact && <AddContact onAdd={addContact} />}
89-
</div>
90-
</header>
91-
</div>
73+
<div className="App">
74+
<header className="App-header">
75+
<h2>Love Tracker</h2>
76+
<div className="container">
77+
<Header title="Eros" />
78+
{contacts.length > 0 ? (
79+
<Contacts
80+
contacts={contacts}
81+
onDelete={deleteContact}
82+
onToggleReminder={toggleReminder}
83+
/>
84+
) : (
85+
'No contacts'
86+
)}
87+
{isFormOpen && <AddContact onAdd={addContact} />}
88+
</div>
89+
</header>
90+
</div>
9291
);
9392
}
9493

eros/src/components/AddContact.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ function AddContact({ onAdd }) {
3131
onChange={(e) => setDay(e.target.value)}
3232
/>
3333
</div>
34-
<button type="submit" className="btn btn-block">
34+
<div className="form-control">
35+
<button type="submit" className="btn btn-block">
3536
<RiUserHeartFill className="iconStyle" size={25} />
3637
</button>
38+
</div>
3739
</form>
3840
);
3941
}

eros/src/components/Button.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import { HiHeart, HiOutlineHeart } from 'react-icons/all';
44
const Button = ({ color, onClick, showAdd }) => {
5-
5+
console.log(showAdd);
66
return (
7-
<button onClick={onClick}>
8-
{showAdd ? <HiHeart className="" size={35} /> :
9-
<HiOutlineHeart className="" size={35} />}
10-
</button>
7+
<button onClick={onClick}>
8+
{showAdd ? (
9+
<HiHeart className="" size={35} />
10+
) : (
11+
<HiOutlineHeart className="" size={35} />
12+
)}
13+
</button>
1114
);
1215
};
1316
Button.defaultProps = {
@@ -16,6 +19,6 @@ Button.defaultProps = {
1619
Button.prototype = {
1720
text: PropTypes.string,
1821
color: PropTypes.string,
19-
onClick:PropTypes.func
22+
onClick: PropTypes.func,
2023
};
2124
export default Button;

eros/src/components/Header.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import React, { Component } from 'react';
1+
import React, {useContext} from 'react';
22
import PropTypes from 'prop-types';
33
import Button from './Button';
4-
5-
const Header = ({ title, onAdd, showAddContact }) => {
4+
import {FormContext} from '../contexts/FormContext';
5+
const Header = ({title}) => {
6+
const { isFormOpen, toggleForm } = useContext(FormContext);
67
return (
78
<div className="header">
89
<h1>{title}</h1>
9-
<Button showAdd={showAddContact} onClick={onAdd} />
10+
<Button showAdd={isFormOpen} onClick={toggleForm} />
1011
</div>
1112
);
1213
};

eros/src/contexts/FormContext.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React, { Component, createContext } from 'react';
2+
3+
export const FormContext = createContext();
4+
5+
class FormContextProvider extends Component {
6+
state = {
7+
isFormOpen: false,
8+
};
9+
toggleForm = () => {
10+
console.log(this.state.isFormOpen);
11+
this.setState({ isFormOpen: !this.state.isFormOpen });
12+
};
13+
render() {
14+
return (
15+
<FormContext.Provider
16+
value={{ ...this.state, toggleForm: this.toggleForm }}
17+
>
18+
{this.props.children}
19+
</FormContext.Provider>
20+
);
21+
}
22+
}
23+
24+
export default FormContextProvider;

eros/src/index.css

+5-23
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ body {
8585
text-align: left;
8686
}
8787
.add-form {
88+
display: flex;
89+
justify-content: center;
8890
margin-bottom: 40px;
89-
display: inline;
90-
text-align: center !important;
9191
outline: none;
9292
}
9393

9494
.form-control {
95-
margin: 20px 0;
95+
margin: 20px 10px 0;
9696
outline: none;
9797
}
9898

@@ -104,26 +104,8 @@ body {
104104
width: 100%;
105105
height: 40px;
106106
margin: 5px;
107+
text-align: center;
108+
border-radius: 5px;
107109
padding: 3px 7px;
108110
font-size: 17px;
109111
}
110-
111-
.form-control-check {
112-
display: flex;
113-
align-items: center;
114-
justify-content: space-between;
115-
}
116-
117-
.form-control-check label {
118-
flex: 1;
119-
}
120-
121-
.form-control-check input {
122-
flex: 2;
123-
height: 20px;
124-
}
125-
126-
footer {
127-
margin-top: 30px;
128-
text-align: center;
129-
}

eros/src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import ReactDOM from 'react-dom';
33
import './index.css';
44
import App from './App';
55
import reportWebVitals from './reportWebVitals';
6+
import FormContextProvider from './contexts/FormContext';
67

78
ReactDOM.render(
89
<React.StrictMode>
10+
<FormContextProvider>
911
<App />
12+
</FormContextProvider>
1013
</React.StrictMode>,
1114
document.getElementById('root')
1215
);

0 commit comments

Comments
 (0)