refactor: avatar

This commit is contained in:
zerosoul
2022-02-19 15:56:28 +08:00
parent 156d9dbac9
commit f96ca44841
7 changed files with 157 additions and 118 deletions
+40 -38
View File
@@ -1,44 +1,46 @@
import { createApi } from '@reduxjs/toolkit/query/react';
import baseQuery from './base.query';
import { createApi } from "@reduxjs/toolkit/query/react";
import baseQuery from "./base.query";
import BASE_URL from "../config";
export const authApi = createApi({
reducerPath: 'auth',
baseQuery,
endpoints: (builder) => ({
login: builder.mutation({
query: (credentials) => ({
url: 'token/login',
method: 'POST',
body: { credential: credentials, device: 'web', device_token: 'test' }
})
// transformResponse: (resp) => {
// console.log("resp", resp);
// if (resp.status == 401) {
// resp.msg = "username or password incorrect";
// }
// return resp;
// },
reducerPath: "auth",
baseQuery,
endpoints: (builder) => ({
login: builder.mutation({
query: (credentials) => ({
url: "token/login",
method: "POST",
body: { credential: credentials, device: "web", device_token: "test" },
}),
transformResponse: (data) => {
const { avatar_updated_at } = data.user;
data.user.avatar =
avatar_updated_at == 0
? ""
: `${BASE_URL}/resource/avatar?uid=${data.user.uid}`;
return data;
},
}),
checkInviteTokenValid: builder.mutation({
query: (token) => ({
url: "user/check_invite_magic_token",
method: "POST",
body: { magic_token: token },
}),
}),
getMetamaskNonce: builder.query({
query: (address) => ({
url: `/token/metamask/nonce?public_address=${address}`,
}),
}),
logout: builder.query({
query: () => ({ url: `token/logout` }),
}),
}),
checkInviteTokenValid: builder.mutation({
query: (token) => ({
url: 'user/check_invite_magic_token',
method: 'POST',
body: { magic_token: token }
})
}),
getMetamaskNonce: builder.query({
query: (address) => ({
url: `/token/metamask/nonce?public_address=${address}`
})
}),
logout: builder.query({
query: () => ({ url: `token/logout` })
})
})
});
export const {
useLazyGetMetamaskNonceQuery,
useLoginMutation,
useLazyLogoutQuery,
useCheckInviteTokenValidMutation
useLazyGetMetamaskNonceQuery,
useLoginMutation,
useLazyLogoutQuery,
useCheckInviteTokenValidMutation,
} = authApi;
+47 -40
View File
@@ -1,45 +1,52 @@
import { createApi } from '@reduxjs/toolkit/query/react';
import baseQuery from './base.query';
import BASE_URL, { ContentTypes } from '../config';
import { REHYDRATE } from 'redux-persist';
import { createApi } from "@reduxjs/toolkit/query/react";
import baseQuery from "./base.query";
import BASE_URL, { ContentTypes } from "../config";
import { REHYDRATE } from "redux-persist";
export const contactApi = createApi({
reducerPath: 'contact',
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;
});
}
reducerPath: "contact",
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 =
user.avatar_updated_at == 0
? ""
: `${BASE_URL}/resource/avatar?uid=${user.uid}`;
user.avatar = avatar;
return user;
});
},
}),
register: builder.mutation({
query: (data) => ({
url: `user/register`,
method: "POST",
body: data,
}),
}),
sendMsg: builder.mutation({
query: ({ id, content, type = "text" }) => ({
headers: {
"content-type": ContentTypes[type],
},
url: `user/${id}/send`,
method: "POST",
body: content,
}),
}),
}),
register: builder.mutation({
query: (data) => ({
url: `user/register`,
method: 'POST',
body: data
})
}),
sendMsg: builder.mutation({
query: ({ id, content, type = 'text' }) => ({
headers: {
'content-type': ContentTypes[type]
},
url: `user/${id}/send`,
method: 'POST',
body: content
})
})
})
});
export const { useGetContactsQuery, useSendMsgMutation, useRegisterMutation } = contactApi;
export const {
useGetContactsQuery,
useSendMsgMutation,
useRegisterMutation,
} = contactApi;