import { IData } from "../../models/IData";
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { createSession, connectSession } from "./ActionCreator";

export interface sessionState {
  data: IData | null;
  isLoading: boolean;
  error: any;
}

const initialState: sessionState = {
  data: null,
  isLoading: false,
  error: "",
};

export const sessionSlice = createSlice({
  name: "data",
  initialState,
  reducers: {},
  extraReducers: {
    [createSession.fulfilled.type]: (state, action: PayloadAction<IData>) => {
      state.isLoading = false;
      state.data = action.payload;
    },
    [createSession.rejected.type]: (state, action: PayloadAction<any>) => {
      state.isLoading = false;
      state.error = action.payload;
    },
    [createSession.pending.type]: (state) => {
      state.isLoading = true;
    },
    [connectSession.fulfilled.type]: (state, action: PayloadAction<IData>) => {
      state.isLoading = false;
      state.data = action.payload;
    },
    [connectSession.rejected.type]: (state, action: PayloadAction<any>) => {
      state.isLoading = false;
      state.error = action.payload;
    },
    [connectSession.pending.type]: (state) => {
      state.isLoading = true;
    },
  },
});

export default sessionSlice.reducer;
