Skip to content

Commit

Permalink
add fetch rockets
Browse files Browse the repository at this point in the history
  • Loading branch information
OybekKayumov committed May 10, 2022
1 parent 9fcabb2 commit 82e2eb0
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 1 deletion.
165 changes: 165 additions & 0 deletions package-lock.json

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

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.2.0",
"@testing-library/user-event": "^13.5.0",
"prop-types": "^15.8.1",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-redux": "^8.0.1",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"redux": "^4.2.0",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.4.1",
"redux-thunks": "^1.0.0",
"web-vitals": "^2.1.4"
},
"scripts": {
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import { Provider } from 'react-redux';
import './index.css';
import App from './App';
import elementStore from './redux/configureStore';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
<Provider store={elementStore}>
<App />
</Provider>
</React.StrictMode>,
);
13 changes: 13 additions & 0 deletions src/redux/configureStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createStore, combineReducers, applyMiddleware } from 'redux';
import logger from 'redux-logger';
import thunk from 'redux-thunk';
import rocketReducer, { fetchRocketsAPI } from './rockets/rockets';

const reducer = combineReducers({
rocketReducer,
});

const elementStore = createStore(reducer, applyMiddleware(thunk, logger));
elementStore.dispatch(fetchRocketsAPI());

export default elementStore;
56 changes: 56 additions & 0 deletions src/redux/rockets/rockets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const ADD_RESERVATION = 'space-travelers/rockets/ADD_ROCKET_RESERVATION';
const REMOVE_RESERVATION = 'space-travelers/rockets/REMOVE__ROCKET_RESERVATION';
const GET_ROCKETS = 'space-travelers/rockets/GET_ROCKETS';
const baseURL = 'https://api.spacexdata.com/v3/rockets';

const initialState = [];

export const addRocketReservation = (payload) => ({
type: ADD_RESERVATION,
payload,
});

export const removeRocketReservation = (payload) => ({
type: REMOVE_RESERVATION,
payload,
});

export const getRockets = (payload) => ({
type: GET_ROCKETS,
payload,
});

export const fetchRocketsAPI = () => async (dispatch) => {
await fetch(`${baseURL}`)
.then((response) => response.json())
.then((rocketList) => {
console.log('rocketList: ', rocketList);

const arrangedList = rocketList.map((rocket) => ({
id: rocket.rocket_id,
name: rocket.rocket_name,
type: rocket.rocket_type,
flickr_images: rocket.flickr_images,
reservation: false,
}));
if (arrangedList) {
dispatch(getRockets(arrangedList));
}
});
};

// reducer
const reducer = (state = initialState, action) => {
switch (action.type) {
case ADD_RESERVATION:
return [...state, action.payload];
case REMOVE_RESERVATION:
return state.filter((book) => book.item_id !== action.payload);
case GET_ROCKETS:
return [...action.payload];
default:
return state;
}
};

export default reducer;

0 comments on commit 82e2eb0

Please sign in to comment.