Skip to content

Commit

Permalink
Disable save button while submitting form in settings (twentyhq#5352)
Browse files Browse the repository at this point in the history
as per title
  • Loading branch information
ijreilly authored May 10, 2024
1 parent ae0e31a commit 72521d5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ export const SettingsNewObject = () => {
resolver: zodResolver(newObjectFormSchema),
});

const canSave = formConfig.formState.isValid;

const handleSave = async () => {
const formValues = formConfig.getValues();
const canSave =
formConfig.formState.isValid && !formConfig.formState.isSubmitting;

const handleSave = async (
formValues: SettingsDataModelNewObjectFormValues,
) => {
try {
const { data: response } = await createOneObjectMetadataItem(
settingsCreateObjectInputSchema.parse(formValues),
Expand Down Expand Up @@ -81,7 +82,7 @@ export const SettingsNewObject = () => {
<SaveAndCancelButtons
isSaveDisabled={!canSave}
onCancel={() => navigate(settingsObjectsPagePath)}
onSave={handleSave}
onSave={formConfig.handleSubmit(handleSave)}
/>
</SettingsHeaderContainer>
<Section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ export const SettingsObjectEdit = () => {

if (!activeObjectMetadataItem) return null;

const { isDirty, isValid } = formConfig.formState;
const canSave = isDirty && isValid;
const { isDirty, isValid, isSubmitting } = formConfig.formState;
const canSave = isDirty && isValid && !isSubmitting;

const handleSave = async () => {
const formValues = formConfig.getValues();
const handleSave = async (
formValues: SettingsDataModelObjectEditFormValues,
) => {
const dirtyFieldKeys = Object.keys(
formConfig.formState.dirtyFields,
) as (keyof SettingsDataModelObjectEditFormValues)[];
Expand Down Expand Up @@ -121,7 +122,7 @@ export const SettingsObjectEdit = () => {
onCancel={() =>
navigate(`${settingsObjectsPagePath}/${objectSlug}`)
}
onSave={handleSave}
onSave={formConfig.handleSubmit(handleSave)}
/>
)}
</SettingsHeaderContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,19 @@ export const SettingsObjectFieldEdit = () => {

if (!activeObjectMetadataItem || !activeMetadataField) return null;

const canSave = formConfig.formState.isValid && formConfig.formState.isDirty;
const canSave =
formConfig.formState.isValid &&
formConfig.formState.isDirty &&
!formConfig.formState.isSubmitting;

const isLabelIdentifier = isLabelIdentifierField({
fieldMetadataItem: activeMetadataField,
objectMetadataItem: activeObjectMetadataItem,
});

const handleSave = async () => {
const formValues = formConfig.getValues();
const handleSave = async (
formValues: SettingsDataModelFieldEditFormValues,
) => {
const { dirtyFields } = formConfig.formState;

try {
Expand Down Expand Up @@ -166,7 +170,7 @@ export const SettingsObjectFieldEdit = () => {
<SaveAndCancelButtons
isSaveDisabled={!canSave}
onCancel={() => navigate(`/settings/objects/${objectSlug}`)}
onSave={handleSave}
onSave={formConfig.handleSubmit(handleSave)}
/>
)}
</SettingsHeaderContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,12 @@ export const SettingsObjectNewFieldStep2 = () => {

if (!activeObjectMetadataItem) return null;

const canSave = formConfig.formState.isValid;

const handleSave = async () => {
const formValues = formConfig.getValues();
const canSave =
formConfig.formState.isValid && !formConfig.formState.isSubmitting;

const handleSave = async (
formValues: SettingsDataModelNewFieldFormValues,
) => {
try {
if (
formValues.type === FieldMetadataType.Relation &&
Expand Down Expand Up @@ -294,7 +295,7 @@ export const SettingsObjectNewFieldStep2 = () => {
<SaveAndCancelButtons
isSaveDisabled={!canSave}
onCancel={() => navigate(`/settings/objects/${objectSlug}`)}
onSave={handleSave}
onSave={formConfig.handleSubmit(handleSave)}
/>
)}
</SettingsHeaderContainer>
Expand Down

0 comments on commit 72521d5

Please sign in to comment.