Changeset 99c1e45 for chapterx-frontend/src/components/admin/UserTable.tsx
- Timestamp:
- 06/24/26 16:28:50 (11 days ago)
- Branches:
- main
- Children:
- a8f4a2d
- Parents:
- 0b502c2
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
chapterx-frontend/src/components/admin/UserTable.tsx
r0b502c2 r99c1e45 1 1 import React, { useState } from 'react' 2 2 import { Search, Shield, UserX, UserCheck } from 'lucide-react' 3 import axios from 'axios' 3 4 import { useAuthStore } from '../../store/authStore' 4 import { useNotificationStore } from '../../store/notificationStore'5 5 import { useUIStore } from '../../store/uiStore' 6 6 import { User, UserRole } from '../../types' … … 10 10 import { Modal } from '../ui/Modal' 11 11 12 const API = 'https://localhost:7125/api' 13 12 14 export const UserTable: React.FC = () => { 13 const { allUsers, updateUserRole, currentUser } = useAuthStore() 14 const { addNotification } = useNotificationStore() 15 const { allUsers, updateUserRole, currentUser, token } = useAuthStore() 15 16 const { addToast } = useUIStore() 16 17 const [search, setSearch] = useState('') 17 18 const [confirmUser, setConfirmUser] = useState<User | null>(null) 18 19 const [confirmAction, setConfirmAction] = useState<'promote' | 'demote' | null>(null) 20 const [loading, setLoading] = useState(false) 21 22 const authHeaders = token ? { Authorization: `Bearer ${token}` } : {} 19 23 20 24 const filtered = allUsers.filter( … … 25 29 ) 26 30 27 const handlePromote = (user: User) => { 28 const newRole: UserRole = user.role === 'regular' ? 'writer' : user.role === 'writer' ? 'admin' : 'admin' 29 updateUserRole(user.user_id, newRole) 30 addNotification({ 31 user_id: user.user_id, 32 type: 'system', 33 title: 'Role Updated', 34 message: `Your account has been promoted to ${newRole}.`, 35 }) 36 addToast(`${user.username} promoted to ${newRole}`) 37 setConfirmUser(null) 31 const handlePromote = async (user: User) => { 32 setLoading(true) 33 try { 34 await axios.post(`${API}/admins`, { userId: user.user_id }, { headers: authHeaders }) 35 updateUserRole(user.user_id, 'admin') 36 addToast(`${user.username} promoted to admin`) 37 } catch (err: any) { 38 addToast(err?.response?.data?.message || 'Failed to promote user.', 'error') 39 } finally { 40 setLoading(false) 41 setConfirmUser(null) 42 } 38 43 } 39 44 40 const handleDemote = (user: User) => { 41 const newRole: UserRole = user.role === 'admin' ? 'writer' : 'regular' 42 updateUserRole(user.user_id, newRole) 43 addToast(`${user.username} role changed to ${newRole}`, 'info') 44 setConfirmUser(null) 45 const handleDemote = async (user: User) => { 46 setLoading(true) 47 try { 48 await axios.delete(`${API}/admins/${user.user_id}`, { headers: authHeaders }) 49 updateUserRole(user.user_id, 'writer') 50 addToast(`${user.username} removed from admin`, 'info') 51 } catch (err: any) { 52 addToast(err?.response?.data?.message || 'Failed to demote user.', 'error') 53 } finally { 54 setLoading(false) 55 setConfirmUser(null) 56 } 45 57 } 46 58 … … 140 152 variant={confirmAction === 'promote' ? 'primary' : 'danger'} 141 153 className="flex-1" 154 loading={loading} 142 155 onClick={() => confirmAction === 'promote' ? handlePromote(confirmUser) : handleDemote(confirmUser)} 143 156 >
Note:
See TracChangeset
for help on using the changeset viewer.
