Skip to content

Commit

Permalink
fix: project identifier validation (#1643)
Browse files Browse the repository at this point in the history
* fix: project identifier validation

* feat: alphanumric identifier validation

* chore: code refactor

* refactor: project identifier change function

---------

Co-authored-by: Aaryan Khandelwal <aaryankhandu123@gmail.com>
  • Loading branch information
anmolsinghbhatia and aaryan610 committed Jul 25, 2023
1 parent 9b531ac commit ad410d1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
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

0 comments on commit ad410d1

Please sign in to comment.