import React, { useEffect, useState, useRef } from 'react'; import { View, Pressable, StyleSheet, ScrollView, RefreshControl, Platform } from 'react-native'; import { useLanguage, OrderListGroups, useConfig } from 'ordering-components/native'; import { Placeholder, PlaceholderLine, Fade } from 'rn-placeholder'; import FontistoIcon from 'react-native-vector-icons/Fontisto' import FeatherIcon from 'react-native-vector-icons/Feather'; import SelectDropdown from 'react-native-select-dropdown' import { useTheme } from 'styled-components/native'; import { NotificationSetting } from '../../../../../src/components/NotificationSetting' import { FiltersTab, TabsContainer, Tag, IconWrapper, ModalContainer, ModalTitle, TabPressable, OrderStatus, SlaOption, SearchModalContent, SlaSettingModalContent, DeliveryStatusWrapper, VerticalLine, LeftSide, RightSide, Sides } from './styles'; import { OText, OButton, OModal, OIconButton, OInput, OIcon } from '../shared'; import { NotFoundSource } from '../NotFoundSource'; import { useOrderUtils, StatusBlock, Timer } from './utils' import { DeviceOrientationMethods } from '../../../../../src/hooks/DeviceOrientation' import { NewOrderNotification } from '../NewOrderNotification'; import { PreviousOrders } from '../PreviousOrders'; import { OrdersOptionParams } from '../../types'; import { OrdersOptionCity } from '../OrdersOptionCity'; import { OrdersOptionBusiness } from '../OrdersOptionBusiness'; import { OrdersOptionDelivery } from '../OrdersOptionDelivery'; import { OrdersOptionPaymethod } from '../OrdersOptionPaymethod'; import { OrdersOptionDriver } from '../OrdersOptionDriver'; import { OrdersOptionDate } from '../OrdersOptionDate'; import { OrderDetailsBusiness } from '../OrderDetails/Business'; import { WebsocketStatus } from '../WebsocketStatus' const { useDeviceOrientation, PORTRAIT } = DeviceOrientationMethods const OrdersListManagerUI = (props: OrdersOptionParams) => { const { setCurrentFilters, tabs, currentTabSelected, setCurrentTabSelected, ordersGroup, setOrdersGroup, orderStatus, loadOrders, loadMoreOrders, onNavigationRedirect, onFiltered, handleClickOrder, isBusinessApp, handleClickLogisticOrder, logisticOrders, loadLogisticOrders, isLogisticActivated } = props; const theme = useTheme(); const [, t] = useLanguage(); const [{ calculateDate, preorderTypeList, defaultOrderTypes, deliveryStatus, defaultSearchList }] = useOrderUtils() const [orientationState] = useDeviceOrientation(); const [openSearchModal, setOpenSearchModal] = useState(false) const [openSLASettingModal, setOpenSLASettingModal] = useState(false) const [slaSettingTime, setSlaSettingTime] = useState(6000) const [configState] = useConfig() const [currentDeliveryType, setCurrentDeliveryType] = useState('Delivery') const [search, setSearch] = useState(defaultSearchList) const [selectedTabStatus, setSelectedTabStatus] = useState([]) const HEIGHT_SCREEN = orientationState?.dimensions?.height const IS_PORTRAIT = orientationState.orientation === PORTRAIT const styles = StyleSheet.create({ header: { marginBottom: isBusinessApp ? 10 : 20, flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }, title: { fontFamily: 'Poppins', fontStyle: 'normal', fontWeight: '600', fontSize: 26, color: theme.colors.textGray, }, icons: { flexDirection: 'row', }, tab: { fontFamily: 'Poppins', fontStyle: 'normal', fontSize: 14, paddingBottom: 10, marginBottom: -1, zIndex: 100, borderColor: theme.colors.textGray, textTransform: 'capitalize' }, icon: { paddingBottom: 10, zIndex: 100, marginBottom: 5, }, tagsContainer: { marginBottom: 20, height: '100%' }, tag: { fontFamily: 'Poppins', fontStyle: 'normal', fontWeight: 'normal', fontSize: 14, }, pressable: { alignItems: 'center', }, loadButton: { borderRadius: 7.6, height: 44, marginRight: 10, marginBottom: 10, marginTop: 5, }, loadButtonText: { color: theme.colors.inputTextColor, fontFamily: 'Poppins', fontStyle: 'normal', fontWeight: 'normal', fontSize: 18, }, inputStyle: { width: '100%', borderWidth: 1, borderColor: '#DEE2E6', borderRadius: 7.6, marginBottom: 24 }, SLAwrapper: { flexDirection: 'row', marginBottom: 15 }, selectOption: { alignItems: 'center', justifyContent: 'space-between', minHeight: 40, width: '100%', paddingHorizontal: 15, backgroundColor: theme.colors.inputChat, borderRadius: 7.6, }, buttonTextStyle: { textAlign: 'left', marginHorizontal: 0, fontSize: 16, lineHeight: 24, color: '#748194' }, dropdownStyle: { borderWidth: 1, borderRadius: 8, paddingTop: 5, backgroundColor: '#fff', borderColor: theme.colors.lightGray, overflow: 'hidden', minHeight: 155 }, rowStyle: { display: 'flex', borderBottomWidth: 0, height: 36, alignItems: 'center', paddingHorizontal: 10 }, acceptButtonStyle: { borderRadius: 7.6, width: 130, height: 42, }, errorMessage: { marginBottom: 10, color: theme.colors.error, } }); const scrollRef = useRef() as React.MutableRefObject; const scrollListRef = useRef() as React.MutableRefObject; const scrollRefTab = useRef() as React.MutableRefObject; const [refreshing] = useState(false); const [currentOrderSelected, setCurrentOrderSelected] = useState(null) const [tagsState, setTags] = useState({ values: [] }) const tagsList = ordersGroup[currentTabSelected]?.defaultFilter ?? [] const currentOrdersGroup = ordersGroup[currentTabSelected] const isEqual = (array1: any, array2: any) => { return array1?.every((item: any) => array2.includes(item)) && array2?.every((item: any) => array1.includes(item)) } const getOrderStatus = (key: number) => { return orderStatus.find((status: any) => status?.key === key)?.text; }; const applyFilters = () => { setOrdersGroup({ ...ordersGroup, [currentTabSelected]: { ...ordersGroup[currentTabSelected], orders: [] } }) const dateRange = calculateDate(search.date.type, search.date.from, search.date.to) onFiltered && onFiltered({ ...search, date: { ...dateRange } }) setOpenSearchModal(false) } const handleTagSelected = (tag: any) => { const tags = tagsState?.values.includes(tag) ? tagsState?.values.filter((t: any) => t !== tag) : [...tagsState?.values, tag] setCurrentFilters(!tags.length ? tagsList : tags) setTags({ values: isEqual(tags, tagsList) ? [] : tags }) setOrdersGroup({ ...ordersGroup, [currentTabSelected]: { ...ordersGroup[currentTabSelected], currentFilter: !tags.length ? tagsList : tags } }) } const handleAllSelect = () => { setCurrentFilters(tagsList) setTags({ values: [] }) setOrdersGroup({ ...ordersGroup, [currentTabSelected]: { ...ordersGroup[currentTabSelected], currentFilter: tagsList } }) } const onClickSetting = () => { setOpenSLASettingModal(true) } const handleClose = () => { setOpenSearchModal(false) setOpenSLASettingModal(false) } useEffect(() => { setCurrentFilters(null) onFiltered && onFiltered(null) setSearch(defaultSearchList) scrollRefTab.current?.scrollTo({ animated: true }); scrollListRef.current?.scrollTo({ animated: true }); scrollRef.current?.scrollTo({ y: 0, animated: true }); }, [currentTabSelected]) useEffect(() => { setSelectedTabStatus(deliveryStatus) }, []) useEffect(() => { if (currentOrdersGroup?.orders?.length > 0 && !tagsList.includes(currentOrderSelected?.status)) { setCurrentOrderSelected(currentOrdersGroup?.orders[0]) } }, [currentOrdersGroup?.orders, currentTabSelected]) return ( <> {t('MY_ORDERS', 'My orders')} { currentTabSelected === 'logisticOrders' ? loadLogisticOrders && loadLogisticOrders() : loadOrders && loadOrders({ newFetch: true }) }} style={{ marginRight: 20 }} /> setOpenSearchModal(true)} /> {configState?.configs?.order_deadlines_enabled?.value === '1' && ( { onFiltered && onFiltered({ ...search, timeStatus: selectedItem?.key }) }} buttonTextAfterSelection={(selectedItem, index) => { return selectedItem.name }} rowTextForSelection={(item, index) => { return item.key }} buttonStyle={styles.selectOption} buttonTextStyle={styles.buttonTextStyle} renderDropdownIcon={isOpened => { return ; }} dropdownStyle={styles.dropdownStyle} dropdownOverlayColor='transparent' rowStyle={styles.rowStyle} renderCustomizedRowChild={(item, index) => { return ( {index !== 0 && } {item?.name} ); }} /> )} {(isLogisticActivated && !isBusinessApp) && ( setCurrentTabSelected('logisticOrders')}> )} {tabs.map((tab: any) => ( setCurrentTabSelected(tab?.title)} isSelected={tab.title === currentTabSelected ? 1 : 0} > {tab.text} ))} {currentTabSelected !== 'logisticOrders' && ( {tagsList && tagsList?.length > 1 && ( handleAllSelect()} isSelected={ isEqual(currentOrdersGroup.currentFilter, tagsList) ? theme.colors.primary : theme.colors.tabBar }> {t('All', 'All')} )} {tagsList && tagsList.map((key: number) => ( !currentOrdersGroup.loading && handleTagSelected(key)} isSelected={ currentOrdersGroup.currentFilter.includes(key) && !isEqual(currentOrdersGroup.currentFilter, tagsList) ? theme.colors.primary : theme.colors.tabBar }> {getOrderStatus(key)} { currentOrdersGroup.currentFilter.includes(key) && !isEqual(currentOrdersGroup.currentFilter, tagsList) && ' X' } ))} )} { currentTabSelected === 'logisticOrders' ? loadLogisticOrders && loadLogisticOrders() : loadOrders && loadOrders({ newFetch: true }) }} /> } > {!currentOrdersGroup?.error?.length && currentOrdersGroup?.orders?.length > 0 && currentTabSelected !== 'logisticOrders' && ( )} {!logisticOrders?.error?.length && logisticOrders && logisticOrders?.orders?.length > 0 && currentTabSelected === 'logisticOrders' && ( !order?.expired).map((order: any) => ({ ...order, isLogistic: true }))} onNavigationRedirect={onNavigationRedirect} getOrderStatus={getOrderStatus} handleClickLogisticOrder={handleClickLogisticOrder} isLogisticOrder handleClickEvent={setCurrentOrderSelected} currentOrdenSelected={currentOrderSelected?.id} /> ) } {((currentOrdersGroup?.loading || currentOrdersGroup?.pagination?.total === null) || (logisticOrders?.loading)) && ( <> {[...Array(8)].map((_, i) => ( ))} )} {!currentOrdersGroup?.error?.length && !currentOrdersGroup?.loading && currentOrdersGroup?.pagination?.totalPages && currentOrdersGroup?.pagination?.currentPage < currentOrdersGroup?.pagination?.totalPages && currentOrdersGroup?.orders?.length > 0 && ( loadMoreOrders && loadMoreOrders()} text={t('LOAD_MORE_ORDERS', 'Load more orders')} imgRightSrc={null} textStyle={styles.loadButtonText} style={styles.loadButton} bgColor={theme.colors.primary} borderColor={theme.colors.primary} /> )} {((!currentOrdersGroup?.loading && (currentOrdersGroup?.error?.length || currentOrdersGroup?.orders?.length === 0)) || (currentTabSelected === 'logisticOrders' && (logisticOrders && logisticOrders?.error?.length > 0 || logisticOrders?.orders?.length === 0 || !logisticOrders?.orders?.some(order => !order?.expired))) ) && ( 0 && !logisticOrders?.orders?.some(order => !order?.expired))))) ? t('NO_RESULTS_FOUND', 'Sorry, no results found') : currentOrdersGroup?.error?.[0]?.message || currentOrdersGroup?.error?.[0] || (currentTabSelected === 'logisticOrders' && logisticOrders?.error) || t('NETWORK_ERROR', 'Network Error') } image={theme.images.general.notFound} conditioned={false} /> )} {currentOrderSelected && ( )} {(openSearchModal || openSLASettingModal) && ( handleClose()} /> {openSearchModal && ( {t('SEARCH_ORDERS', 'Search orders')} setSearch({ ...search, id: value })} style={styles.inputStyle} placeholder={t('ORDER_NUMBER', 'Order number')} autoCorrect={false} /> {isBusinessApp && ( <> )} )} {openSLASettingModal && ( {t('SLA_SETTINGS', 'SLA’s Settings')} {defaultOrderTypes && defaultOrderTypes.map((tab: any) => ( setCurrentDeliveryType(tab?.name)} isSelected={tab.name.toUpperCase() === currentDeliveryType.toUpperCase() ? 1 : 0} > {tab.name} ))} {selectedTabStatus && selectedTabStatus.length > 0 && selectedTabStatus.map((item: any, i: any) => ( ))} )} )} ); }; export const OrdersListManager = (props: OrdersOptionParams) => { const [, t] = useLanguage(); const [checkNotificationStatus, setCheckNotificationStatus] = useState({ open: false, checked: false }) const ordersProps = { ...props, UIComponent: OrdersListManagerUI, useDefualtSessionManager: true, asDashboard: true, isIos: Platform.OS === 'ios', orderStatus: [ { key: 0, text: t('PENDING', 'Pending') }, { key: 1, text: t('COMPLETED', 'Completed') }, { key: 2, text: t('REJECTED', 'Rejected') }, { key: 3, text: t('DRIVER_IN_BUSINESS', 'Driver in business') }, { key: 4, text: t('READY_FOR_PICKUP', 'Ready for pickup') }, { key: 5, text: t('REJECTED_BY_BUSINESS', 'Rejected by business') }, { key: 6, text: t('REJECTED_BY_DRIVER', 'Rejected by Driver') }, { key: 7, text: t('ACCEPTED_BY_BUSINESS', 'Accepted by business') }, { key: 8, text: t('ACCEPTED_BY_DRIVER', 'Accepted by driver') }, { key: 9, text: t('PICK_UP_COMPLETED_BY_DRIVER', 'Pick up completed by driver'), }, { key: 10, text: t('PICK_UP_FAILED_BY_DRIVER', 'Pick up Failed by driver'), }, { key: 11, text: t('DELIVERY_COMPLETED_BY_DRIVER', 'Delivery completed by driver'), }, { key: 12, text: t('DELIVERY_FAILED_BY_DRIVER', 'Delivery Failed by driver'), }, { key: 13, text: t('PREORDER', 'Preorder') }, { key: 14, text: t('ORDER_NOT_READY', 'Order not ready') }, { key: 15, text: t( 'ORDER_PICKEDUP_COMPLETED_BY_CUSTOMER', 'Order picked up completed by customer', ), }, { key: 16, text: t('CANCELLED_BY_CUSTOMER', 'Cancelled by customer') }, { key: 17, text: t( 'ORDER_NOT_PICKEDUP_BY_CUSTOMER', 'Order not picked up by customer', ), }, { key: 18, text: t( 'DRIVER_ALMOST_ARRIVED_TO_BUSINESS', 'Driver almost arrived to business', ), }, { key: 19, text: t( 'DRIVER_ALMOST_ARRIVED_TO_CUSTOMER', 'Driver almost arrived to customer', ), }, { key: 20, text: t( 'ORDER_CUSTOMER_ALMOST_ARRIVED_BUSINESS', 'Customer almost arrived to business', ), }, { key: 21, text: t( 'ORDER_CUSTOMER_ARRIVED_BUSINESS', 'Customer arrived to business', ), }, { key: 22, text: t('ORDER_LOOKING_FOR_DRIVER', 'Looking for driver') }, { key: 23, text: t('ORDER_DRIVER_ON_WAY', 'Driver on way') }, { key: 24, text: t('ORDER_DRIVER_WAITING_FOR_ORDER', 'Driver waiting for order') }, { key: 25, text: t('ORDER_ACCEPTED_BY_DRIVER_COMPANY', 'Accepted by driver company') }, { key: 26, text: t('ORDER_DRIVER_ARRIVED_CUSTOMER', 'Driver arrived to customer') } ], tabs: [ { key: 0, text: t('PENDING', 'Pending'), tags: props?.orderGroupStatusCustom?.pending ?? [0, 13], title: 'pending' }, { key: 1, text: t('IN_PROGRESS', 'In Progress'), tags: props?.orderGroupStatusCustom?.inProgress ?? [3, 4, 7, 8, 9, 14, 18, 19, 20, 21, 22, 23, 24, 25, 26], title: 'inProgress', }, { key: 2, text: t('COMPLETED', 'Completed'), tags: props?.orderGroupStatusCustom?.completed ?? [1, 11, 15], title: 'completed', }, { key: 3, text: t('CANCELLED', 'Cancelled'), tags: props?.orderGroupStatusCustom?.cancelled ?? [2, 5, 6, 10, 12, 16, 17], title: 'cancelled', }, ] }; return (<> {props?.checkNotification && ( )} ); };