mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-06 00:07:44 +01:00
3193423d2d
Project scoped stickiness <!-- Thanks for creating a PR! To make it easier for reviewers and everyone else to understand what your changes relate to, please add some relevant content to the headings below. Feel free to ignore or delete sections that you don't think are relevant. Thank you! ❤️ --> Adds `projectScopedStickiness` flag to experimental.ts Refactor Stickiness select for reusability Modify FlexibleStrategy to respect the setting. Modify EnvironmentVariantModal to respect the setting ## About the changes <!-- Describe the changes introduced. What are they and why are they being introduced? Feel free to also add screenshots or steps to view the changes if they're visual. --> <!-- Does it close an issue? Multiple? --> Closes # <!-- (For internal contributors): Does it relate to an issue on public roadmap? --> <!-- Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item: # --> ### Important files <!-- PRs can contain a lot of changes, but not all changes are equally important. Where should a reviewer start looking to get an overview of the changes? Are any files particularly important? --> ## Discussion points <!-- Anything about the PR you'd like to discuss before it gets merged? Got any questions or doubts? --> --------- Signed-off-by: andreas-unleash <andreas@getunleash.ai>
90 lines
2.4 KiB
TypeScript
90 lines
2.4 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import useProjectApi from 'hooks/api/actions/useProjectApi/useProjectApi';
|
|
import { formatUnknownError } from 'utils/formatUnknownError';
|
|
import { useDefaultProjectStickiness } from 'hooks/useDefaultProjectStickiness';
|
|
|
|
const useProjectForm = (
|
|
initialProjectId = '',
|
|
initialProjectName = '',
|
|
initialProjectDesc = '',
|
|
initialProjectStickiness = 'default'
|
|
) => {
|
|
const [projectId, setProjectId] = useState(initialProjectId);
|
|
const { defaultStickiness } = useDefaultProjectStickiness(projectId);
|
|
|
|
const [projectName, setProjectName] = useState(initialProjectName);
|
|
const [projectDesc, setProjectDesc] = useState(initialProjectDesc);
|
|
const [projectStickiness, setProjectStickiness] = useState(
|
|
defaultStickiness || initialProjectStickiness
|
|
);
|
|
const [errors, setErrors] = useState({});
|
|
|
|
const { validateId } = useProjectApi();
|
|
|
|
useEffect(() => {
|
|
setProjectId(initialProjectId);
|
|
}, [initialProjectId]);
|
|
|
|
useEffect(() => {
|
|
setProjectName(initialProjectName);
|
|
}, [initialProjectName]);
|
|
|
|
useEffect(() => {
|
|
setProjectDesc(initialProjectDesc);
|
|
}, [initialProjectDesc]);
|
|
|
|
const getProjectPayload = () => {
|
|
return {
|
|
id: projectId,
|
|
name: projectName,
|
|
description: projectDesc,
|
|
projectStickiness,
|
|
};
|
|
};
|
|
|
|
const validateProjectId = async () => {
|
|
if (projectId.length === 0) {
|
|
setErrors(prev => ({ ...prev, id: 'Id can not be empty.' }));
|
|
return false;
|
|
}
|
|
try {
|
|
await validateId(getProjectPayload());
|
|
return true;
|
|
} catch (error: unknown) {
|
|
setErrors(prev => ({ ...prev, id: formatUnknownError(error) }));
|
|
return false;
|
|
}
|
|
};
|
|
|
|
const validateName = () => {
|
|
if (projectName.length === 0) {
|
|
setErrors(prev => ({ ...prev, name: 'Name can not be empty.' }));
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
};
|
|
|
|
const clearErrors = () => {
|
|
setErrors({});
|
|
};
|
|
|
|
return {
|
|
projectId,
|
|
projectName,
|
|
projectDesc,
|
|
projectStickiness,
|
|
setProjectId,
|
|
setProjectName,
|
|
setProjectDesc,
|
|
setProjectStickiness,
|
|
getProjectPayload,
|
|
validateName,
|
|
validateProjectId,
|
|
clearErrors,
|
|
errors,
|
|
};
|
|
};
|
|
|
|
export default useProjectForm;
|