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

#6094 Prevent creating a custom field with an existing name #6100

Merged

Conversation

deval2498
Copy link
Contributor

@deval2498 deval2498 commented Jul 2, 2024

Fixes #6094
Description: Added logic inside SettingsObjectNewFieldStep2.tsx to prevent form submission
Current Behaviours:
Screenshot 2024-07-03 at 1 45 31 PM

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

  • Added logic to prevent form submission if a custom field name already exists in packages/twenty-front/src/pages/settings/data-model/SettingsObjectNewField/SettingsObjectNewFieldStep2.tsx
  • Maintains a list of existing field names for validation
  • Displays an error message when a duplicate name is detected

1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

(updates since last review)

  • Added error icon next to label input in packages/twenty-front/src/modules/settings/data-model/fields/forms/components/SettingsDataModelFieldAboutForm.tsx
  • Introduced state management for existing field names in packages/twenty-front/src/pages/settings/data-model/SettingsObjectNewField/SettingsObjectNewFieldStep2.tsx
  • Implemented validation checks to prevent duplicate field names in packages/twenty-front/src/pages/settings/data-model/SettingsObjectNewField/SettingsObjectNewFieldStep2.tsx
  • Disabled save button and displayed error message for duplicate field names in packages/twenty-front/src/pages/settings/data-model/SettingsObjectNewField/SettingsObjectNewFieldStep2.tsx

2 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings

Copy link
Collaborator

@ijreilly ijreilly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution @deval2498!
The code is functional but I added a few indications to try to follow our best practices!

useEffect(() => {
if (!activeObjectMetadataItem) {
navigate(AppPath.NotFound);
}
else {
setExistingFieldNames(activeObjectMetadataItem.fields.map(field => field.name));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of our best practices is to avoid using useEffect as much as possible as they trigger rerenders.
In our case here since the list of field names (activeObjectMetadataItem.fields.map(field => field.name)) is not supposed to change often we don't need to be reactive and to store them in a state :)


const formConfig = useForm<SettingsDataModelNewFieldFormValues>({
mode: 'onTouched',
resolver: zodResolver(settingsFieldFormSchema),
});

const [existingFieldNames, setExistingFieldNames] = useState<string[]>([]);
const [validateNameField, setValidateNameField] = useState(true)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we want to try to use react-hook-form helpers instead of implementing a custom side-validation. Here it would mean relying on react-hook-form useForm's resolver (used L61): settingsFieldFormSchema can be updated to a function accepting additional limitations through arguments. This should prevent the user from saving as formConfig.formState.isValid will be false.

@@ -202,7 +219,10 @@ export const SettingsObjectNewFieldStep2 = () => {
title="Name and description"
description="The name and description of this field"
/>
<SettingsDataModelFieldAboutForm />
<SettingsDataModelFieldAboutForm isErrorField={!validateNameField}/>
{!validateNameField && (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too, we should try to use react-hook-form to get the errors and display them at field-level

@deval2498
Copy link
Contributor Author

On it. Thanks for this review

@deval2498 deval2498 force-pushed the 6094-prevent-form-submission-if-name-used branch from a1405c3 to 7677b01 Compare July 7, 2024 18:31
@deval2498
Copy link
Contributor Author

deval2498 commented Jul 7, 2024

Updated the code as per the guidelines. Updated:

  1. FieldMetadataItemSchema to accept an array of strings, so that we can throw an error directly from zod resolver
  2. Used inbuilt error controls of Controller in SettingsDataModelFieldAboutForm
  3. Updated types to incorporate the changes made to FieldMetadataItemSchema
    Cuurent Behaviour:
Screenshot 2024-07-08 at 12 02 28 AM

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

(updates since last review)

  • Added validation logic in packages/twenty-front/src/pages/settings/data-model/SettingsObjectNewField/SettingsObjectNewFieldStep2.tsx to prevent duplicate field names.
  • Updated packages/twenty-front/src/modules/object-metadata/validation-schemas/fieldMetadataItemSchema.ts to include existingLabels parameter for label uniqueness validation.
  • Modified packages/twenty-front/src/modules/object-metadata/validation-schemas/metadataLabelSchema.ts to check for unique metadata labels.
  • Introduced maximum length constraints for field names in packages/twenty-front/src/modules/settings/data-model/fields/forms/components/SettingsDataModelFieldAboutForm.tsx.
  • Updated packages/twenty-front/src/modules/settings/data-model/fields/forms/validation-schemas/settingsFieldFormSchema.ts to accept existingLabels for validation.

83 file(s) reviewed, 2 comment(s)
Edit PR Review Bot Settings

},
)
.refine((label) => {
try{if(!existingLabels) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪶 style: Consider adding a space after try for consistency and readability.

Suggested change
try{if(!existingLabels) {
try { if (!existingLabels) {

const validateLabel = () => {
trigger('label')
}
console.log(errors.label, "errors near label")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪶 style: Remove console.log statement before merging.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

(updates since last review)

  • Enhanced metadataLabelSchema.ts to ensure label uniqueness.
  • Removed console.log and added error message for non-unique labels in SettingsDataModelFieldAboutForm.tsx.

No major changes found since last review.

2 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings

@deval2498 deval2498 requested a review from ijreilly July 8, 2024 07:42
@ijreilly
Copy link
Collaborator

ijreilly commented Jul 15, 2024

great work @deval2498 , thanks for your contribution :) i allowed myself to add some minor improvements.
I think this also fixes #5428 :) great

there are some remaining issues from the tests (probably some code to adapt), do you want to have a look or shall i take over? @deval2498

@deval2498
Copy link
Contributor Author

Will take a look and update you on this, thanks for helping here

@Bonapara
Copy link
Member

Hi @deval2498, thanks for the PR! Could you remove the red error icon on the right side of the field and add a Border Danger around the field?

CleanShot 2024-07-15 at 17 26 41

https://www.figma.com/design/xt8O9mFeLl46C5InWwoMrN/Twenty?node-id=3007-69739&t=hrDkyWQ3cs5iGjno-11

@deval2498
Copy link
Contributor Author

deval2498 commented Jul 17, 2024

@ijreilly color exists in theme.border.color.danger, unable to perform tests on my machine heap running out of memory would be great if you can help:)

Copy link
Collaborator

@ijreilly ijreilly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great work @deval2498 this will be very useful ! :)

@ijreilly ijreilly merged commit 94c2358 into twentyhq:main Jul 17, 2024
7 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prevent creating a custom field with an existing name
4 participants