1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-06 00:07:44 +01:00
unleash.unleash/frontend/src/component/project/Project/hooks/useProjectForm.ts

158 lines
4.5 KiB
TypeScript
Raw Normal View History

import { useEffect, useState } from 'react';
2022-03-28 10:49:59 +02:00
import useProjectApi from 'hooks/api/actions/useProjectApi/useProjectApi';
import { formatUnknownError } from 'utils/formatUnknownError';
export type ProjectMode = 'open' | 'protected' | 'private';
export const DEFAULT_PROJECT_STICKINESS = 'default';
const useProjectForm = (
initialProjectId = '',
initialProjectName = '',
initialProjectDesc = '',
initialProjectStickiness = DEFAULT_PROJECT_STICKINESS,
2023-07-11 13:55:43 +02:00
initialProjectMode: ProjectMode = 'open',
initialFeatureLimit = '',
initialFeatureNamingPattern = '',
initialFeatureNamingExample = '',
initialFeatureNamingDescription = '',
) => {
const [projectId, setProjectId] = useState(initialProjectId);
const [projectName, setProjectName] = useState(initialProjectName);
const [projectDesc, setProjectDesc] = useState(initialProjectDesc);
const [projectStickiness, setProjectStickiness] = useState<string>(
initialProjectStickiness,
);
const [projectMode, setProjectMode] =
useState<ProjectMode>(initialProjectMode);
2023-07-11 13:55:43 +02:00
const [featureLimit, setFeatureLimit] =
useState<string>(initialFeatureLimit);
const [featureNamingPattern, setFeatureNamingPattern] = useState(
initialFeatureNamingPattern,
);
const [featureNamingExample, setFeatureNamingExample] = useState(
initialFeatureNamingExample,
);
const [featureNamingDescription, setFeatureNamingDescription] = useState(
initialFeatureNamingDescription,
);
const [errors, setErrors] = useState({});
const { validateId } = useProjectApi();
useEffect(() => {
setProjectId(initialProjectId);
}, [initialProjectId]);
useEffect(() => {
setProjectName(initialProjectName);
}, [initialProjectName]);
useEffect(() => {
setProjectDesc(initialProjectDesc);
}, [initialProjectDesc]);
2023-03-16 15:29:52 +01:00
useEffect(() => {
setProjectMode(initialProjectMode);
}, [initialProjectMode]);
2023-07-11 13:55:43 +02:00
useEffect(() => {
setFeatureLimit(initialFeatureLimit);
}, [initialFeatureLimit]);
useEffect(() => {
setFeatureNamingPattern(initialFeatureNamingPattern);
}, [initialFeatureNamingPattern]);
useEffect(() => {
setFeatureNamingExample(initialFeatureNamingExample);
}, [initialFeatureNamingExample]);
useEffect(() => {
setFeatureNamingDescription(initialFeatureNamingDescription);
}, [initialFeatureNamingDescription]);
useEffect(() => {
setProjectStickiness(initialProjectStickiness);
}, [initialProjectStickiness]);
const getProjectPayload = () => {
return {
id: projectId,
name: projectName,
description: projectDesc,
defaultStickiness: projectStickiness,
featureLimit: getFeatureLimitAsNumber(),
2023-03-16 15:29:52 +01:00
mode: projectMode,
featureNaming: {
pattern: featureNamingPattern,
example: featureNamingExample,
description: featureNamingDescription,
},
};
};
const getFeatureLimitAsNumber = () => {
if (featureLimit === '') {
return undefined;
}
return Number(featureLimit);
};
const validateProjectId = async () => {
2022-01-25 12:30:55 +01:00
if (projectId.length === 0) {
setErrors((prev) => ({ ...prev, id: 'Id can not be empty.' }));
2022-01-25 12:30:55 +01:00
return false;
}
try {
await validateId(getProjectPayload().id);
return true;
} catch (error: unknown) {
setErrors((prev) => ({ ...prev, id: formatUnknownError(error) }));
return false;
}
};
2022-02-18 08:26:26 +01:00
const validateName = () => {
if (projectName.length === 0) {
setErrors((prev) => ({ ...prev, name: 'Name can not be empty.' }));
return false;
}
2022-01-25 12:30:55 +01:00
return true;
};
const clearErrors = () => {
setErrors({});
};
return {
projectId,
projectName,
projectDesc,
projectStickiness,
2023-03-16 15:29:52 +01:00
projectMode,
2023-07-11 13:55:43 +02:00
featureLimit,
featureNamingPattern,
featureNamingExample,
featureNamingDescription,
setFeatureNamingPattern,
setFeatureNamingExample,
setFeatureNamingDescription,
setProjectId,
setProjectName,
setProjectDesc,
setProjectStickiness,
2023-03-16 15:29:52 +01:00
setProjectMode,
2023-07-11 13:55:43 +02:00
setFeatureLimit,
getProjectPayload,
validateName,
validateProjectId,
clearErrors,
errors,
};
};
export default useProjectForm;