import { createSlice } from "@reduxjs/toolkit";
import { addPoiThunk } from "../../thunks/database/addPoiThunk";

const initialState = {
  status: "idle", // idle | loading | succeeded | failed
  error: null,
};

const addPoiSlice = createSlice({
  name: "addPoi",
  initialState,
  reducers: {
    resetAddPoiStatus: (state) => {
      state.status = "idle";
      state.error = null;
    },
  },
  extraReducers: (builder) => {
    builder
      .addCase(addPoiThunk.pending, (state) => {
        state.status = "loading";
      })
      .addCase(addPoiThunk.fulfilled, (state) => {
        state.status = "succeeded";
        state.error = null;
      })
      .addCase(addPoiThunk.rejected, (state, action) => {
        state.status = "failed";
        state.error = action.payload;
      });
  },
});

export const { resetAddPoiStatus } = addPoiSlice.actions;
export default addPoiSlice.reducer;

export const selectAddPoiStatus = (state) => state.addPoi.status;
export const selectAddPoiError = (state) => state.addPoi.error;
