Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch Rockets data from API #32

Merged
merged 4 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ See the section about [deployment](https://facebook.github.io/create-react-app/d
- [x] **Create Missions view**
- [x] **Create Rockets view**
- [x] **Use Redux to manage the states of rockets, missions**
- [ ] **Fetch data using the spaceX API**
- [x] **Fetch data using the spaceX API**
- [ ] **Add actions and other functionality to Join/Leave Missions**
- [ ] **Add actions and other functionality to Reserve Rocket and Cancel Reservation**
- [ ] **Create the list of My Missions and My Rockets in the My Profile view**
Expand Down
13 changes: 10 additions & 3 deletions src/assets/styles/Header.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,21 @@ header {
color: #e59011;
}

#navigation {
display: flex;
justify-content: center;
align-items: center;
gap: 1rem;
}

.nav-item a {
color: #20cfe6;
font-size: 1.3rem;
font-weight: 500;
color: #007bff;
background-color: transparent;
}

.nav-item a.active,
.nav-item a:hover {
color: #fff;
color: #007bff;
text-decoration: underline;
}
39 changes: 38 additions & 1 deletion src/redux/rockets/rocketsSlice.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import { createSlice } from '@reduxjs/toolkit';
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import axios from 'axios';

const apiEndpoint = 'https://api.spacexdata.com/v4/rockets';

const actionName = 'rockets/getAllRockets';

export const getAllRockets = createAsyncThunk(actionName, async (thunkAPI) => {
try {
const response = await axios.get(apiEndpoint);
return response.data;
} catch (error) {
return thunkAPI.rejectWithValue(error.message);
}
});

const initialState = {
rockets: [],
Expand All @@ -10,6 +24,29 @@ const rocketsSlice = createSlice({
name: 'rockets',
initialState,
reducers: {},
extraReducers: (builder) => {
builder
.addCase(getAllRockets.pending, (state) => {
state.isLoading = true;
})
.addCase(getAllRockets.fulfilled, (state, action) => {
state.isLoading = false;
if (state.rockets.length === 0) {
state.rockets = action.payload.map((rocket) => (
{
id: rocket.id,
name: rocket.name,
type: rocket.type,
image: rocket.flickr_images[0],
}
));
}
})
.addCase(getAllRockets.rejected, (state, action) => {
state.isLoading = false;
state.error = action.payload;
});
},
});

export default rocketsSlice.reducer;
21 changes: 16 additions & 5 deletions src/views/Rockets.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { getAllRockets } from '../redux/rockets/rocketsSlice';
import Layout from './Layout';

const Rockets = () => (
<Layout>
<section id="rockets" />
</Layout>
);
const Rockets = () => {
const dispatch = useDispatch();

useEffect(() => {
dispatch(getAllRockets());
}, []);

return (
<Layout>
<section id="rockets" />
</Layout>
);
};

export default Rockets;