import React, { FC, useState } from 'react' import { Order } from '../model/models' import { Text, Link } from '@wordpress/ui' import { ServiceIcon } from '@mybring/iconsystem' import { Button, DropdownMenu, MenuGroup, MenuItem, Popover, Snackbar, Spinner, } from '@wordpress/components' import { useI18n } from '@wordpress/react-i18n' import { more, redo, undo, pages, external } from '@wordpress/icons' import apiFetch from '@wordpress/api-fetch' import OrderListDetails from './OrderListDetails' export declare interface BookedOrderListItemProps { order: Order } const BookedOrderListItem: FC = ({ order }) => { const [menuOpen, setMenuOpen] = useState(false) const [displayOrderDetails, setDisplayOrderDetails] = useState(false) const [labelFailure, setLabelFailure] = useState(false) const [labelInProgress, setLabelInProgress] = useState(false) const [waybillInProgress, setWaybillInProgress] = useState(false) const [returnLabelInProgress, setReturnLabelInProgress] = React.useState(false) const { __ } = useI18n() const getLabel = async (labelType: string) => { setLabelFailure(false) const newTab = window.open('', '_blank') const path = labelType === 'WAYBILL' ? `/posten-bring-checkout/waybills?orderIds=${order.order_id}` : `/posten-bring-checkout/labels?orderIds=${order.order_id}&labelType=${labelType}` const response = await apiFetch({ path: path, method: 'GET', headers: { Accept: 'application/pdf', }, parse: false, }) .catch(_ => { setLabelFailure(true) }) .finally(() => { setLabelInProgress(false) setReturnLabelInProgress(false) setWaybillInProgress(false) }) if (response?.ok) { const blob = await response.blob() const url = window.URL.createObjectURL( new Blob([blob], { type: 'application/pdf' }) ) if (newTab) { newTab.location.href = url } } } return (
  • <>
    {order.mybring_bookings[0].consignmentNumber}
    {labelFailure && ( setLabelFailure(false)} > {__( 'An error occurred while generating label. Please try again', 'posten-bring-checkout' )} )}
    {({ onToggle }) => ( <> ) : ( redo ) } iconPosition="right" onClick={() => { setLabelInProgress(true) getLabel('LABEL') onToggle() }} > {__('Print label', 'posten-bring-checkout')} {order.mybring_bookings[0].waybillUrl && ( ) : ( pages ) } iconPosition="right" onClick={() => { setWaybillInProgress(true) getLabel('WAYBILL') onToggle() }} > {__('Print waybill', 'posten-bring-checkout')} )} {order.mybring_bookings[0].returnLabelUrl && ( ) : ( undo ) } iconPosition="right" onClick={() => { setReturnLabelInProgress(true) getLabel('RETURN') onToggle() }} > {__('Print return label', 'posten-bring-checkout')} )} setMenuOpen(false)} > {__('Tracking', 'posten-bring-checkout')} )}
  • ) } export default BookedOrderListItem