feat(auth/contact/sms): sync contact submissions in admin, refine SMS error logs, trigger Auth Modal on 401 [TASK-13.1, TASK-14.1, TASK-15.1]

This commit is contained in:
parsa aghaei committed 2026-08-03 11:47:50 +03:30
1 parent 89c836d7b6
commit d52d1e57f2
4 files changed
+34 -33

No files matched your search

@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import axios from 'axios';
import toast from 'react-hot-toast'; import toast from 'react-hot-toast';
import api from '../services/api';
interface ContactSubmission { interface ContactSubmission {
id: string; id: string;
@@ -31,73 +31,56 @@ export default function ContactSubmissions() {
const [adminNotes, setAdminNotes] = useState(''); const [adminNotes, setAdminNotes] = useState('');
const [status, setStatus] = useState('PENDING'); const [status, setStatus] = useState('PENDING');
const API_URL = import.meta.env.VITE_API_URL || '';
const fetchSubmissionsData = async () => { const fetchSubmissionsData = async () => {
try { try {
const token = localStorage.getItem('adminToken'); const res = await api.get('/contact/submissions');
const res = await axios.get(`${API_URL}/api/contact/submissions`, {
headers: { Authorization: `Bearer ${token}` },
});
setSubmissions(res.data.items || []); setSubmissions(res.data.items || []);
} catch { } catch {
toast.error('خطا در دریافت پیام‌های تماس با ما'); // Handled by api interceptor
} }
}; };
useEffect(() => { useEffect(() => {
let isSubscribed = true; let isSubscribed = true;
const token = localStorage.getItem('adminToken'); setLoading(true);
axios.get(`${API_URL}/api/contact/submissions`, { api.get('/contact/submissions').then(res => {
headers: { Authorization: `Bearer ${token}` },
}).then(res => {
if (isSubscribed) setSubmissions(res.data.items || []); if (isSubscribed) setSubmissions(res.data.items || []);
}).catch(() => { }).catch(() => {
toast.error('خطا در دریافت پیام‌های تماس با ما'); // Handled by api interceptor
}).finally(() => { }).finally(() => {
if (isSubscribed) setLoading(false); if (isSubscribed) setLoading(false);
}); });
axios.get(`${API_URL}/api/contact/info`).then(res => { api.get('/contact/info').then(res => {
if (isSubscribed) setInfoItems(res.data || []); if (isSubscribed) setInfoItems(res.data || []);
}).catch(() => { }).catch(() => {
toast.error('خطا در دریافت اطلاعات نمایندگی'); // Handled by api interceptor
}); });
return () => { return () => {
isSubscribed = false; isSubscribed = false;
}; };
}, [API_URL]); }, []);
const handleUpdateStatus = async () => { const handleUpdateStatus = async () => {
if (!selectedSub) return; if (!selectedSub) return;
try { try {
const token = localStorage.getItem('adminToken'); await api.put(`/contact/submissions/${selectedSub.id}`, { status, adminNotes });
await axios.put(
`${API_URL}/api/contact/submissions/${selectedSub.id}`,
{ status, adminNotes },
{ headers: { Authorization: `Bearer ${token}` } },
);
toast.success('وضعیت پیام با موفقیت به‌روزرسانی شد'); toast.success('وضعیت پیام با موفقیت به‌روزرسانی شد');
setSelectedSub(null); setSelectedSub(null);
fetchSubmissionsData(); fetchSubmissionsData();
} catch { } catch {
toast.error('خطا در ثبت تغییرات'); // Handled by api interceptor
} }
}; };
const handleSaveContactInfo = async () => { const handleSaveContactInfo = async () => {
try { try {
const token = localStorage.getItem('adminToken'); await api.put('/contact/info', { items: infoItems });
await axios.put(
`${API_URL}/api/contact/info`,
{ items: infoItems },
{ headers: { Authorization: `Bearer ${token}` } },
);
toast.success('اطلاعات نمایندگی و دفتر مرکزی با موفقیت ذخیره شد'); toast.success('اطلاعات نمایندگی و دفتر مرکزی با موفقیت ذخیره شد');
} catch { } catch {
toast.error('خطا در بروزرسانی اطلاعات نمایندگی'); // Handled by api interceptor
} }
}; };
@@ -9,6 +9,7 @@ import { usePathname, useRouter } from 'next/navigation';
import CartDrawer from "../components/CartDrawer"; import CartDrawer from "../components/CartDrawer";
import LoginModal from "../components/LoginModal"; import LoginModal from "../components/LoginModal";
import AuthModal from "../components/AuthModal";
import B2BPortal from "../components/B2BPortal"; import B2BPortal from "../components/B2BPortal";
import { useUserStore } from "../lib/store/userStore"; import { useUserStore } from "../lib/store/userStore";
import { useSettingsStore } from "../lib/store/settingsStore"; import { useSettingsStore } from "../lib/store/settingsStore";
@@ -106,6 +107,10 @@ export default function ClientLayout({ children }: { children: React.ReactNode }
petName={advisorData?.name as string | undefined} petName={advisorData?.name as string | undefined}
isAdvisorContext={!!advisorData} isAdvisorContext={!!advisorData}
/> />
<AuthModal
isOpen={useUserStore(state => state.isAuthModalOpen)}
onClose={() => useUserStore.getState().setAuthModalOpen(false)}
/>
<Toaster position="top-center" expand={true} richColors closeButton /> <Toaster position="top-center" expand={true} richColors closeButton />
</> </>
); );
+11 -2
View File
@@ -46,8 +46,17 @@ api.interceptors.response.use(
const status = error.response?.status; const status = error.response?.status;
const farsiMessage = responseData?.message || 'خطایی در ارتباط با سرور رخ داده است.'; const farsiMessage = responseData?.message || 'خطایی در ارتباط با سرور رخ داده است.';
// Handle 401 Unauthorized globally // Handle 401 Unauthorized globally - trigger Auth Modal
if (status === 401 || status === 403) { if (status === 401) {
localStorage.removeItem('accessToken');
localStorage.removeItem('refreshToken');
try {
useUserStore.getState().logout();
useUserStore.getState().setAuthModalOpen(true);
} catch {
// Ignore if store not ready
}
} else if (status === 403) {
localStorage.removeItem('accessToken'); localStorage.removeItem('accessToken');
localStorage.removeItem('refreshToken'); localStorage.removeItem('refreshToken');
try { try {
@@ -44,9 +44,11 @@ interface UserProfile {
interface UserStore { interface UserStore {
role: UserRole; role: UserRole;
isLoggedIn: boolean; isLoggedIn: boolean;
isAuthModalOpen: boolean;
profile: UserProfile; profile: UserProfile;
setRole: (role: UserRole) => void; setRole: (role: UserRole) => void;
setLoggedIn: (isLoggedIn: boolean) => void; setLoggedIn: (isLoggedIn: boolean) => void;
setAuthModalOpen: (open: boolean) => void;
updateProfile: (profile: Partial<UserProfile>) => Promise<void>; updateProfile: (profile: Partial<UserProfile>) => Promise<void>;
addAddress: (address: Address) => Promise<void>; addAddress: (address: Address) => Promise<void>;
updateAddress: (id: string, address: Address) => Promise<void>; updateAddress: (id: string, address: Address) => Promise<void>;
@@ -62,6 +64,7 @@ export const useUserStore = create<UserStore>()(
(set) => ({ (set) => ({
role: "User_Guest", role: "User_Guest",
isLoggedIn: false, isLoggedIn: false,
isAuthModalOpen: false,
profile: { profile: {
firstName: "", firstName: "",
lastName: "", lastName: "",
@@ -74,6 +77,7 @@ export const useUserStore = create<UserStore>()(
}, },
setRole: (role) => set({ role }), setRole: (role) => set({ role }),
setLoggedIn: (isLoggedIn) => set({ isLoggedIn }), setLoggedIn: (isLoggedIn) => set({ isLoggedIn }),
setAuthModalOpen: (open) => set({ isAuthModalOpen: open }),
updateProfile: async (updates) => { updateProfile: async (updates) => {
try { try {
const profileData = await authService.updateProfile(updates); const profileData = await authService.updateProfile(updates);