From b224c9a48ac510fa25aa7cc7136d01e8c3265abf Mon Sep 17 00:00:00 2001 From: pranalidhanavade Date: Wed, 4 Oct 2023 11:45:53 +0530 Subject: [PATCH] fix:sonarcloud issue Signed-off-by: pranalidhanavade --- .../Ecosystems/CreateEcosystems.tsx | 119 +++++++++--------- 1 file changed, 58 insertions(+), 61 deletions(-) diff --git a/src/components/Ecosystems/CreateEcosystems.tsx b/src/components/Ecosystems/CreateEcosystems.tsx index 2ebee1ec3..de23594b1 100644 --- a/src/components/Ecosystems/CreateEcosystems.tsx +++ b/src/components/Ecosystems/CreateEcosystems.tsx @@ -5,28 +5,26 @@ import { Field, Form, Formik, FormikHelpers } from 'formik'; import { IMG_MAX_HEIGHT, IMG_MAX_WIDTH, apiStatusCodes, imageSizeAccepted, storageKeys } from '../../config/CommonConstant' import { calculateSize, dataURItoBlob } from "../../utils/CompressImage"; import { useEffect, useRef, useState } from "react"; - import { AlertComponent } from "../AlertComponent"; import type { AxiosResponse } from 'axios'; import { asset } from '../../lib/data.js'; import { createEcosystems } from "../../api/ecosystems"; -import React from "react"; import { getFromLocalStorage } from "../../api/Auth"; -interface Values { +interface EcoValues { name: string; description: string; } -interface ILogoImage { +interface LogoImage { logoFile: string | File imagePreviewUrl: string | ArrayBuffer | null | File, fileName: string } -const CreateEcosystems = (props: { openModal: boolean; setMessage: (message: string)=> void ; setOpenModal: (flag: boolean) => void }) => { +const CreateEcosystems = (props: { openModal: boolean; setMessage: (message: string) => void; setOpenModal: (flag: boolean) => void }) => { - const [logoImage, setLogoImage] = useState({ + const [ecologoImage, setEcoLogoImage] = useState({ logoFile: "", imagePreviewUrl: "", fileName: '' @@ -34,25 +32,25 @@ const CreateEcosystems = (props: { openModal: boolean; setMessage: (message: str const [loading, setLoading] = useState(false) - const [isImageEmpty, setIsImageEmpty] = useState(true) + const [islogoImageEmpty, setIsLogoImageEmpty] = useState(true) const [initialEcoData, setEcoData] = useState({ name: '', description: '', }) - const [erroMsg, setErrMsg] = useState(null) + const [errorMsg, setErrorMsg] = useState(null) - const [imgError, setImgError] = useState('') + const [imageError, setImageError] = useState('') useEffect(() => { setEcoData({ name: '', description: '', }) - setLogoImage({ - ...logoImage, + setEcoLogoImage({ + ...ecologoImage, logoFile: "", imagePreviewUrl: "" - }) + }) }, [props.openModal]) @@ -85,7 +83,7 @@ const CreateEcosystems = (props: { openModal: boolean; setMessage: (message: str srcEncoded = ctx.canvas.toDataURL(ev, file.type) const blob = dataURItoBlob(srcEncoded, file.type) fileUpdated = new File([blob], file.name, { type: file.type, lastModified: new Date().getTime() }) - setLogoImage({ + setEcoLogoImage({ logoFile: fileUpdated, imagePreviewUrl: srcEncoded, fileName: file.name @@ -99,16 +97,16 @@ const CreateEcosystems = (props: { openModal: boolean; setMessage: (message: str const isEmpty = (object: any): boolean => { // eslint-disable-next-line @typescript-eslint/no-unused-vars, guard-for-in for (const property in object) { - setIsImageEmpty(false) + setIsLogoImageEmpty(false) return false } - setIsImageEmpty(true) + setIsLogoImageEmpty(true) return true } const handleImageChange = (event: any): void => { - setImgError('') + setImageError('') const reader = new FileReader() const file = event?.target?.files @@ -123,48 +121,47 @@ const CreateEcosystems = (props: { openModal: boolean; setMessage: (message: str reader.readAsDataURL(file[0]) event.preventDefault() } else { - setImgError("Please check image size") + setImageError("Please check image size") } } else { - setImgError("Invalid image type") + setImageError("Invalid image type") } } - const submitCreateEcosystem = async (values: Values) => { -try{ - setLoading(true) - const user_data = JSON.parse(await getFromLocalStorage(storageKeys.USER_PROFILE)) - const organizationId = await getFromLocalStorage(storageKeys.ORG_ID); - const ecoData = { - name: values.name, - description: values.description, - logo: logoImage?.imagePreviewUrl as string || "", - tags:"", - orgId: Number(organizationId), - userId:Number(user_data?.id) - } - const resCreateOrg = await createEcosystems(ecoData) + const submitCreateEcosystem = async (values: EcoValues) => { + try { + setLoading(true) + const user_data = JSON.parse(await getFromLocalStorage(storageKeys.USER_PROFILE)) + const organizationId = await getFromLocalStorage(storageKeys.ORG_ID); + const ecoData = { + name: values.name, + description: values.description, + logo: ecologoImage?.imagePreviewUrl as string || "", + tags: "", + orgId: Number(organizationId), + userId: Number(user_data?.id) + } + const resCreateOrg = await createEcosystems(ecoData) - const { data } = resCreateOrg as AxiosResponse - setLoading(false) + const { data } = resCreateOrg as AxiosResponse + setLoading(false) - if (data?.statusCode === apiStatusCodes.API_STATUS_CREATED) { - props.setMessage(data?.message) - props.setOpenModal(false) + if (data?.statusCode === apiStatusCodes.API_STATUS_CREATED) { + props.setMessage(data?.message) + props.setOpenModal(false) - } else { - setErrMsg(resCreateOrg as string) + } else { + setErrorMsg(resCreateOrg as string) + } + } + catch (error) { + console.error("An error occurred:", error); + setLoading(false); } } - catch (error) - { - console.error("An error occurred:", error); - setLoading(false); - } -} return ( { - setLogoImage({ + setEcoLogoImage({ logoFile: "", imagePreviewUrl: "", fileName: '' @@ -175,13 +172,13 @@ try{ }> Create Ecosystem - { - setErrMsg(null) + onAlertClose={() => { + setErrorMsg(null) }} - /> + /> + values: EcoValues, + { resetForm }: FormikHelpers ) => { submitCreateEcosystem(values) @@ -221,13 +218,13 @@ try{ className="items-center sm:flex 2xl:flex sm:space-x-4 xl:space-x-4 2xl:space-x-4" > { - typeof (logoImage.logoFile) === "string" ? + typeof (ecologoImage.logoFile) === "string" ? : Jese picture } @@ -249,7 +246,7 @@ try{ id="ecosystemlogo" title="" onChange={(event): void => handleImageChange(event)} /> {/* {selectedImage || 'No File Chosen'} */} - {imgError ?
{imgError}
: {logoImage.fileName || 'No File Chosen'}} + {imageError ?
{imageError}
: {ecologoImage.fileName || 'No File Chosen'}} @@ -308,13 +305,13 @@ try{