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