-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #146 from ruby10127130/feature/add-marathon-reduce…
…rs-actions feat: set up marathon-related actions, saga, and store
- Loading branch information
Showing
6 changed files
with
257 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters