import { createSlice } from "@reduxjs/toolkit";
import { getOrders } from "../httpServices/httpServiceOrder";

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

const orderSlice = createSlice({
    name: "orders",
    initialState,
    reducers:{
        setOrderLoadingTrue: (state) => {
            state.orders = []
            state.loading = false
            state.error = null
        }
    },
    extraReducers:(builder)=>{
        // Get Orders
        builder.addCase(getOrders.pending, (state) => {
            state.loading = true;
            state.error = null;
        })
        builder.addCase(getOrders.fulfilled, (state, action) => {
            state.loading = false;
            state.orders = action.payload;
        })
        builder.addCase(getOrders.rejected, (state, action) => {
            state.loading = false;
            state.error = action.error.message;
        });
    }
})

export const { setOrderLoadingTrue } = orderSlice.actions;
export default orderSlice.reducer;