import {createSlice} from "@reduxjs/toolkit";
import {
    getRdsSettings,
    insertRdsSettings,
} from "../httpServices/httpServiceSettings";

const initialState = {
    settings:{},
    loading: true,
    error: null,
}

const tableSlice = createSlice({
    name: "settings",
    initialState,
    reducers:{
        resetRdsSettings: (state) => {
            state.settings = {}
            state.loading = false
            state.error = null
        }
    },
    extraReducers:(builder)=>{
        // Add RDS Settings
        builder.addCase(insertRdsSettings.pending, (state, action) => {
            state.loading   = true;
            state.error     = null;
        });
        builder.addCase(insertRdsSettings.fulfilled, (state, action) => {
            state.loading   = false;
            state.settings  = action.payload;
        });
        builder.addCase(insertRdsSettings.rejected, (state,action) => {
            state.loading   = false;
            state.error     = action.payload;
        });

        // Get RDS Settings
        builder.addCase(getRdsSettings.pending, (state) => {
            state.loading   = true;
            state.error     = null;
        })
        builder.addCase(getRdsSettings.fulfilled, (state, action) => {
            state.loading   = false;
            state.settings  = action.payload;
        })
        builder.addCase(getRdsSettings.rejected, (state, action) => {
            state.loading   = false;
            state.error     = action.payload;
        });
    }
})

export const { resetRdsSettings } = tableSlice.actions;
export default tableSlice.reducer;