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 2 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
23 changes: 20 additions & 3 deletions apps/app/components/project/create-project-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ export const CreateProjectModal: React.FC<Props> = ({ isOpen, setIsOpen, user })

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

useEffect(() => () => setIsChangeIdentifierRequired(true), [isOpen]);
Expand Down Expand Up @@ -148,6 +154,16 @@ export const CreateProjectModal: React.FC<Props> = ({ isOpen, setIsOpen, user })
});
};

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

const alphanumericValue = value.replace(/[^a-zA-Z0-9]/g, "");
const FormattedValue = alphanumericValue.toUpperCase();
aaryan610 marked this conversation as resolved.
Show resolved Hide resolved

setValue("identifier", FormattedValue);
setIsChangeIdentifierRequired(false);
};

const options = workspaceMembers?.map((member) => ({
value: member.member.id,
query:
Expand Down Expand Up @@ -288,11 +304,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