- {title.includes("فرم") && (
+ {title?.includes("فرم") && (
{
e.stopPropagation();
onView && onView();
- setShowModal(!showModal);
}}
className="flex items-center gap-[6px] px-2 ml-2 border-2 border-[#AA00FF] bg-white text-[#AA00FF] text-lg rounded-full"
type="button"
>
دیدن
- setShowModal(false)} questions={questions} />
)}
{checked ?
:
}
diff --git a/src/apps/new-ui/pages/OpinionSMS/index.tsx b/src/apps/new-ui/pages/OpinionSMS/index.tsx
index d802769e..000b31bc 100644
--- a/src/apps/new-ui/pages/OpinionSMS/index.tsx
+++ b/src/apps/new-ui/pages/OpinionSMS/index.tsx
@@ -1,5 +1,5 @@
-import { useMemo, useState } from "react";
-import { CustomerItem, FormItem, CreatedSurvey } from "./types";
+// OpinionSMS.tsx
+import React, { Suspense, useEffect, useMemo, useState } from "react";
import Header from "./+components/Header";
import SearchBar from "./+components/SearchBar";
import GaugeCard from "./+components/GaugeCard";
@@ -7,166 +7,209 @@ import EmptyState from "./+components/EmptyState";
import CreatedSurveyCard from "./+components/CreatedSurveyCard";
import CreatePanel from "./+components/CreatePanel";
import BottomActions from "./+components/BottomActions";
-import profilePicture from "@new-ui/assets/Profile pic.png";
-import waitingIcon from "@new-ui/assets/waiting-img.svg";
+import { BeatLoader } from "react-spinners";
import "./OpinionSMS.css";
-const opinionSMSListItemsTest: CustomerItem[] = [
- { id: 1, img: profilePicture, checked: false, title: "1 پریسا آذری", description: "09121234567" },
- { id: 2, img: profilePicture, checked: false, title: "2 پریسا آذری", description: "09121234567" },
- { id: 3, img: profilePicture, checked: false, title: "پریسا آذری", description: "09121234567" },
- { id: 4, img: profilePicture, checked: false, title: "پریسا آذری", description: "09121234567" },
- { id: 5, img: profilePicture, checked: false, title: "پریسا آذری", description: "09121234567" },
- { id: 6, img: profilePicture, checked: false, title: "پریسا آذری", description: "09121234567" },
- { id: 7, img: profilePicture, checked: false, title: "پریسا آذری", description: "09121234567" },
-];
-
-const opinionSMSFormsListItemsTest: FormItem[] = [
- {
- id: 1,
- img: waitingIcon,
- checked: false,
- title: "فرم شماره 1",
- description: "سوالات استاندارد",
- questions: [
- { id: 1, title: "رفتار و برخورد آرایشگر چگونه بود ؟", choices: ["بسیار خوب", "خوب", "معمولی", "بد"] },
- { id: 2, title: "میزان تمیزی و بهداشت وسایل چقدر بود تا خط دوم برسد ؟" },
- { id: 3, title: "آیا وقت دهی به موقع انجام شد ؟" },
- ],
- },
- { id: 2, img: waitingIcon, checked: false, title: "فرم شماره 2", description: "سوالات تخصصی ترمیم" },
- { id: 3, img: waitingIcon, checked: false, title: "فرم شماره 3", description: "سوالات تخصصی ترمیم" },
- { id: 4, img: waitingIcon, checked: false, title: "فرم شماره 4", description: "سوالات تخصصی ترمیم" },
-];
+import { useGetAllSurveyForms, useCreateSurvey, useDeleteSurveyForm } from "../../services/OpinionSMS";
+import { useNavigate } from "react-router-dom";
const OpinionSMS: React.FC = () => {
- const [customers] = useState
(opinionSMSListItemsTest);
- const [forms] = useState(opinionSMSFormsListItemsTest);
+ const { data, isLoading, refetch } = useGetAllSurveyForms();
+ const { mutate: deleteSurveyForm, isPending: deleteLoyaltyCardPending } = useDeleteSurveyForm();
+ // نگهداری دادهی خامِ سرور (بدون نرمالایز)
+ const [localSurveys, setLocalSurveys] = useState(null);
+ const navigate = useNavigate();
- // selection states
+ // انتخابها و UI state
const [selectedCustomerId, setSelectedCustomerId] = useState(null);
+ const [selectedCustomer, setSelectedCustomer] = useState();
const [selectedFormId, setSelectedFormId] = useState(null);
+ const [selectedForm, setSelectedForm] = useState();
const [isShowCreateList, setIsShowCreateList] = useState(false);
-
- // created surveys
- const [createdSurveys, setCreatedSurveys] = useState([]);
const [showCreatedPreviewId, setShowCreatedPreviewId] = useState(null);
- // searches
+ // جستجوها
const [searchMain, setSearchMain] = useState("");
const [customerSearch, setCustomerSearch] = useState("");
- const selectedCustomer = useMemo(
- () => customers.find((c) => c.id === selectedCustomerId) || null,
- [customers, selectedCustomerId]
- );
- const selectedForm = useMemo(() => forms.find((f) => f.id === selectedFormId) || null, [forms, selectedFormId]);
+ // hook ایجاد (فرض react-query style)
+ const { mutateAsync: createSurveyAsync } = useCreateSurvey();
- const totalSurveys = createdSurveys.length;
- const averagePercent = 0; // placeholder
+ // وقتی داده از hook آمد، مستقیم آن آرایهٔ surveys را در localSurveys قرار بده (بدون تبدیل)
+ useEffect(() => {
+ if (!data) {
+ setLocalSurveys([]);
+ return;
+ }
- const submitSurvey = () => {
- if (!(selectedCustomer && selectedForm)) return;
+ console.log(data);
- const payload: CreatedSurvey = {
- id: `${Date.now()}`,
- customer: selectedCustomer,
- form: selectedForm,
- createdAt: new Date().toISOString(),
+ if (Array.isArray((data as any).surveys)) {
+ setLocalSurveys((data as any).surveys);
+ return;
+ }
+
+ // اگر data خودش آرایه بود
+ if (Array.isArray(data as any)) {
+ setLocalSurveys(data as any);
+ return;
+ }
+
+ // fallback: اگر ساختار متفاوت بود، خالی بمونه (شما خواستی نرمالایز نشه)
+ setLocalSurveys([]);
+ }, [data]);
+
+ const totalSurveys = (localSurveys ?? []).length;
+
+ // فیلتر ساده روی فیلدهای خامِ سرور — بدون نرمالایز یا map
+ const filteredCreatedSurveys = useMemo(() => {
+ const list = localSurveys ?? [];
+ const t = searchMain.trim().toLowerCase();
+ if (!t) return list;
+ return list.filter((s: any) => {
+ const name = (s.customer_name ?? "").toString().toLowerCase();
+ const phone = (s.customer_phone_number ?? "").toString().toLowerCase();
+ const formId = (s.question_form ?? "").toString().toLowerCase();
+ const link = (s.link ?? "").toString().toLowerCase();
+ const created = (s.created_at ?? "").toString().toLowerCase();
+ // const img =
+ return (
+ name.includes(t) || phone.includes(t) || formId.includes(t) || link.includes(t) || created.includes(t)
+ );
+ });
+ }, [localSurveys, searchMain]);
+
+ // submitSurvey حالا از useCreateSurvey استفاده میکند.
+ // payload باید حداقل شامل: { customer_name, customer_phone_number, question_form }
+ const submitSurvey = async (e?: React.FormEvent | any) => {
+ if (e?.preventDefault) e.preventDefault();
+
+ if (selectedCustomer == null || selectedFormId == null) return;
+
+ // ساخت payload نهایی از stateها
+ const payloadToSend = {
+ customer_name: selectedCustomer.name,
+ customer_phone_number: selectedCustomer.phone_number,
+ question_form: selectedFormId,
};
- // reset selection and hide panel first
- setSelectedCustomerId(null);
- setSelectedFormId(null);
- setIsShowCreateList(false);
+ try {
+ const res = await createSurveyAsync(payloadToSend);
+ await refetch();
- setTimeout(() => {
- setCreatedSurveys((p) => [payload, ...p]);
- setShowCreatedPreviewId(payload.id);
- }, 0);
- };
+ // پاک کردن stateها بعد از ایجاد موفق
+ setSelectedCustomerId(null);
+ setSelectedFormId(null);
+ setSelectedCustomer(null);
+ setSelectedForm(null);
+ setIsShowCreateList(false);
- const handleDeleteCreatedSurvey = (id: string) => {
- setCreatedSurveys((p) => p.filter((s) => s.id !== id));
- if (showCreatedPreviewId === id) setShowCreatedPreviewId(null);
- };
-
- const handleShareCreatedSurvey = (s: CreatedSurvey) => {
- const text = `فرم نظرسنجی برای ${s.customer.title} - ${s.form.title}`;
- if ((navigator as any).share) {
- (navigator as any).share({ title: "فرم نظرسنجی", text }).catch(() => {});
- } else {
- console.log("share:", text);
+ // در صورت نیاز نمایش پیشنمایش
+ const createdId = res && (res as any).id ? String((res as any).id) : String(Date.now());
+ setShowCreatedPreviewId(createdId);
+ } catch (error) {
+ console.error("❌ خطا در ایجاد نظرسنجی:", error);
}
};
- const filteredCreatedSurveys = useMemo(() => {
- const t = searchMain.trim();
- if (!t) return createdSurveys;
- return createdSurveys.filter(
- (s) =>
- s.form.title.includes(t) ||
- s.form.description?.includes(t) ||
- s.customer.title.includes(t) ||
- s.customer.description?.includes(t)
- );
- }, [createdSurveys, searchMain]);
+ const handleDeleteCreatedSurvey = async (id: number) => {
+ setLocalSurveys((p) => (p ? p.filter((s) => String(s.id) !== String(id)) : p));
+ if (showCreatedPreviewId && String(showCreatedPreviewId) === String(id)) setShowCreatedPreviewId(null);
+ // اگر لازم باشه میتونی اینجا درخواست حذف سرور بزنی و بعد refetch()
+
+ deleteSurveyForm(id);
+ await refetch();
+ };
+
+ const handleShareCreatedSurvey = (s: any) => {
+ const text = `فرم نظرسنجی برای ${s.customer_name ?? "مشتری"} - فرم: ${s.question_form ?? ""}`;
+ if ((navigator as any).share) {
+ (navigator as any).share({ title: "فرم نظرسنجی", text }).catch(() => {});
+ } else {
+ navigator.clipboard?.writeText(text).catch(() => {});
+ console.log("share (fallback):", text);
+ }
+ };
+
+ const anyLoading = isLoading && (localSurveys == null || localSurveys.length === 0);
return (
<>
+
{/* Gauge + Main Search */}
{!isShowCreateList && totalSurveys > 0 && (
-
+
)}
{/* Created Surveys */}
- {!isShowCreateList && filteredCreatedSurveys.length > 0 ? (
+ {!isShowCreateList ? (
- {filteredCreatedSurveys.map((s) => (
-
-
-
- ))}
+
+
+
+ }
+ >
+ {anyLoading ? (
+
+
+
+ ) : filteredCreatedSurveys.length > 0 ? (
+ filteredCreatedSurveys.map((s: any) => (
+
+ handleDeleteCreatedSurvey(s.id)}
+ onShare={() => handleShareCreatedSurvey(s)}
+ />
+
+ ))
+ ) : (
+ setIsShowCreateList(true)} />
+ )}
+