text: /* eslint-disable no-useless-escape */
  import cogoToast from 'cogo-toast';
  import {
  ADD_BUS_START,
  ADD_BUS_SUCCESS,
  ADD_BUS_ERROR
  } from './actionTypes';
  import AuthService from '../../../../../utils/AuthService';
  import API from '../../../../../utils/API';
  import history from '../../../../../utils/history';
  
  
  const apiStart = () =>({
      type: ADD_BUS_START,
  });
  
  const apiSuccess = (payload) =>({
      type: ADD_BUS_SUCCESS,
      payload
  });
  
  const apiError = (error) =>({
      type: ADD_BUS_ERROR,
      error
  });
  
  const showSuccessMessage = (message) => {
      cogoToast.success(message, { position: 'top-right' });
    }; 
  
  const showErrorMessage = (message) => {
      cogoToast.error(message, { position: 'top-right' });
    };
  
    export const addBus = (bus_plate_number,bus_type,capacity,is_active) => async (dispatch) =>{
      if (bus_plate_number !== '' && bus_type !== '' && capacity !== '') {
          dispatch(apiStart());
          try {
              const token = AuthService.getToken();
              const config = {
                headers: {
                    'Content-Type': 'application/json',
                    Authorization: `Bearer ${token}`,
                },
              };
              // const is_active = is_active === '' ? false : true;
  
              const jsonData = 
                  {
                      "bus_plate_number": `${bus_plate_number}`,
                      "bus_type": `${bus_type}`,
                      "capacity": `${capacity}`,
                      "is_active": true
                      
                  };
  
              const response = await API.post('company/buses/', jsonData, config);
              showSuccessMessage('Bus Created Successfully!');
              dispatch(apiSuccess(response));
              console.log(response)
              history.push('/viewBusList')
          } catch (error) {
              if(error.response.status === 400){
                  showErrorMessage(error.response.data.bus_plate_number);
                  dispatch(apiError(error.response));
              } else {
              console.log(error.response);
              showErrorMessage('Bus not added, try again!');
              dispatch(apiError(error.response));
              }
          }
          } else {
              showErrorMessage('All fields are required');
          }
    }