changes
This commit is contained in:
parent
575e122b02
commit
6d4972ce11
|
@ -8,7 +8,6 @@ import { useDispatch, useSelector } from "react-redux"
|
||||||
import { adminList } from "../../redux/slices/authSlice"
|
import { adminList } from "../../redux/slices/authSlice"
|
||||||
import { AppDispatch, RootState } from "../../redux/store/store" // Import RootState for selector
|
import { AppDispatch, RootState } from "../../redux/store/store" // Import RootState for selector
|
||||||
|
|
||||||
|
|
||||||
// Sample data for categories
|
// Sample data for categories
|
||||||
|
|
||||||
export default function AdminList() {
|
export default function AdminList() {
|
||||||
|
@ -19,21 +18,17 @@ export default function AdminList() {
|
||||||
const [deleteModal, setDeleteModal] = React.useState<boolean>(false)
|
const [deleteModal, setDeleteModal] = React.useState<boolean>(false)
|
||||||
const [rowData, setRowData] = React.useState<any | null>(null)
|
const [rowData, setRowData] = React.useState<any | null>(null)
|
||||||
|
|
||||||
const dispatch = useDispatch<AppDispatch>();
|
const dispatch = useDispatch<AppDispatch>()
|
||||||
|
|
||||||
// Fetching admin data from the Redux store
|
// Fetching admin data from the Redux store
|
||||||
const admins = useSelector((state: RootState) => state.auth.admins);
|
const admins = useSelector((state: RootState) => state.auth.admins)
|
||||||
|
|
||||||
|
|
||||||
console.log(admins, "woihfiuwhfownfownefoi")
|
|
||||||
|
|
||||||
// Dispatching the API call when the component mounts
|
// Dispatching the API call when the component mounts
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
dispatch(adminList());
|
dispatch(adminList())
|
||||||
}, [dispatch]);
|
}, [dispatch])
|
||||||
|
|
||||||
|
|
||||||
const handleClickOpen = () => {
|
const handleClickOpen = () => {
|
||||||
setModalOpen(true)
|
setModalOpen(true)
|
||||||
setEditRow(null)
|
setEditRow(null)
|
||||||
}
|
}
|
||||||
|
@ -55,11 +50,15 @@ export default function AdminList() {
|
||||||
]
|
]
|
||||||
|
|
||||||
// If no admins are available, display the sample data
|
// If no admins are available, display the sample data
|
||||||
const categoryRows = admins?.length ? admins?.map((admin: { name: any; role: any }, index: number) => ({
|
const categoryRows = admins?.length
|
||||||
srno: index + 1,
|
? admins?.map(
|
||||||
name: admin?.name,
|
(admin: { name: string; role: string }, index: number) => ({
|
||||||
role: admin.role,
|
srno: index + 1,
|
||||||
})) : []
|
name: admin?.name,
|
||||||
|
role: admin?.role || "N/A",
|
||||||
|
})
|
||||||
|
)
|
||||||
|
: []
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|
32
src/redux/slices/F
Normal file
32
src/redux/slices/F
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
|
||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
// Example: You might have an API call for deleting admin like this
|
||||||
|
export const deleteAdmin = createAsyncThunk(
|
||||||
|
'auth/deleteAdmin',
|
||||||
|
async (adminId: number, thunkAPI) => {
|
||||||
|
try {
|
||||||
|
const response = await axios.delete(`/api/admins/${adminId}`);
|
||||||
|
return adminId; // Return the ID of the deleted admin
|
||||||
|
} catch (error) {
|
||||||
|
return thunkAPI.rejectWithValue(error.response.data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const authSlice = createSlice({
|
||||||
|
name: 'auth',
|
||||||
|
initialState: {
|
||||||
|
admins: [], // Array of admins
|
||||||
|
},
|
||||||
|
reducers: {},
|
||||||
|
extraReducers: (builder) => {
|
||||||
|
builder.addCase(deleteAdmin.fulfilled, (state, action) => {
|
||||||
|
// Remove the deleted admin from the state
|
||||||
|
state.admins = state.admins.filter((admin) => admin.id !== action.payload);
|
||||||
|
});
|
||||||
|
// Handle rejected state if needed
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default authSlice.reducer;
|
|
@ -5,22 +5,27 @@ import { toast } from "react-toastify"
|
||||||
|
|
||||||
// Define types for state
|
// Define types for state
|
||||||
interface User {
|
interface User {
|
||||||
map(arg0: (admin: { name: any; role: any }, index: number) => { srno: number; name: any; role: any }): unknown
|
map(
|
||||||
|
arg0: (
|
||||||
|
admin: { name: string; role: string },
|
||||||
|
index: number
|
||||||
|
) => { srno: number; name: string; role: string }
|
||||||
|
): unknown
|
||||||
id: string
|
id: string
|
||||||
email: string
|
email: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Admin {
|
interface Admin {
|
||||||
id: string,
|
id: string
|
||||||
name: string,
|
name: string
|
||||||
role: string
|
role: string
|
||||||
}
|
}
|
||||||
interface AuthState {
|
interface AuthState {
|
||||||
user: User | null;
|
user: User | null
|
||||||
admins: Admin[];
|
admins: Admin[]
|
||||||
isAuthenticated: boolean;
|
isAuthenticated: boolean
|
||||||
isLoading: boolean;
|
isLoading: boolean
|
||||||
error: string | null;
|
error: string | null
|
||||||
}
|
}
|
||||||
|
|
||||||
// Async thunk for login
|
// Async thunk for login
|
||||||
|
@ -30,7 +35,10 @@ export const loginUser = createAsyncThunk<
|
||||||
{ rejectValue: string }
|
{ rejectValue: string }
|
||||||
>("auth/login", async ({ email, password }, { rejectWithValue }) => {
|
>("auth/login", async ({ email, password }, { rejectWithValue }) => {
|
||||||
try {
|
try {
|
||||||
const response = await backendHttp.post("admin/login", { email, password })
|
const response = await backendHttp.post("admin/login", {
|
||||||
|
email,
|
||||||
|
password,
|
||||||
|
})
|
||||||
localStorage.setItem("authToken", response.data?.data?.token) // Save token
|
localStorage.setItem("authToken", response.data?.data?.token) // Save token
|
||||||
toast.success(response.data?.message)
|
toast.success(response.data?.message)
|
||||||
return response.data
|
return response.data
|
||||||
|
@ -66,19 +74,21 @@ export const adminList = createAsyncThunk<
|
||||||
{ rejectValue: string }
|
{ rejectValue: string }
|
||||||
>("/auth", async (_, { rejectWithValue }) => {
|
>("/auth", async (_, { rejectWithValue }) => {
|
||||||
try {
|
try {
|
||||||
const response = await apiHttp.get("/auth");
|
const response = await apiHttp.get("/auth")
|
||||||
console.log(response)
|
console.log(response)
|
||||||
return response?.data?.data?.map((admin: { name: string; role: string }) => ({
|
return response?.data?.data?.map(
|
||||||
|
(admin: { name: string; role: string }) => ({
|
||||||
name: admin?.name,
|
name: admin?.name,
|
||||||
role: admin?.role || "N/A",
|
role: admin?.role,
|
||||||
}));
|
})
|
||||||
console.log(response.data.data)
|
)
|
||||||
|
// console.log(response.data.data)
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return rejectWithValue(error.response?.data?.message || "An error occurred");
|
return rejectWithValue(
|
||||||
|
error.response?.data?.message || "An error occurred"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
|
|
||||||
const initialState: AuthState = {
|
const initialState: AuthState = {
|
||||||
user: null,
|
user: null,
|
||||||
|
@ -86,7 +96,7 @@ const initialState: AuthState = {
|
||||||
isAuthenticated: false,
|
isAuthenticated: false,
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
error: null,
|
error: null,
|
||||||
};
|
}
|
||||||
|
|
||||||
const authSlice = createSlice({
|
const authSlice = createSlice({
|
||||||
name: "auth",
|
name: "auth",
|
||||||
|
@ -140,16 +150,18 @@ const authSlice = createSlice({
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// created by Jaanvi and Eknoor
|
// created by Jaanvi and Eknoor
|
||||||
.addCase(adminList.pending, (state) => {
|
.addCase(adminList.pending, (state) => {
|
||||||
state.isLoading = true
|
state.isLoading = true
|
||||||
state.error = null
|
state.error = null
|
||||||
})
|
})
|
||||||
.addCase(adminList.fulfilled, (state, action: PayloadAction<Admin[]>) => {
|
.addCase(
|
||||||
state.isLoading = false;
|
adminList.fulfilled,
|
||||||
state.admins = action.payload; // ✅ Store admins correctly
|
(state, action: PayloadAction<Admin[]>) => {
|
||||||
})
|
state.isLoading = false
|
||||||
|
state.admins = action.payload // ✅ Store admins correctly
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
.addCase(
|
.addCase(
|
||||||
adminList.rejected,
|
adminList.rejected,
|
||||||
|
|
Loading…
Reference in a new issue