updated login, added client, staff api, forget and register api
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
import { useMemo, useEffect, useCallback } from "react";
|
||||
import { useMemo, useEffect, useCallback, useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { Checkbox } from "@/components/ui/checkbox";
|
||||
import { FormActions } from "@/components/formActions";
|
||||
import { FormInput } from "@/components/formInput";
|
||||
import { InfiniteSelect, type InfiniteSelectOption, type InfiniteSelectProps } from "@/components/infiniteSelect";
|
||||
import { Checkbox } from "@/components/ui/checkbox";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { FormActions } from "@/components/formActions";
|
||||
import { BentoGrid, BentoCard } from "@/components/bento";
|
||||
import { useCreateClient, useUpdateClient } from "../hooks/useClients";
|
||||
import { clientFormSchema, DEFAULT_FORM_VALUES, buildPayload } from "../schemas/client.schema";
|
||||
import type { ClientFormData } from "../schemas/client.schema";
|
||||
import { useInfiniteRoles } from "@/features/roles/hooks/useRoles";
|
||||
import type { Client } from "../api/clientsApi";
|
||||
import { clientFormSchema, DEFAULT_FORM_VALUES, buildPayload, buildUpdatePayload } from "../schemas/client.schema";
|
||||
import type { ClientFormData } from "../schemas/client.schema";
|
||||
|
||||
// ─── Types ────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -42,13 +45,40 @@ export function ClientForm({ mode, initialData }: ClientFormProps) {
|
||||
);
|
||||
const navigate = useNavigate();
|
||||
|
||||
const createMutation = useCreateClient();
|
||||
const updateMutation = useUpdateClient(initialData?.id ?? 0);
|
||||
const createClient = useCreateClient();
|
||||
const updateClient = useUpdateClient(initialData?.id ?? 0);
|
||||
|
||||
const isPending = createMutation.isPending || updateMutation.isPending;
|
||||
const isPending = createClient.isPending || updateClient.isPending;
|
||||
|
||||
const schema = useMemo(() => clientFormSchema(mode, t), [mode, t, i18n.language]);
|
||||
|
||||
// ── Role infinite-scroll select ──
|
||||
const [roleSearch, setRoleSearch] = useState("");
|
||||
const roleQuery = useInfiniteRoles(roleSearch);
|
||||
|
||||
const roleSelect: Omit<InfiniteSelectProps, "label" | "required" | "placeholder"> = useMemo(() => {
|
||||
const opts: InfiniteSelectOption[] = (roleQuery.data?.pages ?? [])
|
||||
.flatMap((p) => p.roles)
|
||||
.map((r) => ({ value: r.name, label: r.name }));
|
||||
|
||||
if (initialData) {
|
||||
const current = initialData.role;
|
||||
if (current && !opts.some((o) => o.value === current)) {
|
||||
opts.unshift({ value: current, label: current });
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
options: opts,
|
||||
loading: roleQuery.isLoading,
|
||||
isLoadingMore: roleQuery.isFetchingNextPage,
|
||||
hasNextPage: !!roleQuery.hasNextPage,
|
||||
onLoadMore: () => roleQuery.fetchNextPage(),
|
||||
searchValue: roleSearch,
|
||||
onSearchChange: setRoleSearch,
|
||||
};
|
||||
}, [roleQuery, roleSearch, initialData]);
|
||||
|
||||
const {
|
||||
watch,
|
||||
setValue,
|
||||
@@ -64,10 +94,18 @@ export function ClientForm({ mode, initialData }: ClientFormProps) {
|
||||
...DEFAULT_FORM_VALUES,
|
||||
username: initialData.username,
|
||||
name: initialData.name,
|
||||
phone: initialData.phone ?? "",
|
||||
firstName: initialData.firstName ?? "",
|
||||
lastName: initialData.lastName ?? "",
|
||||
phone: initialData.phone.replace(/\D/g, "").slice(-8),
|
||||
email: initialData.email ?? "",
|
||||
avatar: initialData.avatar ?? "",
|
||||
role: initialData.role ?? "",
|
||||
socialFacebook: initialData.socialMedia?.facebook ?? "",
|
||||
socialInstagram: initialData.socialMedia?.instagram ?? "",
|
||||
socialTelegram: initialData.socialMedia?.telegram ?? "",
|
||||
lightMode: !!initialData.lightMode,
|
||||
password: "",
|
||||
isActive: initialData.isActive,
|
||||
active: !!initialData.active,
|
||||
}
|
||||
: DEFAULT_FORM_VALUES,
|
||||
});
|
||||
@@ -88,7 +126,6 @@ export function ClientForm({ mode, initialData }: ClientFormProps) {
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [i18n.language]);
|
||||
|
||||
// ── Submit ──────────────────────────────────────────────────────────────────
|
||||
const handleSubmit = async () => {
|
||||
const isValid = await trigger();
|
||||
if (!isValid) return;
|
||||
@@ -97,27 +134,15 @@ export function ClientForm({ mode, initialData }: ClientFormProps) {
|
||||
|
||||
if (mode === "create") {
|
||||
const payload = buildPayload(data);
|
||||
createMutation.mutate(payload, {
|
||||
onSuccess: () => navigate("/clients"),
|
||||
});
|
||||
await createClient.mutateAsync(payload);
|
||||
navigate("/clients");
|
||||
} else {
|
||||
updateMutation.mutate(
|
||||
{
|
||||
username: data.username.trim(),
|
||||
name: data.name.trim(),
|
||||
phone: data.phone.trim(),
|
||||
email: data.email.trim() || undefined,
|
||||
isActive: data.isActive,
|
||||
...(data.password.trim() ? { password: data.password } : {}),
|
||||
},
|
||||
{
|
||||
onSuccess: () => navigate(`/clients/${initialData!.id}`),
|
||||
},
|
||||
);
|
||||
const payload = buildUpdatePayload(data);
|
||||
await updateClient.mutateAsync(payload);
|
||||
navigate(`/clients/${initialData!.id}`);
|
||||
}
|
||||
};
|
||||
|
||||
// ── Render ──────────────────────────────────────────────────────────────────
|
||||
return (
|
||||
<div className="flex flex-col gap-5">
|
||||
<h1 className="text-xl font-semibold text-foreground">
|
||||
@@ -127,88 +152,178 @@ export function ClientForm({ mode, initialData }: ClientFormProps) {
|
||||
<BentoCard>
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t("clients.fields.username", "Ulanyjy ady")}
|
||||
label={t("users.fields.username", "Ulanyjy ady")}
|
||||
value={form.username}
|
||||
onChange={(v) => setField("username", v)}
|
||||
placeholder={t("clients.fields.username", "Ulanyjy ady")}
|
||||
placeholder={t("users.fields.username", "Ulanyjy ady")}
|
||||
error={errors.username}
|
||||
disabled={isPending}
|
||||
required
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground -mt-3">{t("users.fields.usernameHint", "Unikal login ady")}</p>
|
||||
</BentoCard>
|
||||
<BentoCard>
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t("users.fields.name", "Ady")}
|
||||
value={form.name}
|
||||
onChange={(v) => setField("name", v)}
|
||||
placeholder={t("users.fields.name", "Ady")}
|
||||
error={errors.name}
|
||||
disabled={isPending}
|
||||
required
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground -mt-3">{t("users.fields.nameHint", "Doly ady we familiýasy")}</p>
|
||||
</BentoCard>
|
||||
<BentoCard>
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t("users.fields.firstName", "Ady (at)")}
|
||||
value={form.firstName}
|
||||
onChange={(v) => setField("firstName", v)}
|
||||
placeholder={t("users.fields.firstName", "Ady (at)")}
|
||||
error={errors.firstName}
|
||||
disabled={isPending}
|
||||
/>
|
||||
</BentoCard>
|
||||
<BentoCard>
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t("clients.fields.name", "Ady")}
|
||||
value={form.name}
|
||||
onChange={(v) => setField("name", v)}
|
||||
placeholder={t("clients.fields.name", "Ady")}
|
||||
error={errors.name}
|
||||
label={t("users.fields.lastName", "Familiýasy")}
|
||||
value={form.lastName}
|
||||
onChange={(v) => setField("lastName", v)}
|
||||
placeholder={t("users.fields.lastName", "Familiýasy")}
|
||||
error={errors.lastName}
|
||||
disabled={isPending}
|
||||
required
|
||||
/>
|
||||
</BentoCard>
|
||||
<BentoCard>
|
||||
<FormInput
|
||||
type="phone"
|
||||
label={t("clients.fields.phone", "Telefon")}
|
||||
label={t("users.fields.phone", "Telefon")}
|
||||
value={form.phone}
|
||||
onChange={(v) => setField("phone", v)}
|
||||
placeholder="62-38-49-56"
|
||||
error={errors.phone}
|
||||
disabled={isPending}
|
||||
required
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground -mt-3">{t("users.fields.phoneHint", "+993 bilen başlanýar")}</p>
|
||||
</BentoCard>
|
||||
<BentoCard>
|
||||
<FormInput
|
||||
type="email"
|
||||
label={t("clients.fields.email", "E-poçta")}
|
||||
label={t("users.fields.email", "E-poçta")}
|
||||
value={form.email}
|
||||
onChange={(v) => setField("email", v)}
|
||||
placeholder={t("clients.fields.email", "E-poçta")}
|
||||
placeholder={t("users.fields.email", "E-poçta")}
|
||||
error={errors.email}
|
||||
disabled={isPending}
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground -mt-3">{t("users.fields.emailHint", "Islege görä")}</p>
|
||||
</BentoCard>
|
||||
<BentoCard>
|
||||
<InfiniteSelect
|
||||
label={t("users.fields.role", "Roly")}
|
||||
value={form.role}
|
||||
onChange={(v) => setField("role", v)}
|
||||
placeholder={t("users.fields.role", "Roly")}
|
||||
error={errors.role}
|
||||
disabled={isPending}
|
||||
required
|
||||
{...roleSelect}
|
||||
/>
|
||||
</BentoCard>
|
||||
</BentoGrid>
|
||||
<BentoGrid cols={1}>
|
||||
<BentoCard>
|
||||
<FormInput
|
||||
type="password"
|
||||
label={t("clients.fields.password", "Açar sözi") + (mode === "create" ? " *" : "")}
|
||||
value={form.password}
|
||||
onChange={(v) => setField("password", v)}
|
||||
placeholder={
|
||||
mode === "edit"
|
||||
? t("clients.fields.passwordEditHint", "Üýtgetmek üçin täze açar söz giriziň")
|
||||
: t("clients.fields.password", "Açar sözi")
|
||||
}
|
||||
error={errors.password}
|
||||
type="text"
|
||||
label={t("users.fields.avatar", "Awatar URL")}
|
||||
value={form.avatar}
|
||||
onChange={(v) => setField("avatar", v)}
|
||||
placeholder="https://..."
|
||||
error={errors.avatar}
|
||||
disabled={isPending}
|
||||
/>
|
||||
{mode === "edit" && (
|
||||
<p className="text-xs text-muted-foreground -mt-3">
|
||||
{t("clients.fields.passwordEditNote", "Boş goýsaňyz, açar sözi üýtgemez")}
|
||||
</p>
|
||||
)}
|
||||
</BentoCard>
|
||||
<BentoCard>
|
||||
<div className="flex items-center gap-3">
|
||||
<span className="text-sm text-muted-foreground">{t("clients.fields.isActive", "Işjeň")}</span>
|
||||
<Checkbox
|
||||
checked={form.isActive}
|
||||
onCheckedChange={(v) => setField("isActive", !!v)}
|
||||
{mode === "create" && (
|
||||
<BentoCard>
|
||||
<FormInput
|
||||
type="password"
|
||||
label={t("users.fields.password", "Açar sözi")}
|
||||
value={form.password}
|
||||
onChange={(v) => setField("password", v)}
|
||||
placeholder={t("users.fields.password", "Açar sözi")}
|
||||
error={errors.password}
|
||||
disabled={isPending}
|
||||
required
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground -mt-3">{t("users.fields.passwordHint", "Iň az 6 harp")}</p>
|
||||
</BentoCard>
|
||||
)}
|
||||
<BentoCard>
|
||||
<div className="space-y-1">
|
||||
<p className="text-sm font-semibold text-muted-foreground">{t("users.fields.socialMedia", "Sosial media")}</p>
|
||||
</div>
|
||||
<div className="flex flex-col gap-3">
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t("users.fields.facebook", "Facebook")}
|
||||
value={form.socialFacebook}
|
||||
onChange={(v) => setField("socialFacebook", v)}
|
||||
placeholder="facebook.com/..."
|
||||
error={errors.socialFacebook}
|
||||
disabled={isPending}
|
||||
/>
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t("users.fields.instagram", "Instagram")}
|
||||
value={form.socialInstagram}
|
||||
onChange={(v) => setField("socialInstagram", v)}
|
||||
placeholder="instagram.com/..."
|
||||
error={errors.socialInstagram}
|
||||
disabled={isPending}
|
||||
/>
|
||||
<FormInput
|
||||
type="text"
|
||||
label={t("users.fields.telegram", "Telegram")}
|
||||
value={form.socialTelegram}
|
||||
onChange={(v) => setField("socialTelegram", v)}
|
||||
placeholder="t.me/..."
|
||||
error={errors.socialTelegram}
|
||||
disabled={isPending}
|
||||
className="data-[state=checked]:bg-primary data-[state=checked]:border-primary"
|
||||
/>
|
||||
</div>
|
||||
<FormActions
|
||||
isPending={isPending}
|
||||
onSubmit={handleSubmit}
|
||||
onCancel={() => navigate(-1)}
|
||||
submitLabel={mode === "create" ? t("clients.createBtn", "Müşderi döredin") : t("clients.updateBtn", "Üýtgetmeleri sakla")}
|
||||
/>
|
||||
</BentoCard>
|
||||
<BentoCard>
|
||||
<div className="space-y-1">
|
||||
<p className="text-sm font-semibold text-muted-foreground">{t("users.fields.lightMode", "Light režimi")}</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Checkbox id="lightMode" checked={form.lightMode} onCheckedChange={(v) => setField("lightMode", !!v)} disabled={isPending} />
|
||||
<Label htmlFor="lightMode" className="text-sm text-muted-foreground cursor-pointer">
|
||||
{form.lightMode ? t("common.yes", "Hawa") : t("common.no", "Ýok")}
|
||||
</Label>
|
||||
</div>
|
||||
</BentoCard>
|
||||
<BentoCard>
|
||||
<div className="space-y-1">
|
||||
<p className="text-sm font-semibold text-muted-foreground">{t("users.fields.isActive", "Işjeň")}</p>
|
||||
<p className="text-xs text-muted-foreground">{t("users.fields.isActiveHint", "Ulanyjynyň ulgama girip-bilmezligi")}</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Checkbox id="active" checked={form.active} onCheckedChange={(v) => setField("active", !!v)} disabled={isPending} />
|
||||
<Label htmlFor="active" className="text-sm text-muted-foreground cursor-pointer">
|
||||
{form.active ? t("common.active", "Işjeň") : t("common.inactive", "Işjeň däl")}
|
||||
</Label>
|
||||
</div>
|
||||
</BentoCard>
|
||||
</BentoGrid>
|
||||
<FormActions
|
||||
isPending={isPending}
|
||||
onSubmit={handleSubmit}
|
||||
onCancel={() => navigate(-1)}
|
||||
submitLabel={mode === "create" ? t("users.actions.create", "Ulanyjy dörediň") : t("users.actions.save", "Ýatda sakla")}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user