Skip to content

Commit

Permalink
Connect to React + Thunk
Browse files Browse the repository at this point in the history
  • Loading branch information
kostyrko committed Nov 13, 2020
1 parent d73069b commit 2ccfced
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 15 deletions.
25 changes: 25 additions & 0 deletions 5_Redux/2_redux-thunk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions 5_Redux/2_redux-thunk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
"@testing-library/user-event": "^12.2.2",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-redux": "^7.2.2",
"react-scripts": "4.0.0",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0",
"web-vitals": "^0.2.4"
},
"scripts": {
Expand Down
16 changes: 5 additions & 11 deletions 5_Redux/2_redux-thunk/src/App.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@

import React from 'react'
import './App.css';
import {createStore } from 'redux'
import rootReducer from './reducers'

import {droidActions} from './app/droids/duck'

// ---------------------------- Store -----------------------

const store = createStore(rootReducer, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__())
window.store = store
import DroidsContainer from './app/droids/components/DroidsContainer'
import DroidsForm from './app/droids/components/DroidsForm'

store.dispatch(droidActions.add('D-O'))


function App() {
return (
<div className="App">

<DroidsContainer/>
<DroidsForm/>
</div>
);
}
Expand Down
36 changes: 36 additions & 0 deletions 5_Redux/2_redux-thunk/src/app/droids/components/DroidsContainer.js
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 5_Redux/2_redux-thunk/src/app/droids/components/DroidsForm.js
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);
19 changes: 19 additions & 0 deletions 5_Redux/2_redux-thunk/src/app/droids/duck/operations.js
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)))
}
4 changes: 2 additions & 2 deletions 5_Redux/2_redux-thunk/src/app/droids/duck/reducers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import types from './types'

const INITIAL_STATE = {
droidsName: "Favorite",
list: ["R2D2", "C3PO", "BB8"],
droidsName: "Types",
list: [],
};

const droidsReducer = (state = INITIAL_STATE, action) => {
Expand Down
7 changes: 5 additions & 2 deletions 5_Redux/2_redux-thunk/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

import {Provider} from 'react-redux'
import store from './store'

ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</React.StrictMode>,
</Provider>,
document.getElementById('root')
);

Expand Down
11 changes: 11 additions & 0 deletions 5_Redux/2_redux-thunk/src/jsServer/db.json
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"]
}

}
9 changes: 9 additions & 0 deletions 5_Redux/2_redux-thunk/src/store.js
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

0 comments on commit 2ccfced

Please sign in to comment.