import { createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";
import { LOADING_END, LOADING_START } from "../../../lib/redux/slices/appSlice";

export const provinceUpdateMobileMessagesActiveService = createAsyncThunk(
  "PROVINCE_UPDATE_MOBILE_MESSAGES_ACTIVE_SERVICE",
  async (d, { dispatch }) => {
    try {
      dispatch(LOADING_START());
      const { data, status } = await axios.put("moving-text-with-role/0/", d);
      dispatch(LOADING_END());
      return { data, status };
    } catch (e) {
      dispatch(LOADING_END());
      return { error: e.response.data.result };
    }
  }
);

export const provinceUpdateMobileMessagesActiveTextService = createAsyncThunk(
  "PROVINCE_UPDATE_MOBILE_MESSAGES_ACTIVE_TEXT_SERVICE",
  async (d, { dispatch }) => {
    try {
      dispatch(LOADING_START());
      const { data, status } = await axios.put("moving-text/0/", d);
      dispatch(LOADING_END());
      return { data, status };
    } catch (e) {
      dispatch(LOADING_END());
      return { error: e.response.data.result };
    }
  }
);

export const provinceSubmitNewMovingText = createAsyncThunk(
  "PROVINCE_UPDATE_SUBMIT_MOVING_TEXT",
  async (d, { dispatch }) => {
    try {
      dispatch(LOADING_START());
      const { data, status } = await axios.post("moving-text/", d);
      dispatch(LOADING_END());
      return { data, status };
    } catch (e) {
      dispatch(LOADING_END());
      return { error: e.response.data.result };
    }
  }
);

export const provinceDeleteMovingTextService = createAsyncThunk(
  "PROVINCE_DELETE_MOVING_TEXT",
  async (d, { dispatch }) => {
    dispatch(LOADING_START());
    const { data, status } = await axios.delete("moving-text/" + d + "/");
    dispatch(LOADING_END());
    return { data, status };
  }
);
