Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: project identifier validation #1643

Merged
merged 5 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 29 additions & 13 deletions apps/app/components/project/create-project-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const IsGuestCondition: React.FC<{
};

export const CreateProjectModal: React.FC<Props> = ({ isOpen, setIsOpen, user }) => {
const [isChangeIdentifierRequired, setIsChangeIdentifierRequired] = useState(true);
const [isChangeInIdentifierRequired, setIsChangeInIdentifierRequired] = useState(true);

const { setToastAlert } = useToast();

Expand All @@ -98,18 +98,9 @@ export const CreateProjectModal: React.FC<Props> = ({ isOpen, setIsOpen, user })
reValidateMode: "onChange",
});

const projectName = watch("name");
const projectIdentifier = watch("identifier");

useEffect(() => {
if (projectName && isChangeIdentifierRequired)
setValue("identifier", projectName.replace(/ /g, "").toUpperCase().substring(0, 3));
}, [projectName, projectIdentifier, setValue, isChangeIdentifierRequired]);

useEffect(() => () => setIsChangeIdentifierRequired(true), [isOpen]);

const handleClose = () => {
setIsOpen(false);
setIsChangeInIdentifierRequired(true);
reset(defaultValues);
};

Expand Down Expand Up @@ -147,6 +138,29 @@ export const CreateProjectModal: React.FC<Props> = ({ isOpen, setIsOpen, user })
});
};

const changeIdentifierOnNameChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (!isChangeInIdentifierRequired) return;

if (e.target.value === "") setValue("identifier", "");
else
setValue(
"identifier",
e.target.value
.replace(/[^a-zA-Z0-9]/g, "")
.toUpperCase()
.substring(0, 5)
);
};

const handleIdentifierChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { value } = e.target;

const alphanumericValue = value.replace(/[^a-zA-Z0-9]/g, "");

setValue("identifier", alphanumericValue.toUpperCase());
setIsChangeInIdentifierRequired(false);
};

const options = workspaceMembers?.map((member) => ({
value: member.member.id,
query:
Expand Down Expand Up @@ -265,6 +279,7 @@ export const CreateProjectModal: React.FC<Props> = ({ isOpen, setIsOpen, user })
name="name"
type="name"
placeholder="Project Title"
onChange={changeIdentifierOnNameChange}
error={errors.name}
register={register}
validations={{
Expand All @@ -287,11 +302,12 @@ export const CreateProjectModal: React.FC<Props> = ({ isOpen, setIsOpen, user })
placeholder="Identifier"
error={errors.identifier}
register={register}
onChange={() => setIsChangeIdentifierRequired(false)}
onChange={handleIdentifierChange}
validations={{
required: "Identifier is required",
validate: (value) =>
/^[A-Z]+$/.test(value) || "Identifier must be in uppercase.",
/^[A-Z0-9]+$/.test(value.toUpperCase()) ||
"Identifier must be in uppercase.",
minLength: {
value: 1,
message: "Identifier must at least be of 1 character",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ const GeneralSettings: NextPage = () => {
else await updateProject(payload);
};

const handleIdentifierChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const { value } = event.target;

const alphanumericValue = value.replace(/[^a-zA-Z0-9]/g, "");
const formattedValue = alphanumericValue.toUpperCase();

setValue("identifier", formattedValue);
};

const currentNetwork = NETWORK_CHOICES.find((n) => n.key === projectDetails?.network);

return (
Expand Down Expand Up @@ -303,10 +312,11 @@ const GeneralSettings: NextPage = () => {
error={errors.identifier}
register={register}
placeholder="Enter identifier"
onChange={handleIdentifierChange}
validations={{
required: "Identifier is required",
validate: (value) =>
/^[A-Z]+$/.test(value) || "Identifier must be uppercase text.",
/^[A-Z0-9]+$/.test(value.toUpperCase()) || "Identifier must be in uppercase.",
minLength: {
value: 1,
message: "Identifier must at least be of 1 character",
Expand Down