import React, { useEffect, useState } from 'react'; import { appVersionService, authenticationService, gitSyncService } from '@/_services'; import AlertDialog from '@/_ui/AlertDialog'; import { Alert } from '@/_ui/Alert'; import { toast } from 'react-hot-toast'; import { useTranslation } from 'react-i18next'; import Select from '@/_ui/Select'; import { shallow } from 'zustand/shallow'; import useStore from '@/AppBuilder/_stores/store'; import { useModuleContext } from '@/AppBuilder/_contexts/ModuleContext'; const CreateVersionModal = ({ showCreateAppVersion, setShowCreateAppVersion, handleCommitEnableChange, canCommit, orgGit, fetchingOrgGit, handleCommitOnVersionCreation = () => {}, }) => { const { moduleId } = useModuleContext(); const [isCreatingVersion, setIsCreatingVersion] = useState(false); const [versionName, setVersionName] = useState(''); const isGitSyncEnabled = orgGit?.org_git?.git_ssh?.is_enabled || orgGit?.org_git?.git_https?.is_enabled || orgGit?.org_git?.git_lab?.is_enabled; const { createNewVersionAction, fetchDevelopmentVersions, developmentVersions, appId, setCurrentVersionId, selectedVersion, currentMode, } = useStore( (state) => ({ createNewVersionAction: state.createNewVersionAction, selectedEnvironment: state.selectedEnvironment, fetchDevelopmentVersions: state.fetchDevelopmentVersions, developmentVersions: state.developmentVersions, featureAccess: state.license.featureAccess, editingVersion: state.currentVersionId, appId: state.appStore.modules[moduleId].app.appId, currentVersionId: state.currentVersionId, setCurrentVersionId: state.setCurrentVersionId, selectedVersion: state.selectedVersion, currentMode: state.currentMode, }), shallow ); const [selectedVersionForCreation, setSelectedVersionForCreation] = useState(null); useEffect(() => { fetchDevelopmentVersions(appId); }, []); useEffect(() => { if (developmentVersions?.length && selectedVersion?.id) { const selected = developmentVersions.find((version) => version?.id === selectedVersion?.id) || null; setSelectedVersionForCreation(selected); } }, [developmentVersions, selectedVersion]); const { t } = useTranslation(); const options = developmentVersions.map((version) => { return { label: version.name, value: version }; }); const createVersion = () => { if (versionName.trim().length > 25) { toast.error('Version name should not be longer than 25 characters'); return; } if (versionName.trim() == '') { toast.error('Version name should not be empty'); return; } if (selectedVersionForCreation === undefined) { toast.error('Please select a version from.'); return; } setIsCreatingVersion(true); //TODO: pass environmentId to the func createNewVersionAction( appId, versionName, selectedVersionForCreation.id, (newVersion) => { toast.success('Version Created'); setVersionName(''); setIsCreatingVersion(false); setShowCreateAppVersion(false); appVersionService .getAppVersionData(appId, newVersion.id, currentMode) .then((data) => { setCurrentVersionId(newVersion.id); handleCommitOnVersionCreation(data); }) .catch((error) => { toast.error(error); }); }, (error) => { if (error?.data?.code === '23505') { toast.error('Version name already exists.'); } else { toast.error(error?.error); } setIsCreatingVersion(false); } ); }; return ( { setVersionName(''); setShowCreateAppVersion(false); }} title={t('editor.appVersionManager.createVersion', 'Create new version')} customClassName="git-sync-modal" > {fetchingOrgGit ? (
) : (
{ e.preventDefault(); createVersion(); }} >
setVersionName(e.target.value)} className="form-control" data-cy="version-name-input-field" placeholder={t('editor.appVersionManager.enterVersionName', 'Enter version name')} disabled={isCreatingVersion} value={versionName} autoFocus={true} minLength="1" maxLength="25" />
Commit changes
This will commit the creation of the new version to the git repo
)}
The new version will be created in development environment
)}
); }; export default CreateVersionModal;