WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 29f3f23

Browse files
committed
Refactor step handlers in CustomDomainDialog
1 parent 5aa6caf commit 29f3f23

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

apps/web/app/(org)/dashboard/settings/organization/components/CustomDomainDialog/CustomDomainDialog.tsx

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { faGlobe, faRefresh } from "@fortawesome/free-solid-svg-icons";
1111
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
1212
import { useMutation } from "@tanstack/react-query";
1313
import { useRouter } from "next/navigation";
14-
import { useEffect, useReducer, useRef, useState } from "react";
14+
import { useCallback, useEffect, useReducer, useRef, useState } from "react";
1515
import { toast } from "sonner";
1616
import { checkOrganizationDomain } from "@/actions/organization/check-domain";
1717
import { removeOrganizationDomain } from "@/actions/organization/remove-domain";
@@ -141,7 +141,6 @@ const CustomDomainDialog = ({
141141

142142
const pollInterval = useRef<NodeJS.Timeout | undefined>(undefined);
143143

144-
// Mutation for updating domain
145144
const updateDomainMutation = useMutation({
146145
mutationFn: async ({
147146
domain,
@@ -159,7 +158,7 @@ const CustomDomainDialog = ({
159158
);
160159
},
161160
onSuccess: (data) => {
162-
handleNext();
161+
dispatch({ type: "NEXT_STEP" });
163162
toast.success("Domain settings updated");
164163
router.refresh();
165164
if (data) {
@@ -175,7 +174,6 @@ const CustomDomainDialog = ({
175174
},
176175
});
177176

178-
// Mutation for checking domain verification
179177
const checkDomainMutation = useMutation({
180178
mutationFn: async ({
181179
orgId,
@@ -196,10 +194,9 @@ const CustomDomainDialog = ({
196194
setDomainConfig(data.config);
197195

198196
if (data.verified) {
199-
handleNext();
197+
dispatch({ type: "NEXT_STEP" });
200198
}
201199

202-
// Only show toasts if explicitly requested
203200
if (showToasts) {
204201
if (data.verified) {
205202
toast.success("Domain is verified!");
@@ -243,29 +240,49 @@ const CustomDomainDialog = ({
243240
}));
244241

245242
const currentStep = steps[stepState.currentIndex];
246-
const canGoNext = stepState.currentIndex < STEP_CONFIGS.length - 1;
247-
248-
if (!currentStep) {
249-
return null;
250-
}
251243

252-
// Step navigation handlers
253-
const handleNext = () => {
254-
if (canGoNext) {
255-
dispatch({ type: "NEXT_STEP" });
256-
}
257-
};
244+
const handleNext = useCallback(() => {
245+
dispatch({ type: "NEXT_STEP" });
246+
}, []);
258247

259248
const handleStepClick = (index: number) => {
260249
dispatch({ type: "GO_TO_STEP", payload: index });
261250
};
262251

263-
const handleReset = () => {
252+
const handleReset = useCallback(() => {
264253
dispatch({ type: "RESET" });
265254
setDomain("");
266-
};
255+
}, []);
256+
257+
const handleClose = useCallback(() => {
258+
handleReset();
259+
onClose();
260+
}, [handleReset, onClose]);
261+
262+
const checkVerification = useCallback(
263+
(showToasts = true) => {
264+
if (
265+
!activeOrganization?.organization.id ||
266+
!activeOrganization?.organization.customDomain
267+
)
268+
return;
269+
270+
checkDomainMutation.mutate({
271+
orgId: activeOrganization.organization.id,
272+
showToasts,
273+
});
274+
},
275+
[
276+
activeOrganization?.organization.id,
277+
activeOrganization?.organization.customDomain,
278+
checkDomainMutation,
279+
],
280+
);
281+
282+
if (!currentStep) {
283+
return null;
284+
}
267285

268-
// Step-specific handlers
269286
const handleDomainSubmit = async () => {
270287
if (!domain.trim()) {
271288
dispatch({
@@ -319,24 +336,6 @@ const CustomDomainDialog = ({
319336
});
320337
};
321338

322-
const checkVerification = async (showToasts = true) => {
323-
if (
324-
!activeOrganization?.organization.id ||
325-
!activeOrganization?.organization.customDomain
326-
)
327-
return;
328-
329-
checkDomainMutation.mutate({
330-
orgId: activeOrganization.organization.id,
331-
showToasts,
332-
});
333-
};
334-
335-
const handleClose = () => {
336-
handleReset();
337-
onClose();
338-
};
339-
340339
useEffect(() => {
341340
//if current step is success, close dialog in 8 seconds
342341
if (stepState.currentIndex === 2) {

0 commit comments

Comments
 (0)