import { createSlice } from "@reduxjs/toolkit";
import {deleteLogs, getLogs, saveLog} from "../httpServices/httpServiceLog";

const initialState = {
    logs:[],
    loading: false,
    error: null
}

const logSlice = createSlice({
    name: "logs",
    initialState,
    reducers:{},
    extraReducers:(builder)=>{
        // Get Tables
        builder.addCase(getLogs.pending, (state) => {
            state.loading = true;
            state.error = null;
        })
        builder.addCase(getLogs.fulfilled, (state, action) => {
            state.loading = false;
            state.logs = action.payload;
        })
        builder.addCase(getLogs.rejected, (state, action) => {
            state.loading = false;
            state.error = action.payload;
        });

        // Save Tables
        builder.addCase(saveLog.pending, (state) => {
            state.loading = true;
            state.error = null;
        })
        builder.addCase(saveLog.fulfilled, (state, action) => {
            state.loading = false;
            state.logs = action.payload;
        })
        builder.addCase(saveLog.rejected, (state, action) => {
            state.loading = false;
            state.error = action.payload;
        });

        // Delete Tables
        builder.addCase(deleteLogs.pending, (state) => {
            state.loading = true;
            state.error = null;
        })
        builder.addCase(deleteLogs.fulfilled, (state, action) => {
            state.loading = false;
            state.logs = action.payload;
        })
        builder.addCase(deleteLogs.rejected, (state, action) => {
            state.loading = false;
            state.error = action.payload;
        });
    }
})

export default logSlice.reducer;