Skip to content

Commit

Permalink
Merge pull request #146 from ruby10127130/feature/add-marathon-reduce…
Browse files Browse the repository at this point in the history
…rs-actions

feat: set up marathon-related actions, saga, and store
  • Loading branch information
JohnsonMao authored Dec 9, 2024
2 parents 2157d59 + 9a88c03 commit 857945d
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 1 deletion.
48 changes: 48 additions & 0 deletions redux/actions/marathon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export function fetchMarathonProfileById(id) {
return {
type: 'FETCH_MARATHON_PROFILE_BY_ID',
payload: {
id,
},
};
}

export function createMarathonProfileByToken(token, marathon) {
return {
type: 'CREATE_MARATHON_PROFILE_BY_TOKEN',
payload: {
token,
marathon
}
};
}

export function updateMarathonProfile(token, id, marathon) {
return {
type: 'UPDATE_MARATHON_PROFILE',
payload: {
token,
id,
marathon
}
};
}

export function deleteMarathonProfile(token, id) {
return {
type: 'DELETE_MARATHON_PROFILE',
payload: {
token,
id
}
};
}

export function fetchMarathonProfileByUserId(userId) {
return {
type: 'FETCH_MARATHON_PROFILE_BY_USER_ID',
payload: {
userId
}
};
}
2 changes: 2 additions & 0 deletions redux/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import shared from './shared';
import resource from './resource';
import group from './group';
import partners from './partners';
import marathon from './marathon';

const allReducers = combineReducers({
search,
Expand All @@ -15,6 +16,7 @@ const allReducers = combineReducers({
resource,
group,
partners,
marathon,
});

export default allReducers;
89 changes: 89 additions & 0 deletions redux/reducers/marathon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// import toast from 'react-hot-toast';

const initialState = {
title: '',
eventId: '',
userId: '',
description: '',
motivation: { tags: [], description: '' },
content: "",
goals: "",
strategies: { tags: [], description: '' },
resources: [],
milestones: [],
outcomes: { tags: [], description: '' },
status: "Ongoing",
pricing: { option: "", pricing: 0, email: [], file: "" },
isPublic: false,
startDate: '',
endDate: '',
userMarathon: []
};

const reducer = (state = initialState, action) => {
switch (action.type) {
case 'FETCH_MARATHON_PROFILE_BY_USER_ID': {
return {
...state,
apiState: 'pending'
};
}
case 'FETCH_MARATHON_PROFILE_BY_USER_ID_SUCCESS': {
return {
...state,
userMarathon: action.payload,
apiState: 'success'
};
}
case 'FETCH_MARATHON_PROFILE_BY_USER_ID_FAILURE': {
return {
...state,
apiState: 'reject'
};
}
case 'CREATE_MARATHON_PROFILE': {
return {
...state,
apiState: 'pending'
};
}
case 'CREATE_MARATHON_PROFILE_BY_TOKEN_SUCCESS': {
return {
...state,
...action.payload,
apiState: 'success'
};
}
case 'FETCH_MARATHON_PROFILE_BY_ID': {
return {
...state,
apiState: 'pending'
};
}
case 'FETCH_MARATHON_PROFILE_BY_ID_SUCCESS': {
return {
...state,
...action.payload,
apiState: 'success'
};
}
case 'FETCH_MARATHON_PROFILE_BY_ID_FAILURE': {
return {
...state,
apiState: 'reject'
};
}
case 'UPDATE_MARATHON_PROFILE_SUCCESS': {
return {
...state,
...action.payload,
apiState: 'success',
};
}
default: {
return state;
}
}
};

export default reducer;
2 changes: 2 additions & 0 deletions redux/sagas/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { all } from 'redux-saga/effects';
import searchSaga from './searchSaga';
import userSaga from './user';
import marathonSaga from './marathon';
import partnerSaga from './partnersSaga';
import sharedSaga from './sharedSaga';
import resourceSaga from './resourceSaga';
Expand All @@ -16,5 +17,6 @@ export default function* rootSaga() {
resourceSaga(),
groupSaga(),
partnerSaga(),
marathonSaga()
]);
}
115 changes: 115 additions & 0 deletions redux/sagas/marathon/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { put, takeEvery, call } from "redux-saga/effects";
import { BASE_URL } from "@/constants/common";
import req from "@/utils/request";

