ebanutsya

This commit is contained in:
2023-10-30 09:16:25 +03:00
parent cd89a70b17
commit 2a4479faac
13 changed files with 393 additions and 80 deletions

View File

@@ -1,4 +1,4 @@
import {View, Text, Image, StyleSheet, Button} from "react-native";
import {View, Text, Image, StyleSheet, Button, TouchableOpacity} from "react-native";
import {responsiveHeight, responsiveWidth} from "react-native-responsive-dimensions";
import DText from "../../components/DText/DText";
import {RFPercentage} from "react-native-responsive-fontsize";
@@ -16,14 +16,29 @@ import {useNavigation} from "@react-navigation/native";
import printingApi from "../../api/printingApi";
import PrintingService from "../../utils/PrintingService";
import OrderProductsList from "../../components/OrderCard/OrderProductsList";
import {setAssembled, setOrder} from "../../features/assembly/assemblySlice";
import {
confirmAssembly, endAssembly,
setAssembled,
setAssembly,
setOrder,
startAssembly
} from "../../features/assembly/assemblySlice";
import AcceptModal from "../../components/Modals/AcceptModal/AcceptModal";
import {Order} from "../../types/order";
import acceptModal from "../../components/Modals/AcceptModal/AcceptModal";
import printingService from "../../utils/PrintingService";
import ReprintModal from "../../components/Modals/ReprintModal/ReprintModal";
import {setData, setPrinterName} from "../../features/printing/printingSlice";
import {setPrinterName} from "../../features/printing/printingSlice";
import {openReprintModal} from "../../features/reprintModal/reprintModalSlice";
import {ASSEMBLY_STATE} from "../../types/assembly";
import {RenderTargetOptions} from "@shopify/flash-list";
import {createOnShouldStartLoadWithRequest} from "react-native-webview/lib/WebViewShared";
import assemblyApi from "../../api/assemblyApi";
import Toast from "react-native-toast-message";
import mainScreen from "../MainScreen/MainScreen";
import toast from "../../components/Toast/Toast";
import {openImageZoomModal, setImages} from "../../features/imageZoomModal/loadingModalSlice";
type AssemblyPeriod = {
started: Date;
@@ -65,39 +80,131 @@ type OrderScreenProps = {
const OrderScreen: FC<OrderScreenProps> = ({order}) => {
const navigator = useNavigation();
const dispatch = useDispatch();
const assembly = useSelector((state: RootState) => state.assembly.assembly);
const assemblyState = useSelector((state: RootState) => state.assembly.localState);
const [selectedProduct, setSelectedProduct] = useState(order.products[0]);
const [assemblyPeriod, setAssemblyPeriod] = useState<AssemblyPeriod>({started: new Date(), ended: new Date()})
const [acceptModalVisible, setAcceptModalVisible] = useState(false);
const [reprintModalVisible, setReprintModalVisible] = useState(false);
const [skipConfirmButtonVisible, setSkipConfirmButtonVisible] = useState(false);
const _confirmAssembly = async () => {
if (!assembly) return;
dispatch(setLoadingText('Подтверждение сборки...'))
dispatch(openLoadingModal());
assemblyApi.confirm(assembly.databaseId).then(({ok, message}) => {
dispatch(closeLoadingModal());
Toast.show({
type: ok ? 'success' : 'error',
text1: 'Подтверждение сборки',
text2: message
})
if (ok) {
dispatch(confirmAssembly());
const [assemblyStarted, setAssemblyStarted] = useState(false);
const [assemblyIntervalId, setAssemblyIntervalId] = useState(0);
const prettyPrintMilliseconds = (milliseconds: number) => {
const totalSeconds = Math.floor(milliseconds / 1000);
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
// Форматируем минуты и секунды, чтобы они всегда были двузначными
const formattedMinutes = String(minutes).padStart(2, '0');
const formattedSeconds = String(seconds).padStart(2, '0');
return `${formattedMinutes}:${formattedSeconds}`;
} else {
setSkipConfirmButtonVisible(true);
}
})
}
const startAssembly = async () => {
setAssemblyStarted(true);
let intervalId = setInterval(() => {
setAssemblyPeriod(oldValue => ({...oldValue, ended: new Date()}))
}, 1000);
setAssemblyIntervalId(Number(intervalId))
};
const getButtons = () => {
switch (assemblyState) {
case ASSEMBLY_STATE.NOT_STARTED:
return (<BasicButton containerStyle={styles.buttonContainer}
onPress={() => setAcceptModalVisible(true)}
label={"Начать сборку"}/>)
case ASSEMBLY_STATE.ASSEMBLING_PRODUCTS:
return (<BasicButton
containerStyle={styles.buttonContainer}
label={"Отметить как собранный"}
disabled={selectedProduct.assembled}
onPress={() => {
dispatch(setAssembled({orderProductId: selectedProduct.databaseId}))
}}
/>)
case ASSEMBLY_STATE.ALL_PRODUCTS_ASSEMBLED:
return (
<>
<BasicButton
onPress={() => {
_confirmAssembly();
}}
containerStyle={styles.buttonContainer}
label={"Подтвердить сборку"}
/>
{skipConfirmButtonVisible && <BasicButton
onPress={() => {
dispatch(confirmAssembly());
}}
style={{backgroundColor: 'orange'}}
containerStyle={styles.buttonContainer}
label={"Пропустить подтверждение сборки"}
/>}
</>
)
case ASSEMBLY_STATE.CONFIRMED:
return (
<>
<BasicButton
onPress={() => printLabel()}
containerStyle={styles.buttonContainer}
label={"Печать этикетки"}
/>
<BasicButton
containerStyle={styles.buttonContainer}
style={{backgroundColor: 'green'}}
label={"Завершить сборку"}
onPress={() => {
if (!assembly) return;
assemblyApi.close(assembly.databaseId).then(({ok, message}) => {
Toast.show({
type: ok ? 'success' : 'error',
text1: 'Завершение сборки',
text2: message
});
dispatch(endAssembly());
})
}}/>
</>
)
case ASSEMBLY_STATE.ENDED:
return (
<BasicButton
containerStyle={styles.buttonContainer}
label={"Этот заказ уже собран"}
disabled
onPress={() => {
}}/>
)
}
}
const printLabel = () => {
if (!order) return;
dispatch(setLoadingText('Идет печать этикетки...'))
dispatch(openLoadingModal())
printingApi.getLabel(order.databaseId).then(pdfData => {
printingService.getInstance().printPdf('ozon', pdfData).then((response) => {
dispatch(closeLoadingModal());
if (response) return;
dispatch(setPrinterName({printerName: 'ozon'}));
dispatch(openReprintModal());
});
})
}
useEffect(() => {
let newSelectedProduct = order.products.find(o => o.databaseId == selectedProduct.databaseId);
if (!newSelectedProduct) return;
setSelectedProduct(newSelectedProduct);
}, [order]);
useEffect(() => {
dispatch(closeLoadingModal())
}, []);
if (!assembly) return;
assemblyApi.updateState(assembly.databaseId, Number(assemblyState)).then(ok => {
if (ok) return;
Toast.show({
type: 'error',
text1: 'Обновление состояния',
text2: 'Неудалось обновить состояние текущей сборки на сервере'
})
});
}, [assemblyState]);
return (
<View style={styles.container}>
<View style={styles.productsContainer}>
@@ -107,9 +214,16 @@ const OrderScreen: FC<OrderScreenProps> = ({order}) => {
setSelectedProduct(product)
}}/>
</View>
<View style={styles.imageWrapper}>
<Image style={styles.image} source={{uri: selectedProduct.imageUrl}}/>
</View>
<TouchableOpacity style={styles.imageWrapper} onPress={()=>{
dispatch(setImages([selectedProduct.imageUrl]));
dispatch(openImageZoomModal());
}}>
<View style={{flex: 1}}>
<Image style={styles.image} source={{uri: selectedProduct.imageUrl}}/>
</View>
</TouchableOpacity>
</View>
<View style={styles.contentContainer}>
<View style={styles.dataContainer}>
@@ -124,49 +238,29 @@ const OrderScreen: FC<OrderScreenProps> = ({order}) => {
<DText>Номер товара: {0}</DText>
<DText>{}</DText>
<DTitle style={styles.contentTitle}>Сборка</DTitle>
<DText>Затрачено
времени: {prettyPrintMilliseconds(assemblyPeriod.ended.getTime() - assemblyPeriod.started.getTime())}</DText>
</View>
<View style={styles.buttonsContainer}>
{assemblyStarted ? <>
<BasicButton
containerStyle={styles.buttonContainer}
label={"Отметить как собранный"}
disabled={selectedProduct.assembled}
onPress={() => {
dispatch(setAssembled({orderProductId: selectedProduct.databaseId}))
}}
/>
<BasicButton
onPress={() => {
if (!order) return;
dispatch(setLoadingText('Идет печать этикетки...'))
dispatch(openLoadingModal())
printingApi.getLabel(order.databaseId).then(pdfData => {
printingService.getInstance().printPdf('ozon', pdfData).then((response) => {
dispatch(closeLoadingModal());
if (response) return;
dispatch(setPrinterName({printerName: 'ozon'}));
dispatch(openReprintModal());
});
})
}}
containerStyle={styles.buttonContainer}
label={"Печать этикетки"}
disabled={Boolean(order.products.find(product => !product.assembled))}/>
</>
: <BasicButton containerStyle={styles.buttonContainer}
onPress={() => setAcceptModalVisible(true)}
label={"Начать сборку"}/>}
{getButtons()}
</View>
</View>
<AcceptModal visible={acceptModalVisible}
text={`Вы уверены что хотите начать сборку заказа ${order.orderNumber}`}
onAccepted={() => {
setAcceptModalVisible(false);
startAssembly();
assemblyApi.create(order.databaseId).then(creationResult => {
console.log(creationResult.message)
Toast.show({
type: creationResult.ok ? 'success' : 'error',
text1: 'Создание сборки',
text2: creationResult.message
});
if (!creationResult.ok) return;
assemblyApi.getActive().then(activeAssembly => {
dispatch(setAssembly(activeAssembly));
dispatch(startAssembly())
})
})
}}
onRefused={() => {
setAcceptModalVisible(false);