refactor: useInviteLink
This commit is contained in:
@@ -71,12 +71,14 @@ export const channelApi = createApi({
|
|||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
createInviteLink: builder.query({
|
createInviteLink: builder.query({
|
||||||
query: (gid) => ({
|
query: (gid = "") => ({
|
||||||
headers: {
|
headers: {
|
||||||
"content-type": "text/plain",
|
"content-type": "text/plain",
|
||||||
accept: "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()
|
responseHandler: (response) => response.text()
|
||||||
}),
|
}),
|
||||||
transformResponse: (link) => {
|
transformResponse: (link) => {
|
||||||
|
|||||||
@@ -1,44 +1,36 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import useCopy from "./useCopy";
|
import useCopy from "./useCopy";
|
||||||
import {
|
import { useGetSMTPStatusQuery } from "../../app/services/server";
|
||||||
useLazyCreateInviteLinkQuery as useCreateServerInviteLinkQuery,
|
|
||||||
useGetSMTPStatusQuery
|
|
||||||
} from "../../app/services/server";
|
|
||||||
import { useLazyCreateInviteLinkQuery as useCreateChannelInviteLinkQuery } from "../../app/services/channel";
|
import { useLazyCreateInviteLinkQuery as useCreateChannelInviteLinkQuery } from "../../app/services/channel";
|
||||||
export default function useInviteLink(cid = null) {
|
export default function useInviteLink(cid = "") {
|
||||||
const [finalLink, setFinalLink] = useState("");
|
const [finalLink, setFinalLink] = useState("");
|
||||||
const { data: SMTPEnabled, isSuccess: smtpStatusFetchSuccess } = useGetSMTPStatusQuery();
|
const { data: SMTPEnabled, isSuccess: smtpStatusFetchSuccess } = useGetSMTPStatusQuery();
|
||||||
const [generateChannelInviteLink, { data: channelInviteLink, isLoading: generatingChannelLink }] =
|
const [generateChannelInviteLink, { data: channelInviteLink, isLoading: generatingChannelLink }] =
|
||||||
useCreateChannelInviteLinkQuery();
|
useCreateChannelInviteLinkQuery();
|
||||||
const [generateServerInviteLink, { data: serverInviteLink, isLoading: generatingServerLink }] =
|
|
||||||
useCreateServerInviteLinkQuery();
|
|
||||||
const { copied, copy } = useCopy({ enableToast: false });
|
const { copied, copy } = useCopy({ enableToast: false });
|
||||||
const copyLink = () => {
|
const copyLink = () => {
|
||||||
copy(finalLink);
|
copy(finalLink);
|
||||||
};
|
};
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (cid) {
|
generateChannelInviteLink(cid);
|
||||||
generateChannelInviteLink(cid);
|
|
||||||
} else {
|
|
||||||
generateServerInviteLink();
|
|
||||||
}
|
|
||||||
}, [cid]);
|
}, [cid]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const _link = serverInviteLink || channelInviteLink;
|
const _link = channelInviteLink;
|
||||||
console.log("fetching", serverInviteLink, channelInviteLink);
|
console.log("fetching", channelInviteLink);
|
||||||
if (_link && smtpStatusFetchSuccess) {
|
if (_link && smtpStatusFetchSuccess) {
|
||||||
// const tmpURL = new URL(_link);
|
// const tmpURL = new URL(_link);
|
||||||
// tmpURL.searchParams.set("code", SMTPEnabled);
|
// tmpURL.searchParams.set("code", SMTPEnabled);
|
||||||
setFinalLink(_link);
|
setFinalLink(_link);
|
||||||
}
|
}
|
||||||
}, [serverInviteLink, channelInviteLink, smtpStatusFetchSuccess]);
|
}, [channelInviteLink, smtpStatusFetchSuccess]);
|
||||||
const genServerLink = () => {
|
const genServerLink = () => {
|
||||||
generateServerInviteLink();
|
generateChannelInviteLink();
|
||||||
};
|
};
|
||||||
return {
|
return {
|
||||||
enableSMTP: SMTPEnabled,
|
enableSMTP: SMTPEnabled,
|
||||||
generating: cid ? generatingChannelLink : generatingServerLink,
|
generating: generatingChannelLink,
|
||||||
generateNewLink: cid ? generateChannelInviteLink.bind(null, cid) : genServerLink,
|
generateNewLink: cid ? generateChannelInviteLink.bind(null, cid) : genServerLink,
|
||||||
link: finalLink,
|
link: finalLink,
|
||||||
linkCopied: copied,
|
linkCopied: copied,
|
||||||
|
|||||||
+1
-1
@@ -67,7 +67,7 @@ const PageRoutes = () => {
|
|||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<Route
|
<Route
|
||||||
path="/reg"
|
path="/register"
|
||||||
element={
|
element={
|
||||||
<RequireNoAuth>
|
<RequireNoAuth>
|
||||||
<RegBasePage />
|
<RegBasePage />
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
/* eslint-disable no-undef */
|
/* eslint-disable no-undef */
|
||||||
import { useState, useEffect } from "react";
|
import { useState } from "react";
|
||||||
// import { useDispatch } from "react-redux";
|
// import { useDispatch } from "react-redux";
|
||||||
// import { useNavigate } from "react-router-dom";
|
// 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 Input from "../../common/component/styled/Input";
|
||||||
import Button from "../../common/component/styled/Button";
|
import Button from "../../common/component/styled/Button";
|
||||||
// import { useSendMagicLinkMutation } from "../../app/services/auth";
|
// import { useSendMagicLinkMutation } from "../../app/services/auth";
|
||||||
|
|||||||
Reference in New Issue
Block a user