Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
3.7 kB
2
Indexable
Never
http
  .get('/prizes/upload?name=file.png', {
    headers: {
      Authorization: dashboardAuth,
      'Content-Type': 'application/json',
    },
  })
  .then(({ data }) => {
    let formData = new FormData();
    const downloadLink = data.fileLink;
    formData.append('file', selectedPicture);
    Object.keys(data.formData).forEach((key) =>
      formData.append(key, data.formData[key]),
    );
    http
      .post(data.postURL, formData, {
        headers: {
          Authorization: dashboardAuth,
          'Content-Type': 'multipart/form-data',
        },
      })
      .then(({ data }) => {
        http
          .post(
            `/prizes/`,
            {
              photo: downloadLink,
              name: productName,
              pointsNeeded: parseInt(pointNeeded),
              gemsNeeded: parseFloat(gemNeeded),
              type: typeSelect.value,
              ...(quantity
                ? {
                    hasLimited: true,
                    count: quantity,
                  }
                : {
                    hasLimited: false,
                    count: quantity,
                  }),
            },
            {
              headers: {
                Authorization: dashboardAuth,
                'Content-Type': 'application/json',
              },
            },
          )
          .then(({ data }) => {
            toast.update(id, {
              render: 'add gift successfully!',
              type: 'success',
              ...toastVariables,
            });
            setTimeout(() => {
              setDisabled(false);
              giftListData.reverse().push(data.prize);
              setGiftListDataHandler(giftListData.reverse());
              closeHandler();
            }, 2000);
          })
          .catch((error) => {
            if (!error.response) {
              toast.update(id, {
                render: 'please check network connection ...',
                type: 'error',
                ...toastVariables,
              });
            } else if (
              error.response.data.message ===
              'Prize with this name already exists!'
            ) {
              toast.update(id, {
                render: 'prize already exsits!',
                type: 'error',
                ...toastVariables,
              });
            } else {
              toast.update(id, {
                render: 'an unexpected error occurred...',
                type: 'error',
                ...toastVariables,
              });
            }
            setTimeout(() => {
              setDisabled(false);
            }, 2000);
          });
      })
      .catch((error) => {
        if (!error.response) {
          toast.update(id, {
            render: 'please check network connection ...',
            type: 'error',
            ...toastVariables,
          });
        } else {
          toast.update(id, {
            render: 'an unexpected error occurred...',
            type: 'error',
            ...toastVariables,
          });
        }
        setTimeout(() => {
          setDisabled(false);
        }, 2000);
      });
  })
  .catch((error) => {
    if (!error.response) {
      toast.update(id, {
        render: 'please check network connection ...',
        type: 'error',
        ...toastVariables,
      });
    } else {
      toast.update(id, {
        render: 'an unexpected error occurred...',
        type: 'error',
        ...toastVariables,
      });
    }
    setTimeout(() => {
      setDisabled(false);
    }, 2000);
  });