1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
   |  
 
      const orderPageBody = document.querySelector('body');
 
        orderPageBody.addEventListener("submit", event => {
 
            $('body').css('opacity', 0.2);  
            $('body').append('<div class="loadme-circular"></div>');    
 
            event.preventDefault();
 
            const url = '/index.php?fc=module&module=avrevent&controller=eventdispatcher&id_lang=1&ajax=true';
            const hub = new URL('https://mercure.auer-eshop.dev.avr/.well-known/mercure');
 
            let resultGatewayProcess = {
                'AskProductStockQuery': null,
                'VerifyCustomerSolvencyQuery': null,
                'GetGlobalOrderDiscountQuery': null,
                'GetDeliveryAmountQuery': null
            };
 
            function attributesNotNull(obj) {
                for (var key in obj) {
                    if (obj[key] === null)
                        return false;
                }
                return true;
            }
 
            /**
             * redirect or not if all event source process are executed or failed
             * @param {*} resultGatewayProcess
             * @param string queryName 
             */
            function processEventSourceResponse(resultGatewayProcess)
            {
                processFinish = attributesNotNull(resultGatewayProcess);
 
                if(processFinish)
                    $(this).unbind('submit').submit(); // here i want submit post data
            }
 
            async function sendQuery(data) {
                fetch(url
                    , {
                    method: 'POST',
                    body: JSON.stringify(data)
                })
                .then((response) => response.json())
                .then(function(response) { 
                    if(response.match('error'))
                        throw new Error(response);
 
                    let eventSource = new EventSource(`${hub}?topic=${response}`);
                    eventSource.onmessage = (event) => {
                        let res = JSON.parse(event.data);
                        resultGatewayProcess[data.query] = (res.state.includes('OK')) ? true : false;
                        processEventSourceResponse(resultGatewayProcess, data.query);
                    }
                })
                .catch(function(error) {
                    resultGatewayProcess[data.query] = false;
                    processEventSourceResponse(resultGatewayProcess, data.query);
 
                });
            }
 
            sendQuery({query: 'AskProductStockQuery'}); 
            sendQuery({query: 'VerifyCustomerSolvencyQuery'});
            sendQuery({query: 'GetGlobalOrderDiscountQuery'});
            sendQuery({query: 'GetDeliveryAmountQuery'});
 
            setTimeout(() => {
                $(this).unbind('submit').submit()  // here i want submit post data
            }, 1000);
        }) | 
Partager