Skip to content

Commit fe8ab31

Browse files
committed
Mise en place des reducers
1 parent fa56e77 commit fe8ab31

File tree

7 files changed

+172
-8
lines changed

7 files changed

+172
-8
lines changed

src/actions/index.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//Asyncrone request and Promise based HTTP client for the browser
2+
import axios from 'axios';
3+
4+
//Liste des actions
5+
import * from '../constants/actionTypes';
6+
7+
// Point d'entré de notre API
8+
const ROOT_URI = 'http://localhost:3000';
9+
10+
/*-------- Listes des action du domaine d'intervention -------------*/
11+
12+
export function fetchInterventions() {
13+
return dispatch => {
14+
dispatch({type:FETCH_INTERVENTIONS});
15+
axios.get(`${ROOT_URI}/interventions`,{
16+
headers: {accept: 'application/json'}
17+
})
18+
.then(response =>{
19+
dispatch(fetchInterventionsSuccess(response));
20+
})
21+
.catch(()=>{
22+
dispatch(fetchCompetencesFailure("Liste des interventions est introuvable !"));
23+
});
24+
}
25+
}
26+
27+
export function fetchInterventionsSuccess(Interventions){
28+
return {
29+
type:FETCH_INTERVENTIONS_SUCCESS,
30+
payload:Interventions
31+
};
32+
}
33+
34+
export function fetchInterventionsFailure(error){
35+
return {
36+
type:FETCH_INTERVENTIONS_FAILURE,
37+
payload:error
38+
}
39+
}
40+
41+
/*-------- Listes des action du domaine de competence -------------*/
42+
43+
export function fetchCompetences() {
44+
return dispatch => {
45+
dispatch({type:FETCH_COMPETENCES});
46+
axios.get(`${ROOT_URI}/competences`,{
47+
headers: {accept: 'application/json'}
48+
})
49+
.then(response =>{
50+
dispatch(fetchCompetencesSuccess(response));
51+
})
52+
.catch(()=>{
53+
dispatch(fetchCompetencesFailure("Liste des compétences est introuvable !"));
54+
});
55+
}
56+
}
57+
58+
export function fetchCompetencesSuccess(competences){
59+
return {
60+
type:FETCH_COMPETENCES_SUCCESS,
61+
payload:competences
62+
};
63+
}
64+
65+
export function fetchCompetencesFailure(error){
66+
return {
67+
type:FETCH_COMPETENCES_FAILURE,
68+
payload:error
69+
}
70+
}
71+

src/components/App.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ class App extends Component {
66
render() {
77
return (
88
<div className="App">
9-
<div className="App-header">
10-
<img src={logo} className="App-logo" alt="logo" />
11-
<h2>Welcome to React</h2>
12-
</div>
13-
<p className="App-intro">
14-
To get started, edit <code>src/App.js</code> and save to reload.
15-
</p>
9+
{this.props.children}
1610
</div>
1711
);
1812
}

src/components/competences.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
import React, { Component } from 'react';
2+
//import { Link } from 'react-router';
23

34
class Competences extends Component {
5+
6+
ComponentWillMount() {
7+
this.props.fetchCompetences();
8+
}
9+
10+
renderCompetences(competences) {
11+
return competences.map((competence) => {
12+
return {
13+
<div>
14+
<h3>{competence.titre}</h3>
15+
<p>{competence.description}</p>
16+
</div>
17+
};
18+
});
19+
}
20+
421
render() {
22+
const { competences, error, loding } = this.props.competencesList;
23+
24+
if(loding) {
25+
return <div className="interventions">Loading...</div>
26+
}
27+
else if(error) {
28+
return <div className="alert">Error : {error}</div>
29+
}
530
return (
631
<div>
732
<h2>Divers Competences</h2>
33+
{this.renderCompetences(competences)}
834
</div>
935
);
1036
}

src/components/interventions.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
import React, { Component } from 'react';
2+
//import { Link } from 'react-router';
23

34
class Interventions extends Component {
5+
6+
ComponentWillMount() {
7+
this.props.fetchInterventions();
8+
}
9+
10+
renderCompetences(interventions) {
11+
return interventions.map((intervention) => {
12+
return {
13+
<div>
14+
<h3>{intervention.nom}</h3>
15+
<p>{intervention.description}</p>
16+
</div>
17+
};
18+
});
19+
}
20+
421
render() {
22+
const { interventions, error, loding } = this.props.interventionsList;
23+
24+
if(loding) {
25+
return <div className="interventions">Loading...</div>
26+
}
27+
else if(error) {
28+
return <div className="alert">Error : {error}</div>
29+
}
530
return (
631
<div>
7-
<h2>Domaines Interventions</h2>
32+
<h2>Divers Competences</h2>
33+
{this.renderInterventions(interventions)}
834
</div>
935
);
1036
}

src/reducers/competenceReducer.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//Liste des actions
2+
import * from '../constants/actionTypes';
3+
4+
const INITIAL_STATE = {
5+
competencesList:{competences:[], error:null, loading:false}
6+
};
7+
8+
export default function (state = INITIAL_STATE, actions){
9+
switch (action.type) {
10+
case FETCH_COMPETENCES:
11+
return {...state, competencesList:{competences:[], error:null, loading:true}};
12+
case FETCH_COMPETENCES_SUCCESS:
13+
return {...state, competencesList:{competences:action.payload.data, error:null, loading:false}};
14+
default:
15+
return state;
16+
}
17+
}

src/reducers/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { combineReducers } from redux;
2+
3+
import interventionReducer from './intervention_reducer';
4+
import competenceReducer from './competences_reducer';
5+
6+
//import {reducer as form} from 'redux-form';
7+
8+
const rootReducer = combineReducers({
9+
intervention:interventionReducer,
10+
competence:competenceReducer
11+
});
12+
13+
export default rootReducer;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//Liste des actions
2+
import * from '../constants/actionTypes';
3+
4+
const INITIAL_STATE = {
5+
interventionsList:{interventions:[], error:null, loading:false}
6+
};
7+
8+
export default function (state = INITIAL_STATE, actions){
9+
switch (action.type) {
10+
case FETCH_INTERVENTIONS:
11+
return {...state, interventionsList:{interventions:[], error:null, loading:true}};
12+
case FETCH_INTERVENTIONS_SUCCESS:
13+
return {...state, interventionsList:{interventions:action.payload.data, error:null, loading:false}};
14+
default:
15+
return state;
16+
}
17+
}

0 commit comments

Comments
 (0)