Skip to content

Commit b2626cf

Browse files
committed
second commit
1 parent e6c3411 commit b2626cf

10 files changed

Lines changed: 324 additions & 71 deletions

File tree

api/db.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"users": [
3+
{
4+
"id": "unique-1",
5+
"name": "Mert TOSUN",
6+
"salary": "5000",
7+
"department": "Bilişim"
8+
},
9+
{
10+
"id": "unique-2",
11+
"name": "Aslı Eda İNANÇ",
12+
"salary": "5000",
13+
"department": "Tasarım"
14+
},
15+
{
16+
"id": "unique-3",
17+
"name": "Semra TOSUN",
18+
"salary": "5000",
19+
"department": "Donanım"
20+
},
21+
{
22+
"name": "kıvanç çolak",
23+
"department": "makine",
24+
"salary": "6000",
25+
"id": "pBFzRh7"
26+
},
27+
{
28+
"name": "çağatay uncu",
29+
"salary": "11100",
30+
"department": "bilgisayar mühhhhh",
31+
"id": "E0ZqMAH"
32+
},
33+
{
34+
"name": "yılmaz zeren",
35+
"department": "haberleşme",
36+
"salary": "5000",
37+
"id": "vUKJSRi"
38+
},
39+
{
40+
"name": "atilla uysal",
41+
"salary": "5300",
42+
"department": "pazarlama",
43+
"id": "cw7jIWf"
44+
},
45+
{
46+
"name": "Alparslan Türkeş",
47+
"salary": "7000",
48+
"department": "Turan",
49+
"id": "98o2BCo"
50+
}
51+
]
52+
}

