import { createSlice } from "@reduxjs/toolkit";
import {archiveOrders} from "../httpServices/httpServiceArchive";

const initialState = {
    archive:[],
    archiveLoading: true,
    archiveError: null,
    isAborted: false,
}

const archiveSlice = createSlice({
    name: "archive",
    initialState,
    reducers:{
        setArchiveLoadingTrue: (state) => {
            state.archiveLoading = true
        },
        resetArchive: (state) => {
            state.archive = []
            state.archiveLoading = true
            state.archiveError = null
        },
        setArchiveAbort: (state, action) => {
            state.isAborted = !!action.payload;
        }
    },
    extraReducers:(builder)=>{
        // Get Orders
        builder.addCase(archiveOrders.pending, (state) => {
            state.archiveLoading = true;
            state.error = null;
        })
        builder.addCase(archiveOrders.fulfilled, (state, action) => {
            state.archiveLoading = false;
            state.archive = action.payload;
        })
        builder.addCase(archiveOrders.rejected, (state, action) => {
            state.archiveLoading = false;
            state.error = action.payload;
        });
    }
})

export const { setArchiveLoadingTrue, resetArchive, setArchiveAbort } = archiveSlice.actions;
export default archiveSlice.reducer;