function* fetchMarathonProfileByUserId(action) {
const { userId } = action.payload;
try {
const URL = `${BASE_URL}/marathon?userId=${userId}`;

const result = yield req(URL);
yield put({
type: "FETCH_MARATHON_PROFILE_BY_USER_ID_SUCCESS",
payload: result.data,
});
} catch (error) {
console.log(error);
yield put({ type: "FETCH_MARATHON_PROFILE_BY_USER_ID_FAILURE" });
}
}

function* fetchMarathonProfileById(action) {
const { id } = action.payload; // marathon._id
try {
const URL = `${BASE_URL}/marathon/${id}`;

const result = yield req(URL);
yield put({
type: "FETCH_MARATHON_PROFILE_BY_ID_SUCCESS",
payload: result.data,
});
} catch (error) {
console.log(error);
yield put({ type: "FETCH_MARATHON_PROFILE_BY_ID_FAILURE" });
}
}
function* createMarathonProfileByToken(action) {
const { token, marathon } = action.payload;
try {
const URL = `${BASE_URL}/marathon`;

const result = yield req(URL, {
method: "POST",
body: JSON.stringify({
...marathon,
}),
});

yield put({
type: "CREATE_MARATHON_PROFILE_BY_TOKEN_SUCCESS",
payload: { token, ...result.data },
});
} catch (error) {
console.log(error);
yield put({ type: "CREATE_MARATHON_PROFILE_BY_TOKEN_FAILURE" });
}
}

function* updateMarathonProfile(action) {
const { id, marathon } = action.payload;

try {
const URL = `${BASE_URL}/marathon/${id}`;

const result = yield req(URL, {
method: "PUT",
body: JSON.stringify({
...marathon,
}),
});

yield put({
type: "UPDATE_MARATHON_PROFILE_SUCCESS",
payload: result.data,
});
} catch (error) {
yield put({ type: "UPDATE_MARATHON_PROFILE_FAILURE" });
} finally {
yield new Promise((res) => setTimeout(res, 300));
yield put({ type: "UPDATE_MARATHON_PROFILE_API_STATE_RESET" });
}
}

function* deleteMarathonProfile(action) {
const { id } = action.payload;

try {
const URL = `${BASE_URL}/marathon/${id}`;

const result = yield req(URL, {
method: "DELETE",
body: JSON.stringify({
id,
}),
});
yield put({
type: "DELETE_MARATHON_PROFILE_SUCCESS",
payload: result.data,
});
} catch (error) {
yield put({ type: "DELETE_MARATHON_PROFILE_FAILURE" });
} finally {
yield new Promise((res) => setTimeout(res, 300));
yield put({ type: "DELETE_MARATHON_PROFILE_API_STATE_RESET" });
}
}

function* marathonSaga() {
yield takeEvery("FETCH_MARATHON_PROFILE_BY_ID", fetchMarathonProfileById);
yield takeEvery("CREATE_MARATHON_PROFILE_BY_TOKEN", createMarathonProfileByToken);
yield takeEvery("UPDATE_MARATHON_PROFILE", updateMarathonProfile);
yield takeEvery("DELETE_MARATHON_PROFILE", deleteMarathonProfile);
yield takeEvery("FETCH_MARATHON_PROFILE_BY_USER_ID", fetchMarathonProfileByUserId);
}

export default marathonSaga;
2 changes: 1 addition & 1 deletion redux/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
const persistConfig = {
key: 'root',
storage,
whitelist: ['user', 'partners'],
whitelist: ['user', 'partners', 'marathon'],
};

import rootReducer from '../reducers';
Expand Down

0 comments on commit 857945d

Please sign in to comment.