first commit
This commit is contained in:
215
lib/features/cors/corsSlice.ts
Normal file
215
lib/features/cors/corsSlice.ts
Normal file
@@ -0,0 +1,215 @@
|
||||
import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
|
||||
import { fetchClient } from "@/lib/api/fetchClient";
|
||||
|
||||
// Types
|
||||
export interface CorsEntry {
|
||||
id: string;
|
||||
origin: string;
|
||||
description?: string; // For whitelist
|
||||
reason?: string; // For blacklist
|
||||
is_active: boolean;
|
||||
created_by: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
interface CorsState {
|
||||
whitelist: CorsEntry[];
|
||||
blacklist: CorsEntry[];
|
||||
isLoading: boolean;
|
||||
error: string | null;
|
||||
}
|
||||
|
||||
const initialState: CorsState = {
|
||||
whitelist: [],
|
||||
blacklist: [],
|
||||
isLoading: false,
|
||||
error: null,
|
||||
};
|
||||
|
||||
// Async Thunks
|
||||
|
||||
// --- WHITELIST ---
|
||||
|
||||
export const fetchWhitelists = createAsyncThunk(
|
||||
"cors/fetchWhitelists",
|
||||
async (_, { rejectWithValue }) => {
|
||||
try {
|
||||
return await fetchClient("/settings/cors/whitelist");
|
||||
} catch (error: any) {
|
||||
return rejectWithValue(error.message);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
export const createWhitelist = createAsyncThunk(
|
||||
"cors/createWhitelist",
|
||||
async (data: { origin: string; description: string }, { rejectWithValue }) => {
|
||||
try {
|
||||
return await fetchClient("/settings/cors/whitelist", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
} catch (error: any) {
|
||||
return rejectWithValue(error.message);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
export const updateWhitelist = createAsyncThunk(
|
||||
"cors/updateWhitelist",
|
||||
async ({ id, data }: { id: string; data: Partial<CorsEntry> }, { rejectWithValue }) => {
|
||||
try {
|
||||
await fetchClient(`/settings/cors/whitelist/${id}`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return { id, data };
|
||||
} catch (error: any) {
|
||||
return rejectWithValue(error.message);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
export const deleteWhitelist = createAsyncThunk(
|
||||
"cors/deleteWhitelist",
|
||||
async (id: string, { rejectWithValue }) => {
|
||||
try {
|
||||
await fetchClient(`/settings/cors/whitelist/${id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
return id;
|
||||
} catch (error: any) {
|
||||
return rejectWithValue(error.message);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// --- BLACKLIST ---
|
||||
|
||||
export const fetchBlacklists = createAsyncThunk(
|
||||
"cors/fetchBlacklists",
|
||||
async (_, { rejectWithValue }) => {
|
||||
try {
|
||||
return await fetchClient("/settings/cors/blacklist");
|
||||
} catch (error: any) {
|
||||
return rejectWithValue(error.message);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
export const createBlacklist = createAsyncThunk(
|
||||
"cors/createBlacklist",
|
||||
async (data: { origin: string; reason: string }, { rejectWithValue }) => {
|
||||
try {
|
||||
return await fetchClient("/settings/cors/blacklist", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
} catch (error: any) {
|
||||
return rejectWithValue(error.message);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
export const updateBlacklist = createAsyncThunk(
|
||||
"cors/updateBlacklist",
|
||||
async ({ id, data }: { id: string; data: Partial<CorsEntry> }, { rejectWithValue }) => {
|
||||
try {
|
||||
await fetchClient(`/settings/cors/blacklist/${id}`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return { id, data };
|
||||
} catch (error: any) {
|
||||
return rejectWithValue(error.message);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
export const deleteBlacklist = createAsyncThunk(
|
||||
"cors/deleteBlacklist",
|
||||
async (id: string, { rejectWithValue }) => {
|
||||
try {
|
||||
await fetchClient(`/settings/cors/blacklist/${id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
return id;
|
||||
} catch (error: any) {
|
||||
return rejectWithValue(error.message);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
const corsSlice = createSlice({
|
||||
name: "cors",
|
||||
initialState,
|
||||
reducers: {},
|
||||
extraReducers: (builder) => {
|
||||
// Fetch Whitelists
|
||||
builder.addCase(fetchWhitelists.pending, (state) => {
|
||||
state.isLoading = true;
|
||||
state.error = null;
|
||||
});
|
||||
builder.addCase(fetchWhitelists.fulfilled, (state, action) => {
|
||||
state.isLoading = false;
|
||||
state.whitelist = action.payload as CorsEntry[];
|
||||
});
|
||||
builder.addCase(fetchWhitelists.rejected, (state, action) => {
|
||||
state.isLoading = false;
|
||||
state.error = action.payload as string;
|
||||
});
|
||||
|
||||
// Create Whitelist
|
||||
builder.addCase(createWhitelist.fulfilled, (state, action) => {
|
||||
state.whitelist.push(action.payload as CorsEntry);
|
||||
});
|
||||
|
||||
// Update Whitelist
|
||||
builder.addCase(updateWhitelist.fulfilled, (state, action) => {
|
||||
const index = state.whitelist.findIndex((item) => item.id === action.payload.id);
|
||||
if (index !== -1) {
|
||||
state.whitelist[index] = { ...state.whitelist[index], ...action.payload.data };
|
||||
}
|
||||
});
|
||||
|
||||
// Delete Whitelist
|
||||
builder.addCase(deleteWhitelist.fulfilled, (state, action) => {
|
||||
state.whitelist = state.whitelist.filter((item) => item.id !== action.payload);
|
||||
});
|
||||
|
||||
// Fetch Blacklists
|
||||
builder.addCase(fetchBlacklists.pending, (state) => {
|
||||
state.isLoading = true;
|
||||
state.error = null;
|
||||
});
|
||||
builder.addCase(fetchBlacklists.fulfilled, (state, action) => {
|
||||
state.isLoading = false;
|
||||
state.blacklist = action.payload as CorsEntry[];
|
||||
});
|
||||
builder.addCase(fetchBlacklists.rejected, (state, action) => {
|
||||
state.isLoading = false;
|
||||
state.error = action.payload as string;
|
||||
});
|
||||
|
||||
// Create Blacklist
|
||||
builder.addCase(createBlacklist.fulfilled, (state, action) => {
|
||||
state.blacklist.push(action.payload as CorsEntry);
|
||||
});
|
||||
|
||||
// Update Blacklist
|
||||
builder.addCase(updateBlacklist.fulfilled, (state, action) => {
|
||||
const index = state.blacklist.findIndex((item) => item.id === action.payload.id);
|
||||
if (index !== -1) {
|
||||
state.blacklist[index] = { ...state.blacklist[index], ...action.payload.data };
|
||||
}
|
||||
});
|
||||
|
||||
// Delete Blacklist
|
||||
builder.addCase(deleteBlacklist.fulfilled, (state, action) => {
|
||||
state.blacklist = state.blacklist.filter((item) => item.id !== action.payload);
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
export default corsSlice.reducer;
|
||||
Reference in New Issue
Block a user