Skip to content

Commit

Permalink
Merge pull request #136 from pushkar1995/joiningMission
Browse files Browse the repository at this point in the history
Joining mission
  • Loading branch information
BilalLiaquat7 authored Sep 8, 2023
2 parents 1d541c3 + 3e733b5 commit 0dcf17e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
24 changes: 16 additions & 8 deletions src/pages/Missions/Missions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { fetchMissionAsync } from '../../redux/missionSlice';
import { fetchMissionAsync, joiningMission } from '../../redux/missionSlice';
import './Missions.css';

function Missions() {
Expand Down Expand Up @@ -40,20 +40,28 @@ function Missions() {
<tr key={mission.mission_id}>
<td>{mission.mission_name}</td>
<td>{mission.description}</td>
<td><p style={{ backgroundColor: mission.reserved ? '#419bf9' : '#6d757d' }}>{mission.reserved ? 'ACTIVE MEMBER' : 'NOT A MEMBER'}</p></td>
<td>
<button
type="button"
style={{ color: mission.reserved ? 'red' : '', border: mission.reserved ? '1px solid red' : '' }}
<p
style={{
backgroundColor: mission.reserved ? '#419bf9' : '#6d757d',
}}
>
Leave Mission
</button>
{mission.reserved ? 'ACTIVE MEMBER' : 'NOT A MEMBER'}
</p>
</td>
<td>
{!mission.reserved && (
<button
type="button"
style={{ color: mission.reserved ? 'red' : '', border: mission.reserved ? '1px solid red' : '' }}
style={{
color: mission.reserved ? 'red' : '',
border: mission.reserved ? '1px solid red' : '',
}}
onClick={() => dispatch(joiningMission(mission.mission_id))}
>
Join Mission
</button>
)}
</td>
</tr>
))}
Expand Down
20 changes: 19 additions & 1 deletion src/redux/missionSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,24 @@ export const fetchMissionAsync = createAsyncThunk(
const missionSlice = createSlice({
name: 'missions',
initialState,
reducers: {},
reducers: {
joiningMission: (state, action) => {
const id = action.payload;
state.missions = state.missions.map((mission) => {
if (mission.mission_id !== id) {
return mission;
}
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 };
});
},
extraReducers: (builder) => {
builder
.addCase(fetchMissionAsync.pending, (state) => {
Expand All @@ -37,4 +54,5 @@ const missionSlice = createSlice({
},
});

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

0 comments on commit 0dcf17e

Please sign in to comment.