import React, { useState } from 'react'; import { appVersionService } from '@/_services'; import AlertDialog from '@/_ui/AlertDialog'; import { toast } from 'react-hot-toast'; import { useTranslation } from 'react-i18next'; import Select from '@/_ui/Select'; import { useAppVersionStore } from '@/_stores/appVersionStore'; import { shallow } from 'zustand/shallow'; export const CreateVersion = ({ appId, appVersions, setAppVersions, setAppDefinitionFromVersion, showCreateAppVersion, setShowCreateAppVersion, }) => { const [isCreatingVersion, setIsCreatingVersion] = useState(false); const [versionName, setVersionName] = useState(''); const { t } = useTranslation(); const { editingVersion } = useAppVersionStore( (state) => ({ editingVersion: state.editingVersion, }), shallow ); const options = appVersions.map((version) => { return { label: version.name, value: version }; }); const [selectedVersion, setSelectedVersion] = useState( () => options.find((option) => option?.value?.id === editingVersion?.id)?.value ); 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; } setIsCreatingVersion(true); appVersionService .create(appId, versionName, selectedVersion.id) .then((data) => { toast.success('Version Created'); appVersionService.getAll(appId).then((data) => { setVersionName(''); setIsCreatingVersion(false); setAppVersions(data.versions); setShowCreateAppVersion(false); }); appVersionService .getAppVersionData(appId, data.id) .then((data) => { setAppDefinitionFromVersion(data); }) .catch((error) => { toast.error(error); }); }) .catch((error) => { toast.error(error?.error); setIsCreatingVersion(false); }); }; return ( { setVersionName(''); setShowCreateAppVersion(false); }} title={t('editor.appVersionManager.createVersion', 'Create new version')} >
{ 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" />