feat: lots updates

This commit is contained in:
zerosoul
2022-02-24 14:50:26 +08:00
parent b1ba2aa523
commit 0394c99292
46 changed files with 1101 additions and 397 deletions
+44 -2
View File
@@ -1,8 +1,13 @@
import { createApi } from "@reduxjs/toolkit/query/react";
import baseQuery from "./base.query";
import { REHYDRATE } from "redux-persist";
import toast from "react-hot-toast";
import baseQuery from "./base.query";
import { ContentTypes } from "../config";
import { addChannelMsg } from "../slices/message.channel";
import {
addPendingMessage,
removePendingMessage,
} from "../slices/message.pending";
export const channelApi = createApi({
reducerPath: "channel",
baseQuery,
@@ -23,6 +28,12 @@ export const channelApi = createApi({
body: data,
}),
}),
removeChannel: builder.query({
query: (id) => ({
url: `group/${id}`,
method: "DELETE",
}),
}),
sendChannelMsg: builder.mutation({
query: ({ id, content, type = "text" }) => ({
headers: {
@@ -32,11 +43,42 @@ export const channelApi = createApi({
method: "POST",
body: content,
}),
async onQueryStarted(
{ id, content, type, from_uid },
{ dispatch, queryFulfilled }
) {
// id: who send to ,from_uid: who sent
const mid = new Date().getTime();
const tmpMsg = {
id,
content,
content_type: ContentTypes[type],
created_at: new Date().getTime(),
mid,
from_uid,
unread: false,
};
dispatch(addPendingMessage({ type: "channel", msg: tmpMsg }));
try {
const {
data: { gid, ...rest },
} = await queryFulfilled;
// 此处的id,是指给谁发的
dispatch(addChannelMsg({ id: gid, ...rest, unread: false }));
dispatch(removePendingMessage({ id, mid, type: "channel" }));
} catch {
console.log("channel message send failed");
toast.error("Send Message Failed");
dispatch(removePendingMessage({ id, mid, type: "channel" }));
// patchResult.undo();
}
},
}),
}),
});
export const {
useLazyRemoveChannelQuery,
useGetChannelsQuery,
useCreateChannelMutation,
useSendChannelMsgMutation,