text: import React, { createContext, useState, useEffect, useContext } from 'react';
  // import { withRouter } from 'react-router';
  import axios from 'axios';
  import { API_BASE_URL } from 'src/utils/API_URLS';
  import { AuthContext } from './AuthContext';
  import { UsersContext } from './UsersContext';
  
  const OrdersContext = createContext();
  
  const OrdersProvider = props => {
    const [ordersObj, setOrdersObj] = useState({
      allOrders: [],
      userOrders: []
    });
  
    const { user } = useContext(AuthContext);
    const { printers, editUserOnContext } = useContext(UsersContext);
  
    useEffect(() => {
      fetchOrders();
    }, []);
  
    const assignOrderToAPrinter = async (printerId, orderId) => {
      console.log('printerId', printerId);
      console.log('orderId', orderId);
  
      const res = await axios.post(
        `${API_BASE_URL}/orders/assignOrder/${printerId}/${orderId}`,
        {
          loggedUser: user
        }
      );
      console.log('res', res);
  
      // * Update Order on Context
      const newOrders = ordersObj.allOrders.map(order =>
        order._id === res.data.order._id ? res.data.order : order
      );
  
      setOrdersObj({
        ...ordersObj,
        allOrders: newOrders
      });
  
      // * Update Printer on Context
      editUserOnContext(res.data.printer);
    };
  
    const unassignOrder = async orderId => {
      const res = await axios.post(
        `${API_BASE_URL}/orders//unAssignOrder/${orderId}`,
        {
          loggedUser: user
        }
      );
      console.log('res', res);
  
      // * Update Order on Context
      const newOrders = ordersObj.allOrders.map(order =>
        order._id === res.data.order._id ? res.data.order : order
      );
  
      setOrdersObj({
        ...ordersObj,
        allOrders: newOrders
      });
    };
  
    const fetchOrders = () => {
      (async () => {
        const res = await axios.get(`${API_BASE_URL}/orders/getAllOrders`);
        console.log('res', res);
        setOrdersObj({
          ...ordersObj,
          allOrders: res.data.orders
        });
      })();
    };
  
    const deleteOrderById = id => {
      (async () => {
        const res = await axios.delete(`${API_BASE_URL}/orders/${id}`);
        console.log('res', res);
        const newOrders = ordersObj.allOrders.filter(order => order._id !== id);
        setOrdersObj({
          ...ordersObj,
          allOrders: newOrders
        });
      })();
    };
  
    return (
      <OrdersContext.Provider
        value={{
          ordersObj,
          assignOrderToAPrinter,
          unassignOrder,
          deleteOrderById
        }}
      >
        {props.children}
      </OrdersContext.Provider>
    );
  };
  
  export { OrdersProvider, OrdersContext };
  