refactor: useInviteLink

This commit is contained in:
Tristan Yang
2022-06-17 22:18:54 +08:00
parent 9a9fd75e0a
commit b25907f7fc
4 changed files with 16 additions and 22 deletions
+4 -2
View File
@@ -71,12 +71,14 @@ export const channelApi = createApi({
}
}),
createInviteLink: builder.query({
query: (gid) => ({
query: (gid = "") => ({
headers: {
"content-type": "text/plain",
accept: "text/plain"
},
url: `/group/${gid}/create_invite_link`,
url: gid
? `/group/create_reg_magic_link?expired_in=3600&max_times=1&gid=${gid}`
: `/group/create_reg_magic_link?expired_in=3600&max_times=1`,
responseHandler: (response) => response.text()
}),
transformResponse: (link) => {
+9 -17
View File
@@ -1,44 +1,36 @@
import { useState, useEffect } from "react";
import useCopy from "./useCopy";
import {
useLazyCreateInviteLinkQuery as useCreateServerInviteLinkQuery,
useGetSMTPStatusQuery
} from "../../app/services/server";
import { useGetSMTPStatusQuery } from "../../app/services/server";
import { useLazyCreateInviteLinkQuery as useCreateChannelInviteLinkQuery } from "../../app/services/channel";
export default function useInviteLink(cid = null) {
export default function useInviteLink(cid = "") {
const [finalLink, setFinalLink] = useState("");
const { data: SMTPEnabled, isSuccess: smtpStatusFetchSuccess } = useGetSMTPStatusQuery();
const [generateChannelInviteLink, { data: channelInviteLink, isLoading: generatingChannelLink }] =
useCreateChannelInviteLinkQuery();
const [generateServerInviteLink, { data: serverInviteLink, isLoading: generatingServerLink }] =
useCreateServerInviteLinkQuery();
const { copied, copy } = useCopy({ enableToast: false });
const copyLink = () => {
copy(finalLink);
};
useEffect(() => {
if (cid) {
generateChannelInviteLink(cid);
} else {
generateServerInviteLink();
}
generateChannelInviteLink(cid);
}, [cid]);
useEffect(() => {
const _link = serverInviteLink || channelInviteLink;
console.log("fetching", serverInviteLink, channelInviteLink);
const _link = channelInviteLink;
console.log("fetching", channelInviteLink);
if (_link && smtpStatusFetchSuccess) {
// const tmpURL = new URL(_link);
// tmpURL.searchParams.set("code", SMTPEnabled);
setFinalLink(_link);
}
}, [serverInviteLink, channelInviteLink, smtpStatusFetchSuccess]);
}, [channelInviteLink, smtpStatusFetchSuccess]);
const genServerLink = () => {
generateServerInviteLink();
generateChannelInviteLink();
};
return {
enableSMTP: SMTPEnabled,
generating: cid ? generatingChannelLink : generatingServerLink,
generating: generatingChannelLink,
generateNewLink: cid ? generateChannelInviteLink.bind(null, cid) : genServerLink,
link: finalLink,
linkCopied: copied,
+1 -1
View File
@@ -67,7 +67,7 @@ const PageRoutes = () => {
}
/>
<Route
path="/reg"
path="/register"
element={
<RequireNoAuth>
<RegBasePage />
+2 -2
View File
@@ -1,8 +1,8 @@
/* eslint-disable no-undef */
import { useState, useEffect } from "react";
import { useState } from "react";
// import { useDispatch } from "react-redux";
// import { useNavigate } from "react-router-dom";
import toast from "react-hot-toast";
// import toast from "react-hot-toast";
import Input from "../../common/component/styled/Input";
import Button from "../../common/component/styled/Button";
// import { useSendMagicLinkMutation } from "../../app/services/auth";