import React, { useState, useEffect, useRef } from 'react'; import { toast } from 'react-hot-toast'; import Modal from '../HomePage/Modal'; import { ButtonSolid } from '@/_ui/AppButton/AppButton'; import _ from 'lodash'; import { validateName } from '@/_helpers/utils'; import { FormWrapper } from './FormWrapper'; export function AppModal({ closeModal, processApp, show, fileContent = null, templateDetails = null, selectedAppId = null, selectedAppName = null, title, actionButton, actionLoadingButton, }) { if (!selectedAppName && templateDetails) { selectedAppName = templateDetails?.name || ''; } else if (!selectedAppName) { selectedAppName = ''; } if (actionButton === 'Clone app') { if (selectedAppName.length >= 45) { selectedAppName = selectedAppName.slice(0, 45) + '_Copy'; } else { selectedAppName = selectedAppName + '_Copy'; } } const [deploying, setDeploying] = useState(false); const [newAppName, setNewAppName] = useState(selectedAppName); const [errorText, setErrorText] = useState(''); const [infoText, setInfoText] = useState(''); const [isLoading, setIsLoading] = useState(false); const [isNameChanged, setIsNameChanged] = useState(false); const [isSuccess, setIsSuccess] = useState(false); const [clearInput, setClearInput] = useState(false); const inputRef = useRef(null); useEffect(() => { setIsNameChanged(newAppName?.trim() !== selectedAppName); }, [newAppName, selectedAppName]); useEffect(() => { setIsSuccess(false); }, [show]); useEffect(() => { inputRef.current?.select(); }, [show]); useEffect(() => { setIsSuccess(false); setClearInput(false); setNewAppName(selectedAppName); }, [selectedAppName]); const handleAction = async (e) => { setDeploying(true); const trimmedAppName = newAppName.trim(); setNewAppName(trimmedAppName); if (!errorText) { setIsLoading(true); try { let success = true; //create app from template if (templateDetails) { success = await processApp(e, trimmedAppName, templateDetails); //import app } else if (fileContent) { success = await processApp(fileContent, trimmedAppName); //rename app/clone existing app } else if (selectedAppId) { success = await processApp(trimmedAppName, selectedAppId); //create app from scratch } else { success = await processApp(trimmedAppName); } if (success === false) { setErrorText('App name already exists'); setInfoText(''); } else { setErrorText(''); setInfoText(''); closeModal(); } } catch (error) { let errorMessage = 'Some Error Occured'; if (error?.error) { errorMessage = error.error; } else if (error?.message) { errorMessage = error.message; } toast.error(errorMessage, { position: 'top-center', }); } } setIsLoading(false); }; const handleInputChange = (e) => { const newAppName = e.target.value; const trimmedName = newAppName.trim(); setNewAppName(newAppName); if (newAppName.length >= 50) { setInfoText('Maximum length has been reached'); } else { setInfoText(''); const error = validateName(trimmedName, 'App', false); setErrorText(error?.errorMsg || ''); } }; const createBtnDisableState = isLoading || errorText || (actionButton === 'Rename app' && (!isNameChanged || newAppName.trim().length === 0 || newAppName.length > 50)) || // For rename case (actionButton !== 'Rename app' && (newAppName.length > 50 || newAppName.trim().length === 0)); return ( Cancel {isLoading ? actionLoadingButton : actionButton} } >
{errorText ? ( {errorText} ) : infoText || newAppName.length >= 50 ? ( {infoText || 'Maximum length has been reached'} ) : ( App name must be unique and max 50 characters )}
); }