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

Implement mission joining functionality #36

Merged
merged 3 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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,10 @@ See the section about [deployment](https://facebook.github.io/create-react-app/d
- [x] **Create Rockets view**
- [x] **Use Redux to manage the states of rockets, missions**
- [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**
- [x] **Add actions and other functionality to Reserve Rocket**
- [ ] **Add actions and other functionality to Cancel Reservation**
- [x] **Add actions and other functionality to Join Missions**
- [ ] **Add actions and other functionality to Leave Missions**
- [ ] **Create the list of My Missions and My Rockets in the My Profile view**
- [ ] **Add CSS styles to the UI following the mobile first approach**
- [ ] **Create Unit tests using Jest and React testing library**
Expand Down
34 changes: 22 additions & 12 deletions src/components/MissionItem.jsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import { useDispatch } from 'react-redux';
import PropTypes from 'prop-types';
import { addReservedMission } from '../redux/missions/missionsSlice';

const MissionItem = ({ mission }) => (
<tr>
<td className="mission-name">{ mission.mission_name }</td>
<td className="mission-description">{ mission.description}</td>
<td className="mission-status">
<span className="badge badge-gray">Not a member</span>
</td>
<td className="mission-join-button">
<button type="button" className="btn btn-gray">Join Mission</button>
</td>
</tr>
);
const MissionItem = ({ mission }) => {
const dispatch = useDispatch();
const handleClickAddReservedMission = () => {
dispatch(addReservedMission(mission.mission_id));
};

return (
<tr>
<td className="mission-name">{ mission.mission_name }</td>
<td className="mission-description">{ mission.description}</td>
<td className="mission-status">
<span className="badge badge-gray">Not a member</span>
</td>
<td className="mission-join-button">
<button type="button" className="btn btn-gray" onClick={handleClickAddReservedMission}>Join Mission</button>
</td>
</tr>
);
};

MissionItem.propTypes = {
mission: PropTypes.shape({
mission_id: PropTypes.string.isRequired,
mission_name: PropTypes.string.isRequired,
description: PropTypes.string.isRequired,
reserved: PropTypes.bool,
}).isRequired,
};

Expand Down
13 changes: 12 additions & 1 deletion src/redux/missions/missionsSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,17 @@ const initialState = {
const missionsSlice = createSlice({
name: 'missions',
initialState,
reducers: {},
reducers: {
addReservedMission: (state, action) => {
const newMissionState = state.missions.map((mission) => {
if (mission.mission_id === action.payload) {
return { ...mission, reserved: true };
}
return mission;
});
state.missions = newMissionState;
},
},
extraReducers: (builder) => {
builder
.addCase(getAllMissions.pending, (state) => {
Expand All @@ -50,4 +60,5 @@ const missionsSlice = createSlice({
},
});

export const { addReservedMission } = missionsSlice.actions;
export default missionsSlice.reducer;