2022-12-29 11:48:24 +00:00
|
|
|
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';
|
2023-06-29 10:00:10 +00:00
|
|
|
import { useAppVersionStore } from '@/_stores/appVersionStore';
|
|
|
|
|
import { shallow } from 'zustand/shallow';
|
2022-12-29 11:48:24 +00:00
|
|
|
|
|
|
|
|
export const CreateVersion = ({
|
|
|
|
|
appId,
|
|
|
|
|
appVersions,
|
|
|
|
|
setAppVersions,
|
|
|
|
|
setAppDefinitionFromVersion,
|
|
|
|
|
showCreateAppVersion,
|
|
|
|
|
setShowCreateAppVersion,
|
|
|
|
|
}) => {
|
|
|
|
|
const [isCreatingVersion, setIsCreatingVersion] = useState(false);
|
|
|
|
|
const [versionName, setVersionName] = useState('');
|
|
|
|
|
const { t } = useTranslation();
|
2023-06-29 10:00:10 +00:00
|
|
|
const { editingVersion } = useAppVersionStore(
|
|
|
|
|
(state) => ({
|
|
|
|
|
editingVersion: state.editingVersion,
|
|
|
|
|
}),
|
|
|
|
|
shallow
|
|
|
|
|
);
|
2023-07-18 07:41:31 +00:00
|
|
|
|
2022-12-29 11:48:24 +00:00
|
|
|
const createVersion = () => {
|
|
|
|
|
if (versionName.trim().length > 25) {
|
2023-01-04 07:58:55 +00:00
|
|
|
toast.error('Version name should not be longer than 25 characters');
|
2022-12-29 11:48:24 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (versionName.trim() == '') {
|
2023-01-04 07:58:55 +00:00
|
|
|
toast.error('Version name should not be empty');
|
2022-12-29 11:48:24 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setIsCreatingVersion(true);
|
|
|
|
|
appVersionService
|
|
|
|
|
.create(appId, versionName, editingVersion.id)
|
|
|
|
|
.then(() => {
|
|
|
|
|
toast.success('Version Created');
|
|
|
|
|
appVersionService.getAll(appId).then((data) => {
|
|
|
|
|
setVersionName('');
|
|
|
|
|
setIsCreatingVersion(false);
|
|
|
|
|
setAppVersions(data.versions);
|
|
|
|
|
const latestVersion = data.versions.at(0);
|
|
|
|
|
setAppDefinitionFromVersion(latestVersion);
|
|
|
|
|
setShowCreateAppVersion(false);
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
toast.error(error?.error);
|
|
|
|
|
setIsCreatingVersion(false);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const options = appVersions.map((version) => {
|
|
|
|
|
return { label: version.name, value: version };
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<AlertDialog
|
2023-06-29 10:00:10 +00:00
|
|
|
show={showCreateAppVersion}
|
2023-01-04 12:11:40 +00:00
|
|
|
closeModal={() => {
|
2023-05-11 07:02:21 +00:00
|
|
|
setVersionName('');
|
2023-01-04 12:11:40 +00:00
|
|
|
setShowCreateAppVersion(false);
|
|
|
|
|
}}
|
2022-12-29 11:48:24 +00:00
|
|
|
title={t('editor.appVersionManager.createVersion', 'Create new version')}
|
|
|
|
|
>
|
2023-01-05 09:06:39 +00:00
|
|
|
<form
|
|
|
|
|
onSubmit={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
createVersion();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<div className="mb-3 pb-2">
|
2023-09-04 08:00:39 +00:00
|
|
|
<div className="col tj-app-input">
|
2023-03-20 05:13:08 +00:00
|
|
|
<label className="form-label" data-cy="version-name-label">
|
|
|
|
|
{t('editor.appVersionManager.versionName', 'Version Name')}
|
|
|
|
|
</label>
|
2023-01-05 09:06:39 +00:00
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
onChange={(e) => setVersionName(e.target.value)}
|
|
|
|
|
className="form-control"
|
2023-02-02 09:11:25 +00:00
|
|
|
data-cy="version-name-input-field"
|
2023-01-05 09:06:39 +00:00
|
|
|
placeholder={t('editor.appVersionManager.enterVersionName', 'Enter version name')}
|
|
|
|
|
disabled={isCreatingVersion}
|
|
|
|
|
value={versionName}
|
|
|
|
|
autoFocus={true}
|
|
|
|
|
minLength="1"
|
|
|
|
|
maxLength="25"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2022-12-29 11:48:24 +00:00
|
|
|
</div>
|
|
|
|
|
|
2023-05-11 07:02:21 +00:00
|
|
|
<div className="mb-4 pb-2 version-select">
|
2023-02-02 09:11:25 +00:00
|
|
|
<label className="form-label" data-cy="create-version-from-label">
|
|
|
|
|
{t('editor.appVersionManager.createVersionFrom', 'Create version from')}
|
|
|
|
|
</label>
|
|
|
|
|
<div className="ts-control" data-cy="create-version-from-input-field">
|
2023-01-05 09:06:39 +00:00
|
|
|
<Select
|
|
|
|
|
options={options}
|
2023-05-11 07:02:21 +00:00
|
|
|
defaultValue={options.find((option) => option?.value?.id === editingVersion?.id)}
|
2023-01-05 09:06:39 +00:00
|
|
|
onChange={(version) => {
|
2023-03-31 14:29:06 +00:00
|
|
|
setAppDefinitionFromVersion(version, false);
|
2023-01-05 09:06:39 +00:00
|
|
|
}}
|
|
|
|
|
useMenuPortal={false}
|
|
|
|
|
width="100%"
|
|
|
|
|
maxMenuHeight={100}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2022-12-29 11:48:24 +00:00
|
|
|
</div>
|
|
|
|
|
|
2023-01-05 09:06:39 +00:00
|
|
|
<div className="mb-3">
|
|
|
|
|
<div className="col d-flex justify-content-end">
|
|
|
|
|
<button
|
|
|
|
|
className="btn mx-2"
|
2023-02-02 09:11:25 +00:00
|
|
|
data-cy="cancel-button"
|
2023-01-05 09:06:39 +00:00
|
|
|
onClick={() => {
|
2023-05-11 07:02:21 +00:00
|
|
|
setVersionName('');
|
2023-01-05 09:06:39 +00:00
|
|
|
setShowCreateAppVersion(false);
|
|
|
|
|
}}
|
|
|
|
|
type="button"
|
2022-12-29 11:48:24 +00:00
|
|
|
>
|
2023-01-05 09:06:39 +00:00
|
|
|
{t('globals.cancel', 'Cancel')}
|
|
|
|
|
</button>
|
2023-02-02 09:11:25 +00:00
|
|
|
<button
|
|
|
|
|
className={`btn btn-primary ${isCreatingVersion ? 'btn-loading' : ''}`}
|
|
|
|
|
data-cy="create-new-version-button"
|
|
|
|
|
type="submit"
|
|
|
|
|
>
|
2023-01-05 09:06:39 +00:00
|
|
|
{t('editor.appVersionManager.createVersion', 'Create Version')}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2022-12-29 11:48:24 +00:00
|
|
|
</div>
|
2023-01-05 09:06:39 +00:00
|
|
|
</form>
|
2022-12-29 11:48:24 +00:00
|
|
|
</AlertDialog>
|
|
|
|
|
);
|
|
|
|
|
};
|