Skip to content

Commit

Permalink
Merge pull request #169 from ruby10127130/feature/marathon/add-api-state
Browse files Browse the repository at this point in the history
Feature/marathon/add api state
  • Loading branch information
vincentxuu authored Dec 16, 2024
2 parents 169c1be + 200d7a6 commit 5d45e19
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
26 changes: 14 additions & 12 deletions components/Marathon/SignUp/ConfirmForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,27 +287,29 @@ export default function ConfirmForm({
};

useEffect(() => {
switch (marathonState.apiState) {
case 'success': {
switch (marathonState.apiStateWithType) {
case 'updateMarathonProfileSuccess': {
toast.success('更新成功');
router.push('/learning-marathon/success');
break;
}
case 'Reject': {
case 'createMarathonProfileByTokenSuccess': {
toast.success('申請成功');
router.push('/learning-marathon/success');
break;
}
case 'updateMarathonProfileFailure': {
toast.error('更新失敗');
break;
}
case 'createMarathonProfileByTokenFailure': {
toast.error('申請失敗');
break;
}
default:
}
}, [user.apiState]);
}, [marathonState.apiStateWithType]);

useEffect(() => {
if (
marathonState._id &&
hasClickSubmitButton &&
(marathonState.apiState === 'success')) {
router.push('/learning-marathon/success');
}
}, [hasClickSubmitButton, user.apiState, marathonState.apiState]);
return (
<>
<StyledMarathonTitleSection>
Expand Down
6 changes: 5 additions & 1 deletion components/Marathon/SignUp/UserProfileForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export default function UserProfileForm({
const check = searchParams.get('check');
const [hasClickNextStep, setHasClickNextStep] = useState(false);
const [hasGetLatestMarathon, setHasGetLatestMarathon] = useState(false);
const [hasUpdateAuthContext, setHasUpdateAuthContext] = useState(false);
const {
userState,
errors,
Expand Down Expand Up @@ -144,7 +145,10 @@ export default function UserProfileForm({
reduxDispatch(fetchMarathonProfileByUserEvent(user._id, "2025S1"));
setHasGetLatestMarathon(true);
}
authDispatch.updateUser(user);
if (!hasUpdateAuthContext) {
authDispatch.updateUser(user);
setHasUpdateAuthContext(true);
}
}
}, [user]);

Expand Down
17 changes: 17 additions & 0 deletions redux/reducers/marathon.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,107 +31,124 @@ const reducer = (state = initialState, action) => {
return {
...state,
apiState: "pending",
apiStateWithType: 'fetchMarathonProfileByUserId'
};
}
case "FETCH_MARATHON_PROFILE_BY_USER_ID_SUCCESS": {
return {
...state,
userMarathon: action.payload,
apiState: "success",
apiStateWithType: 'fetchMarathonProfileByUserIdSuccess'
};
}
case "FETCH_MARATHON_PROFILE_BY_USER_ID_FAILURE": {
return {
...state,
apiState: "reject",
apiStateWithType: 'fetchMarathonProfileByUserIdFailure'
};
}
case "UPDATE_NEW_MARATHON": {
return {
...action.payload,
apiStateWithType: 'updateNewMarathon'
};
}
case "FETCH_MARATHON_PROFILE_BY_USER_EVENT": {
return {
...state,
apiState: "pending",
apiStateWithType: 'fetchMarathonProfileByUserEvent'
};
}
case "FETCH_MARATHON_PROFILE_BY_USER_EVENT_SUCCESS": {
return {
...state,
...action.payload,
apiState: "success",
apiStateWithType: 'fetchMarathonProfileByUserEventSuccess'
};
}
case "FETCH_MARATHON_PROFILE_BY_USER_EVENT_FAILURE": {
return {
...state,
apiState: "reject",
apiStateWithType: 'fetchMarathonProfileByUserEventFailure'
};
}
case "FETCH_MARATHON_PROFILE_BY_ID_SUCCESS": {
return {
...state,
...action.payload,
apiState: "success",
apiStateWithType: 'fetchMarathonProfileByIdSuccess'
};
}
case "FETCH_MARATHON_PROFILE_BY_ID_FAILURE": {
return {
...state,
apiState: "reject",
apiStateWithType: 'fetchMarathonProfileByIdFailure'
};
}
case "UPDATE_MARATHON_PROFILE": {
return {
...state,
apiState: "pending",
apiStateWithType: 'updateMarathonProfile'
};
}
case "UPDATE_MARATHON_PROFILE_SUCCESS": {
return {
...state,
...action.payload,
apiState: "success",
apiStateWithType: 'updateMarathonProfileSuccess'
};
}
case "UPDATE_MARATHON_PROFILE_FAILURE": {
return {
...state,
apiState: "reject",
apiStateWithType: 'updateMarathonProfileFailure',
};
}

case "CREATE_MARATHON_PROFILE_BY_TOKEN": {
return {
...state,
apiState: "pending",
apiStateWithType: 'createMarathonProfileByToken',
};
}
case "CREATE_MARATHON_PROFILE_BY_TOKEN_SUCCESS": {
return {
...state,
...action.payload,
apiState: "success",
apiStateWithType: 'createMarathonProfileByTokenSuccess'
};
}
case "CREATE_MARATHON_PROFILE_BY_TOKEN_FAILURE": {
return {
...state,
apiState: "reject",
apiStateWithType: 'createMarathonProfileByTokenFailure'
};
}
case "FETCH_MARATHON_PROFILE_BY_ID": {
return {
...state,
apiState: "pending",
apiStateWithType: 'fetchMarathonProfileById',
};
}
case 'UPDATE_MARATHON_PROFILE_API_STATE_RESET': {
return {
...state,
apiState: 'None',
apiStateWithType: 'updateMarathonProfileApiStateReset'
};
}
default: {
Expand Down

0 comments on commit 5d45e19

Please sign in to comment.