Skip to content

Commit 64f7215

Browse files
committed
fix: tenant slug issue
1 parent fe5592f commit 64f7215

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

public/locales/zh-TW/tenant.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"createNew": "建立新教會",
33
"createDescription": "建立一個新的教會,您將成為其所有者並管理它。",
44
"name": "教會名稱",
5-
"slug": "網址名稱",
5+
"slug": "slug",
66
"creating": "建立中...",
77
"createChurch": "建立教會",
88
"created": "教會已建立",

src/components/Tenants/TenantForm.tsx

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useForm } from "react-hook-form";
22
import { zodResolver } from "@hookform/resolvers/zod";
33
import { z } from "zod";
4+
import { useEffect, useRef } from "react";
45
import { useTranslation } from "react-i18next";
56
import { Button } from "@/components/ui/button";
67
import { Input } from "@/components/ui/input";
@@ -73,19 +74,30 @@ export function TenantForm({
7374
});
7475

7576
const watchName = form.watch("name");
77+
const watchSlug = form.watch("slug");
78+
const userHasEditedSlug = useRef(false);
7679

77-
// Auto-generate slug from name if enabled
78-
if (autoGenerateSlug) {
79-
const slugValue = watchName
80-
.trim()
81-
.toLowerCase()
82-
.replace(/\s+/g, "-")
83-
.replace(/[^a-z0-9-]/g, "");
84-
85-
if (slugValue !== form.getValues("slug")) {
86-
form.setValue("slug", slugValue);
80+
// Track if user has manually edited the slug
81+
useEffect(() => {
82+
if (watchSlug && watchSlug !== initialValues.slug) {
83+
userHasEditedSlug.current = true;
84+
}
85+
}, [watchSlug, initialValues.slug]);
86+
87+
// Auto-generate slug from name if enabled and user hasn't manually edited slug
88+
useEffect(() => {
89+
if (autoGenerateSlug && !userHasEditedSlug.current && watchName) {
90+
const slugValue = watchName
91+
.trim()
92+
.toLowerCase()
93+
.replace(/\s+/g, "-")
94+
.replace(/[^a-z0-9-]/g, "");
95+
96+
if (slugValue !== form.getValues("slug")) {
97+
form.setValue("slug", slugValue, { shouldValidate: false });
98+
}
8799
}
88-
}
100+
}, [watchName, autoGenerateSlug, form]);
89101

90102
const handleFormSubmit = (values: z.infer<typeof formSchema>) => {
91103
onSubmit(values);
@@ -116,11 +128,18 @@ export function TenantForm({
116128
<FormItem>
117129
<FormLabel>{t("slug")}</FormLabel>
118130
<FormControl>
119-
<Input placeholder={t("slug")} {...field} />
131+
<Input
132+
placeholder={t("slug")}
133+
{...field}
134+
onChange={(e) => {
135+
userHasEditedSlug.current = true;
136+
field.onChange(e);
137+
}}
138+
/>
120139
</FormControl>
121140
<FormMessage />
122141
<p className="text-xs text-muted-foreground">
123-
{t("name")}: /tenant/{field.value || "example"}
142+
URL: /tenant/{field.value || "example"}
124143
</p>
125144
</FormItem>
126145
)}

0 commit comments

Comments
 (0)