Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.1 kB
3
Indexable
Never
const submitJobResponsabilities: SubmitHandler<
      OnboardingJobDescriptionJobResponsabilitiesMetadata
    > = async (data, e) => {
      e?.preventDefault();
      setIsEditingJobResponsabilities({ showEdit: false });
      let dataToBeAdded = {
        data: {
          type: "cms-data",
          attributes: {
            onBoardingType: "onboarding-jobDescription-jobResponsabilities",
            id: existingJobDescriptionJobResponsabilities?.id,
            metadata: {
              jobResponsabilities: {
                title: dataValueJobResponsabilities.jobResponsabilities?.title,
              },

              imageIdNames: dataValueJobResponsabilities.imageIdNames,
            },
            createdAt: new Date(),
            updatedAt: new Date(),
          },
        },
      };
      dataToBeAdded = getNonEmptyNestedValues(dataToBeAdded);
      dataToBeAdded = {
        ...dataToBeAdded,
        data: {
          ...dataToBeAdded.data,
          attributes: {
            ...dataToBeAdded.data.attributes,
            createdAt: new Date(),
            updatedAt: new Date(),
          },
        },
      };

      let jobDescriptionMultipartPayload: any = undefined;

      const jobDescriptionFile = toJS(dataValueJobResponsabilities);

      jobDescriptionMultipartPayload = new FormData();

      Object.entries(jobDescriptionFile).forEach(([key, value]) => {
        jobDescriptionMultipartPayload.append(key, value);
      });

      const strinifiedPayload = JSON.stringify(dataToBeAdded);
      const blobPayload = new Blob([strinifiedPayload], {
        type: "application/vnd.api+json",
      });

      jobDescriptionMultipartPayload.append("onboardingPayload", blobPayload);

      try {
        if (!existingJobDescriptionJobResponsabilities) {
          const response: any = await postOnBoardingWithDocuments({
            data: jobDescriptionMultipartPayload
              ? jobDescriptionMultipartPayload
              : dataToBeAdded,
            headers: {
              Authorization: `Bearer ${token}`,
            },
          });

          if (response?.data) {
            customToast.success({ message: "Job description page created successfully" });
            onBoardingStore?.addOnBoarding(response?.data);
          }
        } else {
          const response = await patchOnBoardingWithDocuments({
            id: existingJobDescriptionJobResponsabilities.id,
            data: jobDescriptionMultipartPayload
              ? jobDescriptionMultipartPayload
              : dataToBeAdded,
            headers: {
              Authorization: `Bearer ${token}`,
            },
          });

          if (response?.status === 200) {
            const res = response.json();
            let body: any = undefined;
            await res.then((a) => {
              body = a.data;
            });
            customToast.success({ message: "Job description page updated successfully" });
            onBoardingStore?.editOnBoarding(body);
          }
        }
      } catch (error: any) {
        customToast.error({ message: error.message });
      }
    };