import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { fetchStoke } from './stock';
import { token, LoginSliceState, Status } from './types';

const initialState: LoginSliceState = {
  items: [],
  status: Status.LOADING, // loading | success | error
};

const loginSlice = createSlice({
  name: 'login',
  initialState,
  reducers: {
    setItems(state, action: PayloadAction<Login[]>) {
      state.items = action.payload;
    },
  },
  extraReducers: (builder) => {
    builder.addCase(fetchStoke.pending, (state, action) => {
      state.status = Status.LOADING;
      state.items = [];
    });

    builder.addCase(fetchStoke.fulfilled, (state, action) => {
      state.items = action.payload;
      state.status = Status.SUCCESS;
    });

    builder.addCase(fetchStoke.rejected, (state, action) => {
      state.status = Status.ERROR;
      state.items = [];
    });
  },
});

export const { setItems } = loginSlice.actions;

export default loginSlice.reducer;
