Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ small refactor of router.querys and 🐛 bug solving realted to usePersistent and coupons #1615

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion public/locales/en/signup.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@
"validate-email-title": "Verify your email",
"validate-email-description": "We sent a confirmation email to: <strong>{{email}}</strong>, please check your email and click on the confirmation link.",
"email-sent-to": "We sent you a confirmation email to {{email}}!",
"email-already-sent": "You have a pending invitation sent less than 1 day ago, check your email"
"email-already-sent": "You have a pending invitation sent less than 1 day ago, check your email",
"coupon-already-applied": "Coupon already applied"
},
"alert-message-validate-email": {
"title": "Verify email",
Expand Down
3 changes: 2 additions & 1 deletion public/locales/es/signup.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@
"validate-email-title": "Verifique su correo electrónico",
"validate-email-description": "Enviamos un correo electrónico de confirmación a: <strong>{{email}}</strong>, verifica tu correo electrónico y haz clic en el enlace de confirmación.",
"email-sent-to": "Enviamos un correo electrónico de confirmación a {{email}}!",
"email-already-sent": "Tienes una invitación pendiente enviada hace menos de 1 día, revisa tu correo"
"email-already-sent": "Tienes una invitación pendiente enviada hace menos de 1 día, revisa tu correo",
"coupon-already-applied": "El cupón ya esta siendo aplicado"
},
"alert-message-validate-email": {
"title": "Verificar correo electrónico",
Expand Down
4 changes: 0 additions & 4 deletions src/common/components/MktShowPrices.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,9 @@ function MktShowPrices({ id, externalPlanProps, cohortId, title, gridColumn1, gr
firstSectionTitle={t('subscription.upgrade-modal.subscription')}
secondSectionTitle={t('subscription.upgrade-modal.finance')}
handleUpgrade={(item) => {
const period = item?.period;

const querys = parseQuerys({
plan: item?.plan_slug,
plan_id: item?.plan_id,
price: item?.price,
period,
cohort: cohortId,
coupon: queryCoupon || coupon,
});
Expand Down
2 changes: 0 additions & 2 deletions src/common/components/PricingCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ export default function PricingCard({ item, courseData, isFetching, relatedSubsc
const qs = parseQuerys({
plan: selectedFinancing?.plan_slug || item?.plan_slug,
plan_id: selectedFinancing?.plan_id || item?.plan_id,
price: selectedFinancing?.price || item?.price,
period: selectedFinancing?.period || item?.period,
coupon: coupon || queryCoupon,
cohort: courseData?.cohort?.id,
});
Expand Down
23 changes: 13 additions & 10 deletions src/common/hooks/usePersistent.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,26 @@ const usePersistent = (key, initialValue) => {

const usePersistentBySession = (key, initialValue) => {
const getStoredValues = useMemo(() => {
if (typeof window === 'undefined') return initialValue;

const item = isWindow ? window.sessionStorage.getItem(key) : null;
const isObject = typeof item === 'object';
const objectValue = JSON.parse(item) || initialValue;
return isObject ? objectValue : item || initialValue;
if (item === null) return initialValue;

if (item.startsWith('{') || item.startsWith('[')) {
return JSON.parse(item);
}

return item;
}, [key, initialValue]);

const [storedValue, setStoredValue] = useState(getStoredValues);

const setValue = (value) => {
try {
setStoredValue(value);
window.sessionStorage.setItem(key, JSON.stringify(value));
} catch (error) {
console.error('usePersistent_error:', error);
}
setStoredValue(value);
const valueToStore = typeof value === 'object' ? JSON.stringify(value) : value;
window.sessionStorage.setItem(key, valueToStore);
};
return [storedValue, setValue];
return [storedValue !== undefined ? storedValue : initialValue, setValue];
};

export {
Expand Down
2 changes: 0 additions & 2 deletions src/js_modules/checkout/Summary.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ function Summary() {
query: {
...router.query,
plan_id: selectedPlanCheckoutData?.plan_id,
price: selectedPlanCheckoutData?.price,
period: selectedPlanCheckoutData?.period,
},
});
} else {
Expand Down
2 changes: 0 additions & 2 deletions src/pages/bootcamp/[course_slug].jsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,6 @@ function Page({ data }) {
plan: featuredPlanToEnroll?.plan_slug,
plan_id: featuredPlanToEnroll?.plan_id,
has_available_cohorts: planData?.has_available_cohorts,
price: featuredPlanToEnroll?.price,
period: featuredPlanToEnroll?.period,
cohort: cohortId,
}) : `?plan=${data?.plan_slug}&cohort=${cohortId}`;

Expand Down
22 changes: 19 additions & 3 deletions src/pages/checkout/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ function Checkout() {
const accessToken = getStorageItem('accessToken');
const tokenExists = accessToken !== null && accessToken !== undefined && accessToken.length > 5;
const { coupon: couponQuery } = query;
const { course } = router.query;
const courseChoosed = course;

const [coupon] = usePersistentBySession('coupon', '');

const couponValue = useMemo(() => {
Expand All @@ -121,9 +124,6 @@ function Checkout() {
return couponString || formatedCouponQuery;
}, [coupon, couponQuery]);

const { course } = router.query;
const courseChoosed = course;

const queryPlanExists = planFormated !== undefined && planFormated?.length > 0;
const queryMentorshipServiceSlugExists = mentorshipServiceSetSlug && mentorshipServiceSetSlug?.length > 0;
const queryEventTypeSetSlugExists = eventTypeSetSlug && eventTypeSetSlug?.length > 0;
Expand Down Expand Up @@ -161,6 +161,22 @@ function Checkout() {
};

const handleCoupon = (coupons, actions) => {
const alreadyAppliedCoupon = selfAppliedCoupon?.slug === discountCode || selfAppliedCoupon?.slug === couponValue;

if (alreadyAppliedCoupon) {
toast({
position: 'top',
title: t('signup:alert-message.coupon-already-applied'),
status: 'info',
duration: 4000,
isClosable: true,
});
if (actions) {
actions.setSubmitting(false);
}
return;
}

bc.payment({
coupons: [coupons || discountCode],
plan: planFormated,
Expand Down