Skip to content

Commit

Permalink
Merge branch 'master' into loadable
Browse files Browse the repository at this point in the history
  • Loading branch information
mark86092 authored Dec 10, 2024
2 parents bab13e3 + 9eaa137 commit 1ae00bf
Show file tree
Hide file tree
Showing 23 changed files with 207 additions and 132 deletions.
76 changes: 30 additions & 46 deletions src/actions/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ import {
} from 'apis/auth';
import { queryMeApi } from 'apis/me';
import authStatus from 'constants/authStatus';
import rollbar from 'utils/rollbar';
import { NOTIFICATION_TYPE } from 'constants/toastNotification';
import { ERROR_CODE_MSG } from 'constants/errorCodeMsg';
import { pushNotification } from 'actions/toastNotification';
import { pushErrorNotificationAndRollbarAndThrowError } from 'actions/toastNotification';
import { GraphqlError } from 'utils/errors';
import {
ER0001,
ER0002,
ER0003,
ER0004,
ER0006,
ER0009,
ER0010,
ER0011,
ER0012,
ER0013,
ER0014,
ER0015,
ER0016,
} from 'constants/errorCodeMsg';

export const SET_LOGIN = '@@auth/SET_LOGIN';
export const SET_USER = '@@auth/SET_USER';
Expand Down Expand Up @@ -42,60 +54,32 @@ const FBSDKLogin = FB => {
});
};

const composeErrMsg = (code, message, error) => {
if (error) {
return `[${code}] ${message}: ${error}`;
} else {
return `[${code}] ${message}`;
}
};
const loginErrorToast = (code, message) =>
pushNotification(NOTIFICATION_TYPE.ALERT, composeErrMsg(code, message));

const toastNotificationAndRollbarAndThrowError = (errorCode, error, extra) => (
dispatch,
getState,
) => {
dispatch(loginErrorToast(errorCode, ERROR_CODE_MSG[errorCode].external));
const internalMsg = composeErrMsg(
errorCode,
ERROR_CODE_MSG[errorCode].internal,
error,
);
if (!extra) {
rollbar.error(internalMsg);
} else {
rollbar.error(internalMsg, extra);
}
throw new Error(internalMsg);
};

/**
* Use `hooks/login/useFacebookLogin` as possible
*/
export const loginWithFB = FBSDK => async (dispatch, getState) => {
if (!FBSDK) {
dispatch(toastNotificationAndRollbarAndThrowError('ER0001'));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0001));
}

let fbLoginResponse = null;
try {
// invoke FB SDK Login to get FB-issued access token
fbLoginResponse = await FBSDKLogin(FBSDK);
} catch (error) {
dispatch(toastNotificationAndRollbarAndThrowError('ER0002', error));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0002, error));
}

if (!fbLoginResponse || !fbLoginResponse.status) {
dispatch(toastNotificationAndRollbarAndThrowError('ER0003'));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0003));
}

switch (fbLoginResponse.status) {
case authStatus.CANCELED:
return;
case authStatus.NOT_AUTHORIZED:
dispatch(setLogin(authStatus.NOT_AUTHORIZED));
dispatch(toastNotificationAndRollbarAndThrowError('ER0004'));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0004));
break;
case authStatus.CONNECTED:
try {
Expand All @@ -107,20 +91,20 @@ export const loginWithFB = FBSDK => async (dispatch, getState) => {
} catch (error) {
if (error instanceof GraphqlError && error.codes) {
if (error.codes[0] === 'UNAUTHENTICATED') {
dispatch(toastNotificationAndRollbarAndThrowError('ER0014'));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0014));
break;
} else if (error.codes[0] === 'FORBIDDEN') {
dispatch(toastNotificationAndRollbarAndThrowError('ER0015'));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0015));
break;
}
}
dispatch(toastNotificationAndRollbarAndThrowError('ER0016', error));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0016, error));
}
break;
default:
dispatch(
toastNotificationAndRollbarAndThrowError(
'ER0006',
pushErrorNotificationAndRollbarAndThrowError(
ER0006,
null,
fbLoginResponse,
),
Expand All @@ -141,27 +125,27 @@ export const loginWithGoogle = credentialResponse => async (
) => {
// TODO: 當登入失敗
if (!credentialResponse || !credentialResponse.credential) {
dispatch(toastNotificationAndRollbarAndThrowError('ER0009'));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0009));
}
const idToken = credentialResponse.credential;
try {
const response = await postAuthGoogleApi({ idToken });
if (response && response.token) {
await dispatch(loginWithToken(response.token));
} else {
dispatch(toastNotificationAndRollbarAndThrowError('ER0010'));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0010));
}
} catch (error) {
if (error instanceof GraphqlError && error.codes) {
if (error.codes[0] === 'UNAUTHENTICATED') {
dispatch(toastNotificationAndRollbarAndThrowError('ER0011'));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0011));
return;
} else if (error.codes[0] === 'FORBIDDEN') {
dispatch(toastNotificationAndRollbarAndThrowError('ER0012'));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0012));
return;
}
}
dispatch(toastNotificationAndRollbarAndThrowError('ER0013', error));
dispatch(pushErrorNotificationAndRollbarAndThrowError(ER0013, error));
}
};

