Skip to content

Commit b138055

Browse files
Merge pull request #103 from Chia-Network/prefix-validation
feat: add validation for org prefix
2 parents e6f1077 + 5467fa5 commit b138055

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

src/renderer/components/blocks/forms/CreateOrganizationForm.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ const CreateOrganizationForm: React.FC<FormProps> = ({ onSubmit }) => {
1818

1919
const validationSchema = yup.object({
2020
name: yup.string().required(intl.formatMessage({ id: 'name-is-required' })),
21-
prefix: yup.string().required(intl.formatMessage({ id: 'prefix-is-required' })),
21+
prefix: yup
22+
.string()
23+
.optional()
24+
.matches(/^[a-zA-Z0-9]+$/, 'Prefix must contain only alphanumeric characters (a-z, A-Z, 0-9)')
25+
.max(20, 'Prefix must be 20 characters or less'),
2226
});
2327

2428
const handleSubmit = useCallback(
@@ -58,9 +62,10 @@ const CreateOrganizationForm: React.FC<FormProps> = ({ onSubmit }) => {
5862
<FloatingLabel
5963
id="prefix"
6064
label={intl.formatMessage({ id: 'organization-prefix' })}
61-
color={(errors.name && touched.name && 'error') || undefined}
65+
color={(errors.prefix && touched.prefix && 'error') || undefined}
6266
variant="outlined"
6367
type="text"
68+
maxLength={20}
6469
{...field}
6570
/>
6671
)}

src/renderer/components/blocks/forms/EditOrganizationForm.tsx

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@ import { Organization } from '@/schemas/Organization.schema';
77

88
const validationSchema = yup.object({
99
organizationName: yup.string().required('The organization name must be at least 3 characters').min(3),
10+
prefix: yup
11+
.string()
12+
.optional()
13+
.matches(/^[a-zA-Z0-9]+$/, 'Prefix must contain only alphanumeric characters (a-z, A-Z, 0-9)')
14+
.max(20, 'Prefix must be 20 characters or less'),
1015
});
1116

1217
interface FormProps {
1318
myOrganization: Organization;
14-
onSubmit: (organizationName: string) => Promise<any>;
19+
onSubmit: (values: { organizationName: string; prefix: string }) => Promise<any>;
1520
onCancel: () => void;
1621
}
1722

1823
const EditOrganizationForm: React.FC<FormProps> = ({ myOrganization, onSubmit, onCancel }: FormProps) => {
1924
const handleSubmit = useCallback(
20-
async (values: { organizationName: string }, { setSubmitting }) => {
21-
await onSubmit(values.organizationName);
25+
async (values: { organizationName: string; prefix: string }, { setSubmitting }) => {
26+
await onSubmit(values);
2227
setSubmitting(false);
2328
},
2429
[onSubmit],
@@ -30,7 +35,7 @@ const EditOrganizationForm: React.FC<FormProps> = ({ myOrganization, onSubmit, o
3035

3136
return (
3237
<Formik
33-
initialValues={{ organizationName: myOrganization.name }}
38+
initialValues={{ organizationName: myOrganization.name, prefix: myOrganization.prefix }}
3439
validationSchema={validationSchema}
3540
onSubmit={handleSubmit}
3641
>
@@ -61,6 +66,29 @@ const EditOrganizationForm: React.FC<FormProps> = ({ myOrganization, onSubmit, o
6166
{touched.organizationName && (
6267
<ErrorMessage name="organizationName" component="div" className="text-red-600" />
6368
)}
69+
<Field name="prefix">
70+
{({ field }) => (
71+
<div className="flex justify-start align-middle">
72+
<div>
73+
<p className="font-bold text-left text-gray-700 dark:text-gray-400 mr-4">
74+
<FormattedMessage id="organization-prefix" />
75+
</p>
76+
</div>
77+
<TextInput
78+
className="w-3/5 mb-2"
79+
id="prefix"
80+
color={errors.prefix && touched.prefix && 'failure'}
81+
variant="outlined"
82+
required
83+
type="text"
84+
maxLength={20}
85+
{...field}
86+
onChange={(event) => handleChange(event, field)}
87+
/>
88+
</div>
89+
)}
90+
</Field>
91+
{touched.prefix && <ErrorMessage name="prefix" component="div" className="text-red-600" />}
6492
<div className="flex justify-start">
6593
<p className="font-bold text-left text-gray-700 dark:text-gray-400 mr-4">
6694
<FormattedMessage id="orguid" />

src/renderer/pages/MyOrganizationPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ const MyOrganizationPage: React.FC<MyOrganizationProps> = () => {
2727
}
2828
}, [myOrganization, navigate, organizationsListLoading]);
2929

30-
const handleSubmitEditOrganization = async (orgName: string) => {
30+
const handleSubmitEditOrganization = async (values: { organizationName: string; prefix: string }) => {
3131
if (myOrganization) {
32-
await triggerEditOrganization({ orgName, orgUid: myOrganization?.orgUid });
32+
await triggerEditOrganization({ orgName: values.organizationName, orgUid: myOrganization?.orgUid });
3333
}
3434
};
3535

0 commit comments

Comments
 (0)