Skip to content

Commit

Permalink
changes for verification flow checkbox changes (#857)
Browse files Browse the repository at this point in the history
Signed-off-by: pranalidhanavade <[email protected]>
  • Loading branch information
pranalidhanavade authored Jan 6, 2025
1 parent 5a26fde commit 4778fac
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions src/commonComponents/CustomCheckbox.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import React, { useEffect, useState } from 'react';
import { getFromLocalStorage, setToLocalStorage } from '../api/Auth';
import { setToLocalStorage } from '../api/Auth';
import { storageKeys } from '../config/CommonConstant';
import type { ICustomCheckboxProps, ISchemaData } from './interface';

const CustomCheckbox: React.FC<ICustomCheckboxProps> = ({ showCheckbox, isVerificationUsingEmail, onChange, schemaData }) => {
const [checked, setChecked] = useState<boolean>(false);

useEffect(() => {
const fetchSelectedSchemas = async () => {
if (schemaData) {
try {
const storedSchemaData = await getFromLocalStorage(storageKeys.SELECTED_SCHEMAS);
if(storedSchemaData){
const selectedSchemas= JSON.parse(storedSchemaData);
const isChecked = selectedSchemas.some((schema: ISchemaData) => schema.schemaId === schemaData.schemaId);
setChecked(isChecked);
}
} catch (error) {
console.error('Error parsing JSON from localStorage:', error);
}
if (schemaData) {
try {
const selectedSchemas = JSON.parse(localStorage.getItem('selectedSchemas') ?? '[]');
const isChecked = selectedSchemas.some((schema: ISchemaData) => schema.schemaId === schemaData.schemaId);
setChecked(isChecked);
} catch (error) {
console.error('Error parsing JSON from localStorage:', error);
}
};
fetchSelectedSchemas();
}
}, [schemaData]);

const handleCheckboxChange = async () => {
Expand All @@ -30,22 +24,17 @@ const CustomCheckbox: React.FC<ICustomCheckboxProps> = ({ showCheckbox, isVerifi
onChange(newChecked, schemaData);

try {
const storedSchemaData = await getFromLocalStorage(storageKeys.SELECTED_SCHEMAS);
if(storedSchemaData){
const selectedSchemas = JSON.parse(storedSchemaData);
if (newChecked) {
selectedSchemas.push(schemaData);
} else {
const index = selectedSchemas.findIndex((schema: ISchemaData) => schema.schemaId === schemaData?.schemaId);
if (index > -1) {
selectedSchemas.splice(index, 1);
}
}
await setToLocalStorage(storageKeys.SELECTED_SCHEMAS, JSON.stringify(selectedSchemas));
const selectedSchemas = JSON.parse(localStorage.getItem('selectedSchemas') ?? '[]');

if (newChecked) {
selectedSchemas.push(schemaData);
} else {
throw new Error('Schema data is empty.');

const index = selectedSchemas.findIndex((schema: ISchemaData) => schema.schemaId === schemaData?.schemaId);
if (index > -1) {
selectedSchemas.splice(index, 1);
}
}
await setToLocalStorage(storageKeys.SELECTED_SCHEMAS, JSON.stringify(selectedSchemas));
} catch (error) {
console.error('Error updating localStorage:', error);
}
Expand Down

0 comments on commit 4778fac

Please sign in to comment.