import gql from '../api';
import { store } from '../store';
import { actionPromise } from '.';

const actionOrder = () =>
  async (dispatch, getState) => {
    const { cart } = store.getState();
    const orderGoods = Object.entries(cart).map(([_id, { good, count }]) => ({ good: { _id }, count }));

    await dispatch(actionPromise('order', gql(`
                  mutation newOrder($order:OrderInput){
                    OrderUpsert(order:$order)
                      { _id total }
                  }
          `, { order: { orderGoods } })));
  };

export default actionOrder;
