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

Profile missions #138

Merged
merged 2 commits into from
Sep 9, 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
31 changes: 31 additions & 0 deletions src/components/MissionProfile/MissionProfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { useSelector } from 'react-redux';
import '../../pages/MyProfile/MyProfile.css';

const MissionProfile = () => {
const missions = useSelector((state) => state.missions.missions);
const joinedMissions = missions.filter((mission) => mission.reserved);

return (
<div>
<h2>My Missions</h2>
<table className="rocket-profile-table">
<tbody>
{joinedMissions.length === 0 ? (
<tr>
<td className="no-rocket-placeholder">No Missions Added</td>
</tr>
) : (
joinedMissions.map((mission) => (
<tr key={mission.id}>
<td>{mission.mission_name}</td>
</tr>
))
)}
</tbody>
</table>
</div>
);
};

export default MissionProfile;
22 changes: 18 additions & 4 deletions src/pages/Missions/Missions.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { fetchMissionAsync, joiningMission } from '../../redux/missionSlice';
import { fetchMissionAsync, joiningMission, leavingMission } from '../../redux/missionSlice';
import './Missions.css';

function Missions() {
const Missions = () => {
const dispatch = useDispatch();
const missions = useSelector((state) => state.missions.missions);
const status = useSelector((state) => state.missions.status);
const error = useSelector((state) => state.missions.error);

useEffect(() => {
dispatch(fetchMissionAsync());
if (missions.length === 0) {
dispatch(fetchMissionAsync());
}
}, [dispatch]);

if (status === 'loading') {
Expand Down Expand Up @@ -50,6 +52,18 @@ function Missions() {
</p>
</td>
<td>
{mission.reserved && (
<button
type="button"
style={{
color: mission.reserved ? 'red' : '',
border: mission.reserved ? '1px solid red' : '',
}}
onClick={() => dispatch(leavingMission(mission.mission_id))}
>
Leave Mission
</button>
)}
{!mission.reserved && (
<button
type="button"
Expand All @@ -68,6 +82,6 @@ function Missions() {
</tbody>
</table>
);
}
};

export default Missions;
4 changes: 2 additions & 2 deletions src/pages/MyProfile/MyProfile.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import RocketProfile from '../../components/RocketProfile/RocketProfile';
import MissionProfile from '../../components/MissionProfile/MissionProfile';
import './MyProfile.css';

const Profile = () => (
<div className="main-profile-div">
<div className="missions-profile-div">
<h2>My Missions</h2>
<div className="content">Content</div>
<MissionProfile />
</div>
<div className="rockets-profile-div">
<div className="rocket-profile-div">
Expand Down
18 changes: 10 additions & 8 deletions src/redux/missionSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ const missionSlice = createSlice({
return { ...mission, reserved: true };
});
},
},
leavingMission: (state, action) => {
const id = action.payload;
state.missions = state.missions.map((mission) => {
if (mission.mission_id !== id) return mission;
return { ...mission, reserved: false };
});
leavingMission: (state, action) => {
const id = action.payload;
state.missions = state.missions.map((mission) => {
if (mission.mission_id !== id) {
return mission;
}
return { ...mission, reserved: false };
});
},
},
extraReducers: (builder) => {
builder
Expand All @@ -54,5 +56,5 @@ const missionSlice = createSlice({
},
});

export const { joiningMission } = missionSlice.actions;
export const { joiningMission, leavingMission } = missionSlice.actions;
export default missionSlice.reducer;