Expand Down
51 changes: 51 additions & 0 deletions src/actions/toastNotification.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import rollbar from 'utils/rollbar';
import { NOTIFICATION_TYPE } from 'constants/toastNotification';
import { ERROR_CODE_MSG } from 'constants/errorCodeMsg';
import { generateNotification } from 'utils/toastNotification';

export const PUSH = '@@TOAST_NOTIFICATION/PUSH';
Expand All @@ -16,3 +19,51 @@ export const removeNotification = id => ({
type: REMOVE,
id,
});

const composeErrorMessage = (code, message, error) => {
if (error) {
return `[${code}] ${message}: ${error}`;
} else {
return `[${code}] ${message}`;
}
};

const pushErrorNotification = (code, message) =>
pushNotification(NOTIFICATION_TYPE.ALERT, composeErrorMessage(code, message));

export const pushErrorNotificationAndRollbar = (errorCode, error, extra) => (
dispatch,
getState,
) => {
dispatch(
pushErrorNotification(errorCode, ERROR_CODE_MSG[errorCode].external),
);

const internalMessage = composeErrorMessage(
errorCode,
ERROR_CODE_MSG[errorCode].internal,
error,
);

if (!extra) {
rollbar.error(internalMessage);
} else {
rollbar.error(internalMessage, extra);
}
};

export const pushErrorNotificationAndRollbarAndThrowError = (
errorCode,
error,
extra,
) => (dispatch, getState) => {
dispatch(pushErrorNotificationAndRollbar(errorCode, error, extra));

const internalMessage = composeErrorMessage(
errorCode,
ERROR_CODE_MSG[errorCode].internal,
error,
);

throw new Error(internalMessage);
};
3 changes: 1 addition & 2 deletions src/components/CompanyAndJobTitle/IndexPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import {
pageTypeTranslation,
generatePageURL,
generateIndexURL,
PAGE_SIZE,
} from 'constants/companyJobTitle';
import styles from './CompanyAndJobTitleIndex.module.css';
import { isFetched } from 'utils/fetchBox';
import { formatTitle, formatCanonicalPath } from 'utils/helmetHelper';
import { SITE_NAME } from 'constants/helmetData';

const PAGE_SIZE = 10;

