Skip to content

Commit

Permalink
fixed namespace, name, and tag input checks not working in modals
Browse files Browse the repository at this point in the history
  • Loading branch information
sanghoonio committed Sep 4, 2024
1 parent fcadcc4 commit 36573cb
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 18 deletions.
24 changes: 20 additions & 4 deletions web/src/components/forms/blank-project-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const CombinedErrorMessage = (props: CombinedErrorMessageProps) => {
}

if (nameError || tagError) {
return <p className="text-danger text-xs pt-1">{msg}</p>;
return <p className="text-danger text-xs pt-1 mb-0">{msg}</p>;
}

return null;
Expand Down Expand Up @@ -147,9 +147,15 @@ sample_table: samples.csv
// dont allow any whitespace
{...register('project_name', {
required: true,
required: {
value: true,
message: "empty",
},
pattern: {
value: /^\S+$/,
message: 'No spaces allowed.',
value: /^[a-zA-Z0-9_-]+$/,
message: "invalid",
},
})}
id="blank-project-name"
Expand All @@ -159,10 +165,20 @@ sample_table: samples.csv
/>
<span className="mx-1 mb-1">:</span>
</div>
<input {...register('tag')} id="blank_tag" type="text" className="form-control" placeholder="default" />
<input {...register('tag', {
required: false,
pattern: {
value: /^[a-zA-Z0-9_-]+$/,
message: "invalid",
},
})}
id="blank_tag"
type="text"
className="form-control"
placeholder="default"
/>
</div>

<ErrorMessage errors={errors} name="project_name" render={({ message }) => <p>{message}</p>} />
<CombinedErrorMessage errors={errors}/>
<label className="fw-semibold text-sm mt-2">Description</label>
<textarea
id="blank_description"
Expand Down
43 changes: 40 additions & 3 deletions web/src/components/forms/create-schema-form.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Editor } from '@monaco-editor/react';
import { Controller, useForm } from 'react-hook-form';
import { ErrorMessage } from '@hookform/error-message';
import { Controller, FieldErrors, useForm } from 'react-hook-form';

import { useSession } from '../../contexts/session-context';
import { useCreateSchemaMutation } from '../../hooks/mutations/useCreateSchemaMutation';
Expand Down Expand Up @@ -36,10 +37,39 @@ type FormFields = {
schemaYaml: string;
};

type CombinedErrorMessageProps = {
errors: FieldErrors<POPInputs>;
};

const CombinedErrorMessage = (props: CombinedErrorMessageProps) => {
const { errors } = props;
const nameError = errors.name?.message;
let msg = null;

if (nameError == 'empty') {
msg = 'Project Name must not be empty.';
} else if (nameError == 'invalid') {
msg = "Project Name must contain only alphanumeric characters, '-', or '_'.";
}

if (nameError) {
return <p className="text-danger text-xs pt-1 mb-0">{msg}</p>;
}

return null;
};

