feat: lots of updates

This commit is contained in:
zerosoul
2022-01-27 22:06:44 +08:00
commit e18c7323a6
72 changed files with 12432 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
import BASE_URL from "../config";
export const authApi = createApi({
reducerPath: "auth",
baseQuery: fetchBaseQuery({ baseUrl: BASE_URL }),
endpoints: (builder) => ({
login: builder.mutation({
query: (credentials) => ({
url: "token/login",
method: "POST",
body: credentials,
}),
// transformResponse: (resp) => {
// console.log("resp", resp);
// if (resp.status == 401) {
// resp.msg = "username or password incorrect";
// }
// return resp;
// },
}),
}),
});
export const { useLoginMutation } = authApi;
+40
View File
@@ -0,0 +1,40 @@
import { fetchBaseQuery } from "@reduxjs/toolkit/query";
import BASE_URL, { tokenHeader } from "../config";
// const whiteList = [
// "/resource/avatar",
// "/resource/company/logo",
// "/resource/thumbnail",
// "/resource/image",
// "/token/login",
// "/token/renew",
// "/user",
// "/admin/system/company",
// ];
const baseQuery = fetchBaseQuery({
baseUrl: BASE_URL,
prepareHeaders: (headers, { getState }) => {
const { token } = getState().authData;
if (token) {
headers.set(tokenHeader, token);
}
return headers;
},
});
const baseQueryWithReauth = async (args, api, extraOptions) => {
let result = await baseQuery(args, api, extraOptions);
if (result.error && result.error.status === 401) {
// try to get a new token with refreshToken
// const refreshResult = await baseQuery('/refreshToken', api, extraOptions);
// if (refreshResult.data) {
// // store the new token
// api.dispatch(tokenReceived(refreshResult.data));
// // retry the initial query
// result = await baseQuery(args, api, extraOptions);
// } else {
// api.dispatch(loggedOut());
// }
}
return result;
};
export default baseQueryWithReauth;
+38
View File
@@ -0,0 +1,38 @@
import { createApi } from "@reduxjs/toolkit/query/react";
import baseQuery from "./base.query";
import { REHYDRATE } from "redux-persist";
export const channelApi = createApi({
reducerPath: "groups",
baseQuery,
extractRehydrationInfo(action, { reducerPath }) {
if (action.type === REHYDRATE) {
return action.payload ? action.payload[reducerPath] : undefined;
}
},
refetchOnFocus: true,
endpoints: (builder) => ({
getChannels: builder.query({
query: () => ({ url: `group` }),
}),
createChannel: builder.mutation({
query: (data) => ({
url: "group",
method: "POST",
body: data,
}),
}),
sendChannelMsg: builder.mutation({
query: ({ gid, message }) => ({
url: `group/${gid}/send`,
method: "POST",
body: message,
}),
}),
}),
});
export const {
useGetChannelsQuery,
useCreateChannelMutation,
useSendChannelMsgMutation,
} = channelApi;
+34
View File
@@ -0,0 +1,34 @@
import { createApi } from "@reduxjs/toolkit/query/react";
import baseQuery from "./base.query";
import BASE_URL from "../config";
import { REHYDRATE } from "redux-persist";
export const contactApi = createApi({
reducerPath: "contacts",
baseQuery,
extractRehydrationInfo(action, { reducerPath }) {
if (action.type === REHYDRATE) {
return action.payload ? action.payload[reducerPath] : undefined;
}
},
endpoints: (builder) => ({
getContacts: builder.query({
query: () => ({ url: `user` }),
transformResponse: (data) => {
return data.map((user) => {
const avatar = `${BASE_URL}/resource/avatar?uid=${user.uid}`;
user.avatar = avatar;
return user;
});
},
}),
sendMsg: builder.mutation({
query: ({ uid, message }) => ({
url: `user/${uid}/send`,
method: "POST",
body: message,
}),
}),
}),
});
export const { useGetContactsQuery, useSendMsgMutation } = contactApi;
+26
View File
@@ -0,0 +1,26 @@
import { createApi } from "@reduxjs/toolkit/query/react";
import BASE_URL from "../config";
// import { REHYDRATE } from 'redux-persist';
import baseQuery from "./base.query";
export const serverApi = createApi({
reducerPath: "server",
baseQuery,
// extractRehydrationInfo(action, { reducerPath }) {
// if (action.type === REHYDRATE) {
// return action.payload ? action.payload[reducerPath] : undefined;
// }
// },
endpoints: (builder) => ({
getServer: builder.query({
query: () => ({ url: `admin/system/company` }),
transformResponse: (data) => {
data.logo = `${BASE_URL}/resource/company/logo`;
return data;
},
}),
}),
});
export const { useGetServerQuery } = serverApi;