Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update edit messages text on Session view compoenent #2715

Merged
merged 5 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions src/frontend/src/controllers/API/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,10 +1081,3 @@ export async function deleteMessagesFn(ids: string[]) {
throw error;
}
}

export async function updateMessageApi(data: Message) {
if (data.files && typeof data.files === "string") {
data.files = JSON.parse(data.files);
}
return await api.put(`${BASE_URL_API}monitor/messages/${data.id}`, data);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useMutationFunctionType } from "@/types/api";
import { Message } from "@/types/messages";
import { UseMutationResult } from "@tanstack/react-query";
import { api } from "../../api";
import { getURL } from "../../helpers/constants";
import { UseRequestProcessor } from "../../services/request-processor";

interface UpdateMessageParams {
message: Message;
}

export const useUpdateMessage: useMutationFunctionType<UpdateMessageParams> = (
options?,
) => {
const { mutate } = UseRequestProcessor();

const updateMessageApi = async (data: Message) => {
if (data.files && typeof data.files === "string") {
data.files = JSON.parse(data.files);
}
const result = await api.put(`${getURL("MESSAGES")}/${data.id}`, data);
return result.data;
};

const mutation: UseMutationResult<
UpdateMessageParams,
any,
UpdateMessageParams
> = mutate(["useUpdateMessages"], updateMessageApi, options);

return mutation;
};
27 changes: 21 additions & 6 deletions src/frontend/src/modals/IOModal/components/SessionView/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Loading from "@/components/ui/loading";
import { useGetMessagesQuery } from "@/controllers/API/queries/messages";
import {
useGetMessagesQuery,
useUpdateMessage,
} from "@/controllers/API/queries/messages";
import { useIsFetching } from "@tanstack/react-query";
import {
CellEditRequestEvent,
Expand All @@ -10,7 +13,6 @@ import cloneDeep from "lodash/cloneDeep";
import { useMemo, useState } from "react";
import TableComponent from "../../../../components/tableComponent";
import useRemoveMessages from "../../../../pages/SettingsPage/pages/messagesPage/hooks/use-remove-messages";
import useUpdateMessage from "../../../../pages/SettingsPage/pages/messagesPage/hooks/use-updateMessage";
import useAlertStore from "../../../../stores/alertStore";
import { useMessagesStore } from "../../../../stores/messagesStore";
import { messagesSorter } from "../../../../utils/utils";
Expand All @@ -26,6 +28,7 @@ export default function SessionView({
const messages = useMessagesStore((state) => state.messages);
const setErrorData = useAlertStore((state) => state.setErrorData);
const setSuccessData = useAlertStore((state) => state.setSuccessData);
const updateMessage = useMessagesStore((state) => state.updateMessage);

const isFetching = useIsFetching({
queryKey: ["useGetMessagesQuery"],
Expand All @@ -40,7 +43,7 @@ export default function SessionView({
selectedRows,
);

const { handleUpdate } = useUpdateMessage(setSuccessData, setErrorData);
const { mutate: updateMessageMutation } = useUpdateMessage();

function handleUpdateMessage(event: NewValueParams<any, string>) {
const newValue = event.newValue;
Expand All @@ -50,9 +53,21 @@ export default function SessionView({
...row,
[field]: newValue,
};
handleUpdate(data).catch((error) => {
event.data[field] = event.oldValue;
event.api.refreshCells();
updateMessageMutation(data, {
onSuccess: () => {
updateMessage(data);
// Set success message
setSuccessData({
title: "Messages updated successfully.",
});
},
onError: () => {
setErrorData({
title: "Error updating messages.",
});
event.data[field] = event.oldValue;
event.api.refreshCells();
},
});
}

Expand Down

This file was deleted.

Loading