Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10,710 changes: 5,074 additions & 5,636 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"@storybook/testing-library": "^0.0.11",
"@types/d3-ease": "^1.0.9",
"@types/node": "^14.0.26",
"@types/react": "^16.9.43",
"@types/react": "^18.0.1",
"@types/react-map-gl": "^5.2.7",
"@types/styled-jsx": "^2.2.8",
"@types/uuid": "^8.3.0",
Expand Down
147 changes: 72 additions & 75 deletions pages/claim/[type]/[code].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { UserPropsContext } from '../../../src/features/common/Layout/UserPropsC
import { ErrorHandlingContext } from '../../../src/features/common/Layout/ErrorHandlingContext';
import ShareOptions from '../../../src/features/common/ShareOptions/ShareOptions';
import { styled } from '@mui/material';
import CancelIcon from '../../../public/assets/images/icons/CancelIcon';

const { useTranslation } = i18next;

Expand Down Expand Up @@ -87,27 +88,26 @@ function ClaimDonation({}: Props): ReactElement {
}
}, [router]);

async function validateCode(code: any, type: any) {
async function redeemCode(code: string) {
setIsUploadingData(true);
const submitData = {
type: type,
// type: type,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove commented code.

code: code,
};
if (contextLoaded && user) {
const userLang = localStorage.getItem('language') || 'en';
postAuthenticatedRequest(
`/api/v1.3/${userLang}/validateCode`,
`/app/redeem`,
submitData,
token,
handleError
).then((res) => {
if (res.code === 401) {
setErrorMessage(res.message);
if (res.error_code === 'already_redeemed') {
setErrorMessage(t('redeem:alreadyRedeemed'));
setIsUploadingData(false);
} else if (res.status === 'error') {
setErrorMessage(res.errorText || t('me:wentWrong'));
} else if (res.error_code === 'invalid_code') {
setErrorMessage(t('redeem:invalidCode'));
setIsUploadingData(false);
} else if (res.status === 'success') {
} else if (res.status === 'redeemed') {
setCodeValidated(true);
setValidCodeData(res);
setIsUploadingData(false);
Expand All @@ -125,8 +125,8 @@ function ClaimDonation({}: Props): ReactElement {
// From here user can go back to home by clicking X
if (contextLoaded && user) {
// validate code
if (routerReady && code && type) {
validateCode(code, type);
if (routerReady && code) {
redeemCode(code);
}
}

Expand All @@ -145,34 +145,34 @@ function ClaimDonation({}: Props): ReactElement {
}
}, [contextLoaded, user, code]);

async function redeemCode(code: any, type: any) {
setIsUploadingData(true);
const submitData = {
type: type,
code: code,
};
if (contextLoaded && user) {
const userLang = localStorage.getItem('language') || 'en';
postAuthenticatedRequest(
`/api/v1.3/${userLang}/convertCode`,
submitData,
token,
handleError
).then((res) => {
if (res.code === 401) {
setErrorMessage(res.message);
setIsUploadingData(false);
} else if (!res.response || res.response.status === 'error') {
setErrorMessage(res.errorText || t('me:wentWrong'));
setIsUploadingData(false);
} else if (res.response.status === 'success') {
setCodeRedeemed(true);
setIsUploadingData(false);
setCodeValidated(false);
}
});
}
}
// async function redeemCode(code: any, type: any) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove unused code. We have git if we want to retrieve it again.

// setIsUploadingData(true);
// const submitData = {
// type: type,
// code: code,
// };
// if (contextLoaded && user) {
// const userLang = localStorage.getItem('language') || 'en';
// postAuthenticatedRequest(
// `/api/v1.3/${userLang}/convertCode`,
// submitData,
// token,
// handleError
// ).then((res) => {
// if (res.code === 401) {
// setErrorMessage(res.message);
// setIsUploadingData(false);
// } else if (!res.response || res.response.status === 'error') {
// setErrorMessage(res.errorText || t('me:wentWrong'));
// setIsUploadingData(false);
// } else if (res.response.status === 'success') {
// setCodeRedeemed(true);
// setIsUploadingData(false);
// setCodeValidated(false);
// }
// });
// }
// }

Comment thread
sunilsabatp marked this conversation as resolved.
const closeRedeem = () => {
setCodeValidated(false);
Expand Down Expand Up @@ -288,61 +288,58 @@ function ClaimDonation({}: Props): ReactElement {
<div className={styles.modal}>
{codeValidated && validCodeData ? (
<>
{errorMessage && (
<span className={styles.formErrors}>{errorMessage}</span>
)}
<div style={{ position: 'relative', left: '108px' }}>
<button className={styles.crossButton}>
<CancelIcon />
</button>
</div>

<div className={styles.codeTreeCount}>
{getFormattedNumber(
i18n.language,
Number(validCodeData.treeCount)
Number(validCodeData.units)
)}
<span>
{t('common:tree', {
count: Number(validCodeData.treeCount),
count: Number(validCodeData.units),
})}
</span>
</div>

{validCodeData.tpos?.length > 0 && (
<div className={styles.plantedBy}>
<span>{t('common:plantedBy')}</span>
<p>{validCodeData.tpos[0].tpoName}</p>
</div>
)}

<div
onClick={() => redeemCode(code, type)}
className="primaryButton"
style={{ maxWidth: '200px', marginTop: '24px' }}
>
{isUploadingData ? (
<div className={styles.spinner}></div>
) : (
t('redeem:addToMyTrees')
)}
<div className={styles.codeTreeCount}>
<span>{t('redeem:successfullyRedeemed')}</span>
</div>
</>
) : (
<>
<div>
{t('redeem:validating')} {code}
<div style={{ position: 'relative', left: '108px' }}>
<button className={styles.crossButton} onClick={closeRedeem}>
<CancelIcon />
</button>
</div>
{errorMessage ? (
<div style={{ fontWeight: 'bold' }}>{code}</div>
) : (
<div style={{ fontWeight: 'bold' }}>
{t('redeem:redeeming')} {code}
</div>
)}

{errorMessage && (
{errorMessage ? (
<span className={styles.formErrors}>{errorMessage}</span>
) : (
<div
onClick={() => redeemCode(code)}
className="primaryButton"
style={{ maxWidth: '200px', marginTop: '24px' }}
>
{isUploadingData ? (
<div className={styles.spinner}></div>
) : (
t('redeem:redeemCode')
)}
</div>
)}
<div
onClick={() => validateCode(code, type)}
className="primaryButton"
style={{ maxWidth: '200px', marginTop: '24px' }}
>
{isUploadingData ? (
<div className={styles.spinner}></div>
) : (
t('redeem:validateCode')
)}
</div>
</>
)}
</div>
Expand Down
11 changes: 9 additions & 2 deletions public/static/locales/en/redeem.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
"addToMyTrees": "Add to my trees",
"validateCode": "Validate Code",
"enterRedeemCode": "Please enter a code to redeem",
"validating": "Validating",
"invalidType": "Invalid type, please check the link again"
"invalidType": "Invalid type, please check the link again",
"successfullyRedeemed": "Successfully Redeemed",
"exitToProfile": "Exit to Profile",
"redeemCode": "Redeem Code",
"alreadyRedeemed": "The code has already been redeemed",
"invalidCode": "This code is invalid",
"redeemAnotherCode": "Redeem Another Code",
"redeeming": "Redeeming:"
}

55 changes: 28 additions & 27 deletions src/features/common/Layout/UserPropsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,37 @@ function UserPropsProvider({ children }: any): ReactElement {
logout({ returnTo: returnUrl });
};

React.useEffect(() => {
async function loadUser() {
setContextLoaded(false);
try {
const res = await getAccountInfo(token);
if (res.status === 200) {
const resJson = await res.json();
setUser(resJson);
} else if (res.status === 303) {
// if 303 -> user doesn not exist in db
setUser(null);
if (typeof window !== 'undefined') {
router.push('/complete-signup', undefined, { shallow: true });
}
} else if (res.status === 401) {
// in case of 401 - invalid token: signIn()
setUser(false);
setToken(null);
loginWithRedirect({
redirectUri: `${process.env.NEXTAUTH_URL}/login`,
ui_locales: localStorage.getItem('language') || 'en',
});
} else {
// any other error
async function loadUser() {
setContextLoaded(false);
try {
const res = await getAccountInfo(token);
if (res.status === 200) {
const resJson = await res.json();
setUser(resJson);
} else if (res.status === 303) {
// if 303 -> user doesn not exist in db
setUser(null);
if (typeof window !== 'undefined') {
router.push('/complete-signup', undefined, { shallow: true });
}
} catch (err) {
console.log(err);
} else if (res.status === 401) {
// in case of 401 - invalid token: signIn()
setUser(false);
setToken(null);
loginWithRedirect({
redirectUri: `${process.env.NEXTAUTH_URL}/login`,
ui_locales: localStorage.getItem('language') || 'en',
});
} else {
// any other error
}
setContextLoaded(true);
} catch (err) {
console.log(err);
}
setContextLoaded(true);
}

React.useEffect(() => {
Comment thread
mohitb35 marked this conversation as resolved.
if (token) loadUser();
}, [token]);

Expand Down
Loading