-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
146 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
5_Redux/2_redux-thunk/src/app/droids/components/DroidsContainer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React, {useEffect} from 'react'; | ||
import {connect} from 'react-redux' | ||
import {getAllDroids} from '../duck/operations' | ||
|
||
const DroidsContainer = ({droids,getAllDroids}) => { | ||
|
||
useEffect(() => { | ||
console.log(getAllDroids()); | ||
console.log("ComponentDidMount"); | ||
},[]) | ||
|
||
|
||
return ( | ||
<ul> | ||
{droids.list.map((droid,i)=> | ||
<li key={i}>{droid}</li> | ||
)} | ||
</ul> | ||
); | ||
} | ||
// przyjmuje stan i zwraca obiekt z zawartością stanu znajdującą się pod kluczem droids | ||
const mapStateToProps = state => ({ | ||
droids : state.droids | ||
}) | ||
|
||
// funkcja wywołująca funkcję dispatch z parametrem funkcji, która ma być wykonana | ||
const mapDispatchToProps = dispatch => ({ | ||
getAllDroids : ()=> dispatch(getAllDroids()) | ||
}) | ||
|
||
|
||
// funkcja connect | ||
// 1. argument - mapowanie elementów ze stora, które mają być przyjęte w komponencie jako propsy | ||
// 2. obiekt przyjmujący dane z API | ||
export default connect(mapStateToProps, mapDispatchToProps)(DroidsContainer); | ||
|
32 changes: 32 additions & 0 deletions
32
5_Redux/2_redux-thunk/src/app/droids/components/DroidsForm.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from 'react'; | ||
import {connect} from 'react-redux' | ||
import actions from '../duck/actions' | ||
|
||
const DroidsForm = (props) => { | ||
|
||
const droidInput = React.createRef() | ||
|
||
const addDroid = event => { | ||
event.preventDefault() | ||
// podanie stora na którym ma się dokonać operacja dodania | ||
// add jest kluczem w obiekcie zwróconym przez mapDispatchToProps | ||
props.add(droidInput.current.value) | ||
console.log('form', droidInput.current.value) | ||
droidInput.current.value = '' | ||
} | ||
|
||
return ( | ||
<form onSubmit={addDroid}> | ||
<input ref={droidInput}/> | ||
<button type="submit">Add droid</button> | ||
</form> | ||
); | ||
} | ||
|
||
// przygotowanie obiektu, który zawiera funkcje jakie należy wywołać na storze | ||
const mapDispatchToProps = dispatch => ({ | ||
add: droid => dispatch(actions.add(droid)) | ||
}) | ||
|
||
// null ponieważ state nie jest sczytywany | ||
export default connect(null, mapDispatchToProps)(DroidsForm); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// akcje generują state | ||
import actions from "./actions"; | ||
|
||
const fetchDroids = async () => { | ||
const response = await fetch('http://localhost:3001/droidsTypes', { method: 'GET' }) | ||
const json = await response.json() | ||
console.log('json'); | ||
return json | ||
}; | ||
|
||
// thunk - zwraca funkcję asynchroniczną i przekaże w niej dispatcha powodując zmianę statu | ||
// export const getAllDroids = () => { | ||
|
||
export const getAllDroids = () => | ||
async (dispatch) => { | ||
const droids = await fetchDroids() | ||
console.log(droids); | ||
droids['list'].map(droid => dispatch(actions.add(droid))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"droidsClasses" : { | ||
"listName": "Classes", | ||
"list": ["Protocol droid", "Astromech droid", "Power droid", "Utility droid"] | ||
}, | ||
"droidsTypes" : { | ||
"listName": "Types", | ||
"list": ["R2D2", "C3PO", "BB8"] | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {createStore, applyMiddleware } from 'redux' | ||
import rootReducer from './reducers' | ||
import thunk from 'redux-thunk' | ||
|
||
|
||
const store = createStore(rootReducer, applyMiddleware(thunk)) | ||
// window.store = store | ||
|
||
export default store |