61 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			61 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import AcceptModal, {AcceptModalProps} from "../../components/Modals/AcceptModal/AcceptModal";
 | 
						|
import assemblyApi, {AssemblyCreationStatusCode, CreateAssemblyResponse} from "../../api/assemblyApi";
 | 
						|
import Toast from "react-native-toast-message";
 | 
						|
import {openCancelAssemblyModal} from "../../features/cancelAssemblyModal/cancelAssemblyModalSlice";
 | 
						|
import React from "react";
 | 
						|
import {RootState, useAppDispatch} from "../../redux/store";
 | 
						|
import {closeAcceptModal} from "../../features/assembly/assemblySlice";
 | 
						|
import {useSelector} from "react-redux";
 | 
						|
 | 
						|
type RestProps = {
 | 
						|
    onCreated: (response: CreateAssemblyResponse) => void;
 | 
						|
}
 | 
						|
type Props = RestProps;
 | 
						|
 | 
						|
const CreateAssemblyModal = (props: Props) => {
 | 
						|
    const {onCreated} = props
 | 
						|
    const {order, acceptModalVisible} = useSelector((state: RootState) => state.assembly);
 | 
						|
    const dispatch = useAppDispatch();
 | 
						|
 | 
						|
    const showCreateStatusToast = (ok: boolean, message: string) => {
 | 
						|
        Toast.show({
 | 
						|
            type: ok ? 'success' : 'error',
 | 
						|
            text1: 'Создание сборки',
 | 
						|
            text2: message
 | 
						|
        });
 | 
						|
    }
 | 
						|
 | 
						|
    const onError = (response: CreateAssemblyResponse) => {
 | 
						|
        const {statusCode, assemblyId, userName} = response;
 | 
						|
        if (statusCode !== AssemblyCreationStatusCode.ASSEMBLY_ALREADY_EXISTS) return;
 | 
						|
        const message =
 | 
						|
            `Заказ собирает ${userName}. Отменить и начать сборку на ваш аккаунт?\n\n` +
 | 
						|
            'Удостоверьтесь, что текущая сборка ошибочна и никто другой её не выполняет.';
 | 
						|
 | 
						|
        dispatch(openCancelAssemblyModal({assemblyId: assemblyId, message: message}));
 | 
						|
 | 
						|
    }
 | 
						|
 | 
						|
    const onAccepted = async () => {
 | 
						|
        if (!order) return
 | 
						|
        const response = await assemblyApi.create(order.databaseId);
 | 
						|
        showCreateStatusToast(response.ok, response.message);
 | 
						|
        const handler = response.ok ? onCreated : onError;
 | 
						|
        handler(response);
 | 
						|
        dispatch(closeAcceptModal());
 | 
						|
    }
 | 
						|
 | 
						|
    const onRefused = async () => {
 | 
						|
        dispatch(closeAcceptModal())
 | 
						|
    }
 | 
						|
 | 
						|
    return (
 | 
						|
        <AcceptModal
 | 
						|
            visible={acceptModalVisible}
 | 
						|
            text={`Вы уверены что хотите начать сборку заказа ${order?.orderNumber}`}
 | 
						|
            onAccepted={onAccepted}
 | 
						|
            onRefused={onRefused}/>
 | 
						|
    )
 | 
						|
}
 | 
						|
 | 
						|
export default CreateAssemblyModal; |