2022-01-19 14:28:55 +01:00
|
|
|
import { useEffect, useState } from 'react';
|
2022-03-28 10:49:59 +02:00
|
|
|
import useProjectApi from 'hooks/api/actions/useProjectApi/useProjectApi';
|
2022-06-07 15:03:40 +02:00
|
|
|
import { formatUnknownError } from 'utils/formatUnknownError';
|
2023-10-04 09:42:02 +02:00
|
|
|
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
|
|
|
|
import { ProjectMode } from './useProjectEnterpriseSettingsForm';
|
2022-01-19 14:28:55 +01:00
|
|
|
|
2023-03-22 19:04:02 +01:00
|
|
|
export const DEFAULT_PROJECT_STICKINESS = 'default';
|
2022-01-19 14:28:55 +01:00
|
|
|
const useProjectForm = (
|
|
|
|
initialProjectId = '',
|
|
|
|
initialProjectName = '',
|
2023-03-10 11:28:02 +01:00
|
|
|
initialProjectDesc = '',
|
2023-04-04 10:46:28 +02:00
|
|
|
initialProjectStickiness = DEFAULT_PROJECT_STICKINESS,
|
2023-09-04 13:53:33 +02:00
|
|
|
initialFeatureLimit = '',
|
2023-10-04 09:42:02 +02:00
|
|
|
initialProjectMode: ProjectMode = 'open',
|
2022-01-19 14:28:55 +01:00
|
|
|
) => {
|
2023-10-04 09:42:02 +02:00
|
|
|
const { isEnterprise } = useUiConfig();
|
2022-01-19 14:28:55 +01:00
|
|
|
const [projectId, setProjectId] = useState(initialProjectId);
|
2023-10-04 09:42:02 +02:00
|
|
|
const [projectMode, setProjectMode] =
|
|
|
|
useState<ProjectMode>(initialProjectMode);
|
2022-01-19 14:28:55 +01:00
|
|
|
const [projectName, setProjectName] = useState(initialProjectName);
|
|
|
|
const [projectDesc, setProjectDesc] = useState(initialProjectDesc);
|
2023-04-04 10:46:28 +02:00
|
|
|
const [projectStickiness, setProjectStickiness] = useState<string>(
|
2023-10-02 14:25:46 +02:00
|
|
|
initialProjectStickiness,
|
2023-04-04 10:46:28 +02:00
|
|
|
);
|
2023-07-11 13:55:43 +02:00
|
|
|
const [featureLimit, setFeatureLimit] =
|
|
|
|
useState<string>(initialFeatureLimit);
|
2023-09-06 10:13:28 +02:00
|
|
|
|
2022-01-19 14:28:55 +01:00
|
|
|
const [errors, setErrors] = useState({});
|
2023-03-10 11:28:02 +01:00
|
|
|
|
2022-01-19 14:28:55 +01:00
|
|
|
const { validateId } = useProjectApi();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setProjectId(initialProjectId);
|
|
|
|
}, [initialProjectId]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setProjectName(initialProjectName);
|
|
|
|
}, [initialProjectName]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setProjectDesc(initialProjectDesc);
|
|
|
|
}, [initialProjectDesc]);
|
|
|
|
|
2023-07-11 13:55:43 +02:00
|
|
|
useEffect(() => {
|
|
|
|
setFeatureLimit(initialFeatureLimit);
|
|
|
|
}, [initialFeatureLimit]);
|
|
|
|
|
2023-09-04 13:53:33 +02:00
|
|
|
useEffect(() => {
|
2023-10-04 09:42:02 +02:00
|
|
|
setProjectStickiness(initialProjectStickiness);
|
|
|
|
}, [initialProjectStickiness]);
|
2023-09-04 13:53:33 +02:00
|
|
|
|
2023-09-06 10:13:28 +02:00
|
|
|
useEffect(() => {
|
2023-10-04 09:42:02 +02:00
|
|
|
setProjectMode(initialProjectMode);
|
|
|
|
}, [initialProjectMode]);
|
2023-09-06 10:13:28 +02:00
|
|
|
|
2023-10-04 09:42:02 +02:00
|
|
|
const getCreateProjectPayload = () => {
|
|
|
|
return isEnterprise()
|
|
|
|
? {
|
|
|
|
id: projectId,
|
|
|
|
name: projectName,
|
|
|
|
description: projectDesc,
|
|
|
|
defaultStickiness: projectStickiness,
|
|
|
|
mode: projectMode,
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
id: projectId,
|
|
|
|
name: projectName,
|
|
|
|
description: projectDesc,
|
|
|
|
defaultStickiness: projectStickiness,
|
|
|
|
};
|
|
|
|
};
|
2023-03-17 13:41:59 +01:00
|
|
|
|
2023-10-04 09:42:02 +02:00
|
|
|
const getEditProjectPayload = () => {
|
2022-01-19 14:28:55 +01:00
|
|
|
return {
|
|
|
|
id: projectId,
|
|
|
|
name: projectName,
|
|
|
|
description: projectDesc,
|
2023-03-17 13:41:59 +01:00
|
|
|
defaultStickiness: projectStickiness,
|
2023-07-13 13:02:35 +02:00
|
|
|
featureLimit: getFeatureLimitAsNumber(),
|
2022-01-19 14:28:55 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-07-13 13:02:35 +02:00
|
|
|
const getFeatureLimitAsNumber = () => {
|
|
|
|
if (featureLimit === '') {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return Number(featureLimit);
|
|
|
|
};
|
|
|
|
|
2022-03-28 23:45:41 +02:00
|
|
|
const validateProjectId = async () => {
|
2022-01-25 12:30:55 +01:00
|
|
|
if (projectId.length === 0) {
|
2023-10-02 14:25:46 +02:00
|
|
|
setErrors((prev) => ({ ...prev, id: 'Id can not be empty.' }));
|
2022-01-25 12:30:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-01-19 14:28:55 +01:00
|
|
|
try {
|
2023-10-04 09:42:02 +02:00
|
|
|
await validateId(getCreateProjectPayload().id);
|
2022-01-19 14:28:55 +01:00
|
|
|
return true;
|
2022-06-07 15:03:40 +02:00
|
|
|
} catch (error: unknown) {
|
2023-10-02 14:25:46 +02:00
|
|
|
setErrors((prev) => ({ ...prev, id: formatUnknownError(error) }));
|
2022-01-19 14:28:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2022-02-18 08:26:26 +01:00
|
|
|
|
2022-01-19 14:28:55 +01:00
|
|
|
const validateName = () => {
|
|
|
|
if (projectName.length === 0) {
|
2023-10-02 14:25:46 +02:00
|
|
|
setErrors((prev) => ({ ...prev, name: 'Name can not be empty.' }));
|
2022-01-19 14:28:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-01-25 12:30:55 +01:00
|
|
|
|
2022-01-19 14:28:55 +01:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const clearErrors = () => {
|
|
|
|
setErrors({});
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
projectId,
|
|
|
|
projectName,
|
|
|
|
projectDesc,
|
2023-03-16 15:29:52 +01:00
|
|
|
projectMode,
|
2023-10-04 09:42:02 +02:00
|
|
|
projectStickiness,
|
2023-07-11 13:55:43 +02:00
|
|
|
featureLimit,
|
2022-01-19 14:28:55 +01:00
|
|
|
setProjectId,
|
|
|
|
setProjectName,
|
|
|
|
setProjectDesc,
|
2023-03-10 11:28:02 +01:00
|
|
|
setProjectStickiness,
|
2023-07-11 13:55:43 +02:00
|
|
|
setFeatureLimit,
|
2023-10-04 09:42:02 +02:00
|
|
|
setProjectMode,
|
|
|
|
getCreateProjectPayload,
|
|
|
|
getEditProjectPayload,
|
2022-01-19 14:28:55 +01:00
|
|
|
validateName,
|
2022-01-25 00:47:49 +01:00
|
|
|
validateProjectId,
|
2022-01-19 14:28:55 +01:00
|
|
|
clearErrors,
|
|
|
|
errors,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useProjectForm;
|