Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | 20x 20x 20x 20x 20x 20x 2x 2x 20x 1x 20x 1x 1x 1x 1x 1x 1x 1x 1x 20x | import { showMessage } from "@common/Toast";
import { SnackbarFile } from "@components/UploadFile/types";
import NiceModal from "@ebay/nice-modal-react";
import { useGetCurrentMerchantId } from "@hooks/common";
import { useUploadFiles } from "@hooks/upload-api/uploadHooks";
import { customInstance } from "@services/api";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { Message } from "features/Minibuilders/Conversations/Modal/types";
import { NOTIFICATION_SIDE_PANEL } from "modals/modal_names";
import { UseFormReturn } from "react-hook-form";
import { useMutation, useQuery } from "react-query";
export type FormFieldsNotification = {
merchant_message: string;
files: any[];
selectedId?: number;
};
export default function useFetchNotificationMessages({
notificationMessageID,
methods,
}: {
notificationMessageID?: number;
methods: UseFormReturn<FormFieldsNotification, any, undefined>;
}) {
const { merchantId } = useGetCurrentMerchantId();
const { reset, getValues } = methods;
const { isNewConversationsEnabled } = useGetFeatureFlagValues();
const { handleUpload } = useUploadFiles();
const v2 = isNewConversationsEnabled ? "v2" : "";
const messageListResponse = useQuery(
["fetch-message-list", notificationMessageID, merchantId],
async () => {
const apiResponse = await customInstance({
url: `${v2}/merchants/${merchantId}/threads/${notificationMessageID}/messages?sort=-createdAt`,
method: "GET",
});
return apiResponse?.data as Message[];
},
{
enabled: !!notificationMessageID && !!merchantId,
retry: 1,
refetchOnWindowFocus: true,
onError: (error: any) => {
if (
error?.response?.data?.message ===
"The requested conversation thread does not exist." &&
isNewConversationsEnabled
) {
showMessage(
"Error",
"Unable to open. This notification is from a previous version of the app.",
undefined,
"Conversation Unavailable",
);
NiceModal.remove(NOTIFICATION_SIDE_PANEL);
} else {
showMessage(
"Error",
error?.response?.data?.message || error?.message,
);
}
},
},
);
const attachmentsPareser = (attachments: SnackbarFile[]) => {
return attachments.map((file) => ({
id: file.id,
name: file.name,
size: file.size,
URL: file.identifier,
}));
};
const updatePostMutation = useMutation(
(data: FormFieldsNotification) => {
const isEdit = !!data?.selectedId;
const url = isEdit
? `${v2}/merchants/${merchantId}/threads/${notificationMessageID}/messages/${data?.selectedId}`
: `${v2}/merchants/${merchantId}/threads/${notificationMessageID}/messages`;
const payload = {
...(!!data?.merchant_message && { body: data?.merchant_message }),
...(!isNewConversationsEnabled && {
attachments: attachmentsPareser(data?.files) || undefined,
}),
...(!data?.merchant_message && { attachmentOnly: true }),
};
return customInstance({
url,
method: isEdit ? "PATCH" : "POST",
data: payload,
});
},
{
onSuccess: async (data: any, payload: FormFieldsNotification) => {
if (isNewConversationsEnabled && payload?.files) {
await handleUpload({
list: payload?.files,
attachmentType: "conversation_message",
merchantId: merchantId,
resourceID: data?.id,
});
}
},
onSettled: () => {
const files = getValues("files");
Iif (files && isNewConversationsEnabled) {
files.map((file: any) => file.remove());
}
messageListResponse?.refetch();
reset();
},
},
);
return { messageListResponse, updatePostMutation };
}
|