export const CreateSchemaForm = (props: Props) => {
const { onCancel, onSubmit, editorHeight, defaultNamespace } = props;
const { user } = useSession();
const { formState, watch, register, control, reset } = useForm<FormFields>({
const {
watch,
register,
control,
reset,
formState: { isValid, isDirty, errors },
} = useForm<FormFields>({
mode: 'onChange',
defaultValues: {
namespace: defaultNamespace || user?.login || undefined,
schemaYaml: defaultSchemaYaml,
Expand Down Expand Up @@ -94,9 +124,15 @@ export const CreateSchemaForm = (props: Props) => {
// dont allow any whitespace
{...register('name', {
required: true,
required: {
value: true,
message: "empty",
},
pattern: {
value: /^\S+$/,
message: 'No spaces allowed.',
value: /^[a-zA-Z0-9_-]+$/,
message: "invalid",
},
})}
id="schema-name"
Expand All @@ -106,6 +142,7 @@ export const CreateSchemaForm = (props: Props) => {
/>
</div>
</div>
<CombinedErrorMessage errors={errors} />
<label className="fw-semibold text-sm mt-2">Description</label>
<textarea
{...register('description')}
Expand Down Expand Up @@ -137,7 +174,7 @@ export const CreateSchemaForm = (props: Props) => {
</p>
<div className="mt-3">
<button
disabled={isSubmitting || !formState.isDirty}
disabled={isSubmitting || !isDirty || !isValid}
type="button"
className="btn btn-success float-end"
onClick={() => {
Expand Down
24 changes: 21 additions & 3 deletions web/src/components/forms/pop-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const CombinedErrorMessage = (props: CombinedErrorMessageProps) => {
}

if (nameError || tagError) {
return <p className="text-danger text-xs pt-1">{msg}</p>;
return <p className="text-danger text-xs pt-1 mb-0">{msg}</p>;
}

return null;
Expand Down Expand Up @@ -123,9 +123,15 @@ export const PopForm: FC<Props> = ({ onHide, defaultNamespace }) => {
// dont allow any whitespace
{...register('project_name', {
required: true,
required: {
value: true,
message: "empty",
},
pattern: {
value: /^\S+$/,
message: 'No spaces allowed.',
value: /^[a-zA-Z0-9_-]+$/,
message: "invalid",
},
})}
id="blank-project-name"
Expand All @@ -136,10 +142,22 @@ export const PopForm: FC<Props> = ({ onHide, defaultNamespace }) => {
<span className="mx-1 mb-1">:</span>
</div>
<div className="d-flex flex-row align-items-center justify-content-between w-full ">
<input {...register('tag')} id="blank_tag" type="text" className="form-control" placeholder="default" />
<input
{...register('tag', {
required: false,
pattern: {
value: /^[a-zA-Z0-9_-]+$/,
message: "invalid",
},
})}
id="blank_tag"
type="text"
className="form-control"
placeholder="default"
/>
</div>
</div>
<ErrorMessage errors={errors} name="project_name" render={({ message }) => <p>{message}</p>} />
<CombinedErrorMessage errors={errors} />
<label className="fw-semibold text-sm mt-2">Description</label>
<textarea
id="blank_description"
Expand Down
26 changes: 22 additions & 4 deletions web/src/components/forms/project-upload-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const CombinedErrorMessage = (props: CombinedErrorMessageProps) => {
}

if (nameError || tagError) {
return <p className="text-danger text-xs pt-1">{msg}</p>;
return <p className="text-danger text-xs pt-1 mb-0">{msg}</p>;
}

return null;
Expand Down Expand Up @@ -133,19 +133,37 @@ export const ProjectUploadForm = ({ onHide, defaultNamespace }: Props) => {
// dont allow any whitespace
{...register('name', {
required: true,
required: {
value: true,
message: "empty",
},
pattern: {
value: /^\S+$/,
message: 'No spaces allowed.',
value: /^[a-zA-Z0-9_-]+$/,
message: "invalid",
},
})}
/>
<span className="mx-1 mb-1">:</span>
</div>
<div className="d-flex flex-row align-items-center justify-content-between w-full ">
<input id="tag" type="text" className="form-control" placeholder="default" {...register('tag')} />
<input
{...register('tag', {
required: false,
pattern: {
value: /^[a-zA-Z0-9_-]+$/,
message: "invalid",
},
})}
id="tag"
type="text"
className="form-control"
placeholder="default"
/>
</div>
</div>
<ErrorMessage errors={errors} name="name" render={({ message }) => <p>{message}</p>} />
<CombinedErrorMessage errors={errors} />
<label className="fw-semibold text-sm mt-2">Description</label>
<textarea
id="description"
Expand Down Expand Up @@ -223,7 +241,7 @@ export const ProjectUploadForm = ({ onHide, defaultNamespace }: Props) => {
},
);
}}
disabled={isUploading}
disabled={!isValid || isUploading}
type="button"
id="new-project-submit-btn"
className="btn btn-success float-end"
Expand Down
34 changes: 31 additions & 3 deletions web/src/components/forms/upload-schema-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ type Props = {
defaultNamespace?: string;
};

type CombinedErrorMessageProps = {
errors: FieldErrors<POPInputs>;
};

const CombinedErrorMessage = (props: CombinedErrorMessageProps) => {
const { errors } = props;
const nameError = errors.name?.message;
let msg = null;

if (nameError == 'empty') {
msg = 'Project Name must not be empty.';
} else if (nameError == 'invalid') {
msg = "Project Name must contain only alphanumeric characters, '-', or '_'.";
}

if (nameError) {
return <p className="text-danger text-xs pt-1 mb-0">{msg}</p>;
}

return null;
};

export const SchemaUploadForm = (props: Props) => {
const { defaultNamespace, onCancel, onSubmit } = props;

Expand All @@ -34,7 +56,7 @@ export const SchemaUploadForm = (props: Props) => {
register,
control,
watch,
formState: { errors },
formState: { isValid, errors },
} = useForm<FromFileInputs>({
mode: 'onChange',
defaultValues: {
Expand Down Expand Up @@ -99,15 +121,21 @@ export const SchemaUploadForm = (props: Props) => {
// dont allow any whitespace
{...register('name', {
required: true,
required: {
value: true,
message: "empty",
},
pattern: {
value: /^\S+$/,
message: 'No spaces allowed.',
value: /^[a-zA-Z0-9_-]+$/,
message: "invalid",
},
})}
/>
</div>
</div>
<ErrorMessage errors={errors} name="name" render={({ message }) => <p>{message}</p>} />
<CombinedErrorMessage errors={errors} />
<label className="fw-semibold text-sm mt-2">Description</label>
<textarea
id="description"
Expand Down Expand Up @@ -159,7 +187,7 @@ export const SchemaUploadForm = (props: Props) => {
},
);
}}
disabled={isUploading}
disabled={!isValid || isUploading}
type="button"
id="new-project-submit-btn"
className="btn btn-success float-end"
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/modals/fork-pep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const CombinedErrorMessage = (props: CombinedErrorMessageProps) => {
}

if (nameError || tagError) {
return <p className="text-danger text-xs pt-1">{msg}</p>;
return <p className="text-danger text-xs pt-1 mb-0">{msg}</p>;
}

return null;
Expand Down

0 comments on commit 36573cb

Please sign in to comment.