import {FC} from "react";
import AcceptModal from "../AcceptModal/AcceptModal";
import {RootState} from "../../../redux/store";
import {useDispatch, useSelector} from "react-redux";
import assemblyApi from "../../../api/assemblyApi";
import Toast from "react-native-toast-message";
import {closeCancelAssemblyModal} from "../../../features/cancelAssemblyModal/cancelAssemblyModalSlice";

const CancelAssemblyModal: FC = () => {
    const state = useSelector((state: RootState) => state.cancelAssemblyModal);
    const dispatch = useDispatch();
    const onAccepted = () => {
        if (!state.assemblyId) return;
        assemblyApi.cancelById(state.assemblyId).then(({ok, message}) => {
            Toast.show({
                type: ok ? 'success' : 'error',
                text1: "Отмена сборки",
                text2: message
            })
            dispatch(closeCancelAssemblyModal());
        })
    }
    const onRefused = () => {
        dispatch(closeCancelAssemblyModal());
    }
    return (<AcceptModal visible={state.isVisible && state.assemblyId !== undefined} text={state.message}
                         onAccepted={onAccepted}
                         onRefused={onRefused}/>)
}

export default CancelAssemblyModal;