src/App.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,53 @@
11
import React, {Component} from 'react';
2-
import Navbar from './components/Navbar';
2+
import Navbar from './layout/Navbar';
33
// import User from './components/user';
44
import './App.css';
55
import Users from "./components/Users";
66
// import PropTypes from 'prop-types';
7-
import AddUser from "./components/AddUser";
7+
import AddUser from "./forms/AddUser";
8+
import UpdateUser from "./forms/UpdateUser";
9+
// import Test from "./components/Test";
10+
import NotFound from "./pages/NotFound";
11+
import {BrowserRouter as Router, Route,Switch} from "react-router-dom";
12+
import Contribute from "./pages/Contribute";
13+
// const Home = () => {
14+
// return(
15+
// <h3>Home Page</h3>
16+
// )
17+
// }
18+
// const About = () => {
19+
// return(
20+
// <h3>About Page</h3>
21+
// )
22+
// }
23+
24+
825
class App extends Component {
9-
1026

1127

1228
render () {
1329

1430
return (
15-
<div className="container">
16-
17-
<Navbar title = "User App" />
31+
<Router>
32+
<div className="container">
33+
34+
<Navbar title = "User App" />
1835
<hr/>
19-
<AddUser></AddUser>
20-
<Users/>
36+
37+
<Switch>
38+
<Route exact path = "/" component = {Users} />
39+
<Route exact path = "/add" component = {AddUser} />
40+
<Route exact path = "/github" component = {Contribute} />
41+
<Route exact path = "/edit/:id" component = {UpdateUser} />
42+
<Route component = {NotFound} />
43+
</Switch>
44+
45+
46+
2147

22-
</div>
48+
</div>
49+
</Router>
50+
2351
);
2452
}
2553

src/components/Navbar.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/components/User.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React, { Component } from 'react'
22
import PropTypes from 'prop-types'
33
import UserConsumer from "../context";
4-
4+
import axios from "axios";
5+
import {Link} from "react-router-dom";
56
class User extends Component {
67
state = {
78
isVisible : false
@@ -27,12 +28,15 @@ class User extends Component {
2728
isVisible : !this.state.isVisible
2829
})
2930
}
30-
onDeleteUser = (dispatch,e) => {
31+
onDeleteUser = async (dispatch,e) => {
3132
const {id} = this.props;
33+
//Delete Request
34+
await axios.delete(`http://localhost:3004/users/${id}`);
3235
//Consumerdan kullanıcagımız dispatch gelicek
3336
dispatch({type: "DELETE_USER", payload:id});
3437

3538
}
39+
3640

3741
// constructor (props) {
3842
// super(props)
@@ -43,7 +47,7 @@ class User extends Component {
4347
render() {
4448

4549
//Destructing
46-
const {name,department,salary} = this.props;
50+
const {id,name,department,salary} = this.props;
4751
const {isVisible} = this.state;
4852

4953
return (
@@ -54,7 +58,7 @@ class User extends Component {
5458

5559
return (
5660
<div className ="col-md-8 mb-4" >
57-
<div className ="card" style= {isVisible ? {backgroundColor: "#e6e6fa",color:"white"} : null}>
61+
<div className ="card" style= {isVisible ? {backgroundColor: "#3498DB ",color:"white"} : null}>
5862
<div className = "card-header d-flex justify-content-between">
5963
<h4 className = "d-inline" onClick = {this.onClickEvent.bind(this,34)}>{name}</h4>
6064
<i onClick = {this.onDeleteUser.bind(this,dispatch)} className="far fa-trash-alt" style = {{cursor: "pointer"}}></i>
@@ -63,7 +67,7 @@ class User extends Component {
6367
isVisible ? <div className = "card-body">
6468
<p className = "card-text">Maaş: {salary}</p>
6569
<p className = "card-text">Departman: {department}</p>
66-
70+
<Link to = {`edit/${id}`} className="btn btn-dark btn-block">Update User</Link>
6771
</div> : null
6872
}
6973

src/context.js

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { Component } from 'react'
2+
import axios from 'axios';
23

34
const UserContext = React.createContext();
45

@@ -15,6 +16,14 @@ const reducer = (state,action) => {
1516
...state,
1617
users: [...state.users,action.payload]
1718
}
19+
case "UPDATE_USER":
20+
return {
21+
...state,
22+
users: state.users.map(user => user.id === action.payload.id ? action.payload : user)
23+
//map fonksiyonu bir array dönüyor , biz burda aslında objeler birleşerek bir array olusturuyuo usersımız o oluyo.
24+
//herbir userın üzerinde geziniyoruz userın id'si o anki güncellenmiş userin id'sine eşitse
25+
//güncellenmiş objemizi koymamız gerekiyo bu durumda action.payloadla değiştiriyo, idsi eşit değilse değişiklik yapmıyo
26+
}
1827
default:
1928
return state //uygulammızda ubulunmamız bi type gönderilirse eski state i dönüyor
2029
}
@@ -24,39 +33,19 @@ const reducer = (state,action) => {
2433
export class UserProvider extends Component {
2534

2635
state = {
27-
users : [
28-
{
29-
id:"unique-1",
30-
name : "Mert TOSUN",
31-
salary : "5000",
32-
department : "Bilişim"
33-
},
34-
{
35-
id:"unique-2",
36-
name : "Aslı Eda İNANÇ",
37-
salary : "5000",
38-
department : "Tasarım"
39-
},
40-
{
41-
id:"unique-3",
42-
name : "Semra TOSUN",
43-
salary : "5000",
44-
department : "Donanım"
45-
},
46-
{
47-
id:"unique-4",
48-
name : "Mehmet TOSUN",
49-
salary : "5000",
50-
department : "Üretim"
51-
}
52-
53-
54-
],
36+
users: [],
5537
dispatch : action => {
5638
this.setState(state => reducer (state,action))
5739
}
5840
}
59-
41+
componentDidMount = async () => {
42+
const response = await axios.get("http://localhost:3004/users");
43+
// console.log(response);
44+
this.setState({
45+
users : response.data
46+
})
47+
}
48+
6049
render() {
6150
return (
6251
<UserContext.Provider value = {this.state}>
Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, { Component } from 'react'
22
import posed from 'react-pose';
33
import UserConsumer from "../context";
4-
5-
var uniqid = require('uniqid');
4+
import axios from "axios";
65
const Animation = posed.div({
76
visible: {
87
opacity: 1,
@@ -22,25 +21,33 @@ class AddUser extends Component {
2221
visible: true,
2322
name : "",
2423
department :"",
25-
salary: ""
24+
salary: "",
25+
error: false
2626
}
2727
changeVisibility = (e) => {
2828
this.setState({
2929
visible:!this.state.visible
3030
})
3131
}
32+
validateForm = () => {
33+
const {name,salary,department} = this.state;
34+
if(name === "" || salary === "" || department ==="") {
35+
return false;
36+
}
37+
return true;
38+
}
3239
changeInput = (e) => {
3340
this.setState({
3441

3542
[e.target.name] : e.target.value
3643
})
3744
}
38-
addUser = (dispatch, e) => {
45+
addUser = async (dispatch, e) => {
3946

4047
e.preventDefault();
4148
const {name,department,salary} = this.state;
4249
const newUser = {
43-
id: uniqid(),
50+
4451
// name : name,
4552
// salary : salary,
4653
// department : department böyle hem anahtar hem değeri aynıysa aşağıdaki gibi yazabiliyoruz
@@ -49,8 +56,21 @@ class AddUser extends Component {
4956
salary
5057

5158
}
52-
dispatch({type : "ADD_USER", payload: newUser});
53-
console.log(newUser);
59+
60+
if(!this.validateForm()){ // eğer validate formdan false değeri dönmüşse
61+
this.setState({
62+
error:true
63+
})
64+
return;
65+
66+
}
67+
68+
const response = await axios.post("http://localhost:3004/users",newUser);
69+
70+
71+
dispatch({type : "ADD_USER", payload:response.data});
72+
//redirect tüm işlemlerimiz bittince root a gitmek istediğimizi belirrtiyoruz
73+
this.props.history.push("/");
5474
}
5575
// changeName = (e) => {
5676
// this.setState({
@@ -68,7 +88,7 @@ class AddUser extends Component {
6888
// })
6989
// }
7090
render() {
71-
const {visible,name,salary,department} = this.state;
91+
const {visible,name,salary,department,error} = this.state;
7292
return <UserConsumer>
7393
{
7494
value => {
@@ -83,7 +103,11 @@ class AddUser extends Component {
83103
<div className ="card-header">
84104
<h4 >Add User Form</h4>
85105
</div>
106+
86107
<div className="card-body">
108+
{
109+
error ? <div className ="alert alert-danger">Lütfen Bilgilerinizi Kontrol Edin!</div> : null
110+
}
87111
<form onSubmit = {this.addUser.bind(this,dispatch)}>
88112
<div className = "form-group">
89113
<label htmlFor = "name">Name</label>

0 commit comments

Comments
 (0)