const IndexHelmet = ({ pageType, page }) => {
const title = `所有${pageTypeTranslation[pageType]}資料 - 第${page}頁`;

Expand Down
6 changes: 3 additions & 3 deletions src/components/CompanyAndJobTitle/Overview/Overview.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ const Overview = ({

Overview.propTypes = {
averageWeekWorkTime: PropTypes.number.isRequired,
interviewExperiences: PropTypes.arrayOf(PropTypes.object),
interviewExperiences: PropTypes.arrayOf(PropTypes.object).isRequired,
interviewExperiencesCount: PropTypes.number.isRequired,
jobAverageSalaries: PropTypes.array,
overtimeFrequencyCount: PropTypes.object.isRequired,
pageName: PropTypes.string.isRequired,
pageType: PropTypes.string.isRequired,
salaryDistribution: PropTypes.array,
salaryWorkTimes: PropTypes.arrayOf(PropTypes.object),
salaryWorkTimes: PropTypes.arrayOf(PropTypes.object).isRequired,
salaryWorkTimesCount: PropTypes.number.isRequired,
workExperiences: PropTypes.arrayOf(PropTypes.object),
workExperiences: PropTypes.arrayOf(PropTypes.object).isRequired,
workExperiencesCount: PropTypes.number.isRequired,
};

Expand Down
4 changes: 1 addition & 3 deletions src/components/CompanyAndJobTitle/Overview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BoxStatusRenderer } from '../StatusRenderer';
import OverviewSection from './Overview';
import Helmet from './Helmet';

const Overview = ({ pageType, pageName, tabType, overviewBox, page }) => (
const Overview = ({ pageType, pageName, tabType, overviewBox }) => (
<CompanyAndJobTitleWrapper
pageType={pageType}
pageName={pageName}
Expand Down Expand Up @@ -40,7 +40,6 @@ const Overview = ({ pageType, pageName, tabType, overviewBox, page }) => (
jobAverageSalaries={data.jobAverageSalaries}
averageWeekWorkTime={data.averageWeekWorkTime}
overtimeFrequencyCount={data.overtimeFrequencyCount}
page={page}
/>
</Fragment>
);
Expand All @@ -66,7 +65,6 @@ Overview.propTypes = {
error: PropTypes.any,
status: PropTypes.string.isRequired,
}).isRequired,
page: PropTypes.number.isRequired,
pageName: PropTypes.string.isRequired,
pageType: PropTypes.string.isRequired,
tabType: PropTypes.string.isRequired,
Expand Down
7 changes: 3 additions & 4 deletions src/components/CompanyAndJobTitle/TimeAndSalary/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ const TimeAndSalary = ({
pageName={pageName}
tabType={tabType}
salaryWorkTimes={salaryWorkTimes}
salaryWorkTimeStatistics={salaryWorkTimeStatistics}
page={page}
pageSize={pageSize}
totalCount={totalCount}
Expand All @@ -57,12 +56,12 @@ const TimeAndSalary = ({

TimeAndSalary.propTypes = {
page: PropTypes.number.isRequired,
pageName: PropTypes.string,
pageName: PropTypes.string.isRequired,
pageSize: PropTypes.number.isRequired,
pageType: PropTypes.string,
pageType: PropTypes.string.isRequired,
salaryWorkTimeStatistics: PropTypes.object.isRequired,
salaryWorkTimes: PropTypes.array,
tabType: PropTypes.string,
tabType: PropTypes.string.isRequired,
totalCount: PropTypes.number.isRequired,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Header, { CompanyJobTitleHeader } from '../../common/TypeFormHeader';
import SubmittableFormBuilder from '../../common/SubmittableFormBuilder';
import { createInterviewExperience } from 'actions/experiences';
import { GA_CATEGORY, GA_ACTION } from 'constants/gaConstants';
import { ERROR_CODE_MSG } from 'constants/errorCodeMsg';
import { ER0008, ERROR_CODE_MSG } from 'constants/errorCodeMsg';
import {
DATA_KEY_COMPANY_NAME,
DATA_KEY_JOB_TITLE,
Expand Down Expand Up @@ -165,7 +165,7 @@ const TypeForm = ({ open, onClose }) => {
category: GA_CATEGORY.SHARE_INTERVIEW_TYPE_FORM,
action: GA_ACTION.UPLOAD_FAIL,
});
const errorCode = 'ER0008';
const errorCode = ER0008;
rollbar.error(
`[${errorCode}] ${ERROR_CODE_MSG[errorCode].internal} ${error.message}`,
error,
Expand Down
4 changes: 2 additions & 2 deletions src/components/ShareExperience/TimeSalaryForm/TypeForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
DATA_KEY_HAS_OVERTIME_SALARY,
DATA_KEY_HAS_COMPENSATORY_DAYOFF,
} from '../constants';
import { ERROR_CODE_MSG } from 'constants/errorCodeMsg';
import { ER0007, ERROR_CODE_MSG } from 'constants/errorCodeMsg';

import { evolve } from '../utils';
import { generateTabURL, pageType, tabType } from 'constants/companyJobTitle';
Expand Down Expand Up @@ -167,7 +167,7 @@ const TypeForm = ({ open, onClose, hideProgressBar = false }) => {
: GA_CATEGORY.SHARE_TIME_SALARY_TYPE_FORM,
action: GA_ACTION.UPLOAD_FAIL,
});
const errorCode = 'ER0007';
const errorCode = ER0007;
rollbar.error(
`[${errorCode}] ${ERROR_CODE_MSG[errorCode].internal} ${error.message}`,
error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { tabType } from '../../../constants/companyJobTitle';
import { createWorkExperienceWithRating } from 'actions/experiences';
import { transferKeyToSnakecase } from 'utils/objectUtil';
import { GA_CATEGORY, GA_ACTION } from 'constants/gaConstants';
import { ERROR_CODE_MSG } from 'constants/errorCodeMsg';
import { ER0020, ERROR_CODE_MSG } from 'constants/errorCodeMsg';

import { sendEvent } from 'utils/hotjarUtil';
import rollbar from 'utils/rollbar';
Expand Down Expand Up @@ -139,7 +139,7 @@ const TypeForm = ({ open, onClose, hideProgressBar = false }) => {
category: GA_CATEGORY.SHARE_WORK,
action: GA_ACTION.UPLOAD_FAIL,
});
const errorCode = 'ER0020';
const errorCode = ER0020;
rollbar.error(
`[${errorCode}] ${ERROR_CODE_MSG[errorCode].internal} ${error.message}`,
error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ConfirmModal from 'common/FormBuilder/Modals/ConfirmModal';
import Footer from './TypeFormFooter';
import { useExperienceCount, useSalaryWorkTimeCount } from 'hooks/useCount';
import rollbar from 'utils/rollbar';
import { ERROR_CODE_MSG } from 'constants/errorCodeMsg';
import { ER0018, ERROR_CODE_MSG } from 'constants/errorCodeMsg';

const SubmittableTypeForm = ({
open,
Expand Down Expand Up @@ -38,7 +38,7 @@ const SubmittableTypeForm = ({
setSubmittedDraft(draft);
setSubmitStatus('success');
} catch (error) {
const errorCode = 'ER0018';
const errorCode = ER0018;
rollbar.error(
`[${errorCode}] ${ERROR_CODE_MSG[errorCode].internal} ${error}`,
error,
Expand Down
Loading

0 comments on commit 1ae00bf

Please sign in to comment.