Skip to content
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
45 changes: 40 additions & 5 deletions src/components/signupComp/SignUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MdCheck, MdDone } from 'react-icons/md';

import { toast } from 'react-toastify';
import { MdCheckCircle } from 'react-icons/md';
import { getCookie, getQueryParamsDeatils, setCookie } from '@/utils/utilis';
import { getCookie, getQueryParamsDeatils, setCookie, getUtmFromCookies, setSharedCookie } from '@/utils/utilis';
import Image from 'next/image';
import StepOne from './StepOne/StepOne';
import StepTwo from './StepTwo/StepTwo';
Expand Down Expand Up @@ -48,7 +48,6 @@ export default class SignUp extends React.Component {

componentDidMount = () => {
this.msg91Query = getCookie('msg91_query');

if (this.msg91Query) {
const queryParams = this.msg91Query.startsWith('?')
? this.msg91Query.replace('?', '&')
Expand All @@ -57,6 +56,12 @@ export default class SignUp extends React.Component {
}

this.otpWidgetSetup();

const urlParams = new URLSearchParams(window.location.search);
urlParams.forEach((value, key) => {
if (key.startsWith('utm_')) setSharedCookie(key, value, 1);
});

const queryParams = getQueryParamsDeatils(this.props?.browserPathCase);
this.setState({ activeStep: queryParams?.code ? 2 : 1 });
if (queryParams?.service) {
Expand All @@ -74,7 +79,17 @@ export default class SignUp extends React.Component {
.then((response) => response?.json())
.then((result) => {
if (result?.status === 'success') {
location.href = SUCCESS_REDIRECTION_URL?.replace(':session', payload.session);
let redirectUrl = SUCCESS_REDIRECTION_URL?.replace(':session', payload.session);
const signupDate = getCookie('signup_date');
const interestedServices = getCookie('interested_services');
if (signupDate) redirectUrl += `&signup_date=${encodeURIComponent(signupDate)}`;
if (interestedServices)
redirectUrl += `&interested_services=${encodeURIComponent(interestedServices)}`;
Object.entries(getUtmFromCookies()).forEach(([key, value]) => {
if (key?.startsWith('utm_') && value)
redirectUrl += `&${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
});
location.href = redirectUrl;
Comment on lines +82 to +92
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There's duplicate code for building the redirect URL with parameters in three different places (lines 82-92, 393-406, and 490-503). Consider extracting this into a reusable function to improve maintainability and reduce the risk of inconsistent behavior.

}
});
} catch (error) {
Expand Down Expand Up @@ -375,10 +390,20 @@ export default class SignUp extends React.Component {
this.setStep(3);
} else if (result?.data?.data?.nextStep === 'loginIntoExistingAccount') {
this.setState({ isLoading: true });
location.href = SUCCESS_REDIRECTION_URL?.replace(
let redirectUrl = SUCCESS_REDIRECTION_URL?.replace(
':session',
result?.data?.sessionDetails?.PHPSESSID
);
const signupDate = getCookie('signup_date');
const interestedServices = getCookie('interested_services');
if (signupDate) redirectUrl += `&signup_date=${encodeURIComponent(signupDate)}`;
if (interestedServices)
redirectUrl += `&interested_services=${encodeURIComponent(interestedServices)}`;
Object.entries(getUtmFromCookies()).forEach(([key, value]) => {
if (key?.startsWith('utm_') && value)
redirectUrl += `&${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
});
location.href = redirectUrl;
} else if (result?.data?.data?.nextStep === 'hasInvitations') {
this.setState({ invitations: result?.data?.data?.invitations });
this.setStep(3);
Expand Down Expand Up @@ -462,10 +487,20 @@ export default class SignUp extends React.Component {
if (result?.status === 'success') {
this.setStep(4);
setTimeout(() => {
location.href = SUCCESS_REDIRECTION_URL?.replace(
let redirectUrl = SUCCESS_REDIRECTION_URL?.replace(
':session',
result?.data?.sessionDetails?.PHPSESSID
);
const signupDate = getCookie('signup_date');
const interestedServices = getCookie('interested_services');
if (signupDate) redirectUrl += `&signup_date=${encodeURIComponent(signupDate)}`;
if (interestedServices)
redirectUrl += `&interested_services=${encodeURIComponent(interestedServices)}`;
Object.entries(getUtmFromCookies()).forEach(([key, value]) => {
if (key?.startsWith('utm_') && value)
redirectUrl += `&${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
});
location.href = redirectUrl;
}, 10);
} else if (result?.hasError) {
toast.error(result?.errors?.[0] ?? result?.errors);
Expand Down
7 changes: 7 additions & 0 deletions src/components/signupComp/StepThree/StepThree.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import axios from 'axios';
import { setSharedCookie } from '@/utils/utilis';
import {
MdKeyboardArrowRight,
MdKeyboardArrowLeft,
Expand Down Expand Up @@ -295,6 +296,7 @@ class StepThree extends React.Component {
};

finalSubmit = () => {
setSharedCookie('signup_date', new Date().toISOString().split('T')[0], 30);
const errors = {
firstNameError: this.validateFirstName() || this.validateMinMax('firstName', 'First Name', null, 50),
lastNameError: this.validateLastName() || this.validateMinMax('lastName', 'Last Name', null, 50),
Expand Down Expand Up @@ -515,6 +517,11 @@ class StepThree extends React.Component {
serviceNeeded: value.map((obj) => obj.value),
},
}));
setSharedCookie(
'interested_services',
JSON.stringify(value.map((obj) => obj.label)),
30
);
Comment on lines +520 to +524
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider adding error handling around the JSON.stringify operation to prevent potential issues if the data structure changes in the future.

Suggested change
setSharedCookie(
'interested_services',
JSON.stringify(value.map((obj) => obj.label)),
30
);
setSharedCookie(
'interested_services',
try {
JSON.stringify(value.map((obj) => obj.label))
} catch (e) {
console.error('Error stringifying services:', e);
'[]'
},
30
);

}}
placeholder='Select Service Needed*'
options={
Expand Down
30 changes: 30 additions & 0 deletions src/utils/utilis.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,36 @@ export function setCookie(name, value, days) {
document.cookie = name + '=' + cookieValue + '; path=/';
}

export function getUtmFromCookies() {
if (typeof document === 'undefined') return {};
const result = {};
document.cookie.split(';').forEach((cookie) => {
const [key, value] = cookie.trim().split('=');
if (key?.startsWith('utm_') && value) result[key] = decodeURIComponent(value);
});
Comment on lines +56 to +59
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The cookie parsing in getUtmFromCookies() might fail if there's a malformed cookie without an equals sign. Consider adding a check before destructuring:

Suggested change
document.cookie.split(';').forEach((cookie) => {
const [key, value] = cookie.trim().split('=');
if (key?.startsWith('utm_') && value) result[key] = decodeURIComponent(value);
});
document.cookie.split(';').forEach((cookie) => {
const parts = cookie.trim().split('=');
if (parts.length >= 2) {
const [key, ...valueParts] = parts;
const value = valueParts.join('='); // Handle values that might contain '='
if (key?.startsWith('utm_') && value) result[key] = decodeURIComponent(value);
}
});

return result;
}

export function setSharedCookie(name, value, days = 1) {
if (typeof document === 'undefined') return;
const expirationDate = new Date();
expirationDate.setDate(expirationDate.getDate() + days);

const isLocalhost = window.location.hostname === 'localhost';
const cookieParts = [
`${name}=${encodeURIComponent(value)}`,
`expires=${expirationDate.toUTCString()}`,
'path=/',
'SameSite=Lax',
];
if (!isLocalhost) {
cookieParts.push('domain=.msg91.com');
cookieParts.push('Secure');
}

document.cookie = cookieParts.join('; ');
}

export function loginWithGitHubAccount(loginProcess) {
let state = Math.floor(100000000 + Math.random() * 900000000);
let otherParams = loginProcess ? `githublogin=true` : `githubsignup=true`;
Expand Down