epam / UUI

React-based components and accelerators library built by EPAM Systems.
https://uui.epam.com/
MIT License
179 stars 65 forks source link

[Form]: Redirect after successful save triggers beforeLeave #2191

Closed RomainGoncalves closed 4 months ago

RomainGoncalves commented 5 months ago

Prerequisites (optional)

This section may be deleted

Please answer the following questions for yourself before submitting an issue.

Description (optional)

When using useForm, the onSave method runs followed by the onSuccess. In the onSuccess, I have a router redirection. When the redirection is triggered I see the confirmation modal from beforeLeave.

Steps to Reproduce

const {
    lens,
    save,
    value: advertisementFormState,
    onValueChange,
    isInProgress,
    isInvalid,
  } = useForm<AdvertisementFormState>({
    value: defaultFormData,
    onSave: (ad: Advertisement) => {
      const payload = produce(ad, (draft) => {
        draft.description = descriptionFormatter(draft.description);
        draft.highlight = undefined;
      });

      return new Promise((resolve, reject) => {
        return axios
          .post<Advertisement>(AdvertisementServiceUrl.advertisementList, payload)
          .then(async ({ data: createdAdvertisement }) => {
            const id = createdAdvertisement.id;
            await handlePendingUploadDelete(id)
              .then(({ remainingPhotos, newHighlight }) => {
                const payload = produce(createdAdvertisement, (draft) => {
                  draft.description = descriptionFormatter(draft.description);
                  draft.attachments = remainingPhotos.map((a) => a.id);
                  draft.highlight = newHighlight;
                  draft.owner = owner;
                });

                return axios
                  .put<Advertisement>(AdvertisementServiceUrl.advertisementDetails(id), payload)
                  .then(({ data }) => resolve({ form: data }))
                  .then(() => SuccessAlert(SuccessMessage.AdvertisementCreate, svc));
              })
              .catch(() => {
                ErrorAlert(ErrorMessage.AttachmentSaveOnCreate, svc);

                // If there has been an error saving the images, we delete the advertisement silently
                axios.delete(AdvertisementServiceUrl.advertisementDetails(id));
              });
          })
          .catch(reject);
      });
    },
    onSuccess: () => {
      // Here the modal that confirms if I want to leave the page is triggered
      // If I remove the redirection, I don't see the confirmation modal
      svc.uuiRouter.redirect({ pathname: '/my-advertisements' });
    },
  });

Actual result

I see the beforeLeave confirmation modal

Expected result

As the save was successful, I don't want to be prompted by the confirmation modal

Environment

RomainGoncalves commented 5 months ago

Could I be told if that behavior is expected? If so, for now, I will turn it off with beforeLeave={null}.

AlekseyManetov commented 5 months ago

Could I be told if that behavior is expected? If so, for now, I will turn it off with beforeLeave={null}.

@RomainGoncalves Thanks for the issue report. That's a bug. We'll try to fix it in a next release.

NatalliaAlieva commented 4 months ago

Released in 5.8.0 ver.