templates/front/default/ruleta.html.twig line 1

Open in your IDE?
  1. {% extends "front/_common/_base.html.twig" %}
  2. {% block linkcanonical %}
  3.      <link rel="canonical" href="{{ absolute_url(path('home')) }}">
  4. {% endblock %}
  5. {% block cssadicional %}{% endblock %}
  6. {% block classbody %}footer_none icono_ruleta_none{% endblock %}
  7. {% block content %}
  8.     <div class="ruleta_header">
  9.         <div class="ruleta_header_row ww_center">
  10.             {% if home.logo|image_path %}
  11.                 <a class="ruleta_header_logo" href="{{ path('home') }}">
  12.                     <img src="{{ asset(home.logo|image_path) }}" width="203" alt="{{ asset(home.logo|split_alt) }}">
  13.                 </a>
  14.             {% endif %}
  15.             {% if home.backBtn %}
  16.                 <div class="ruleta_header_back">
  17.                     <a class="btn btn_blue" href="{{ path('home') }}">{{ home.backBtn }}</a>
  18.                 </div>
  19.             {% endif %}
  20.         </div>
  21.     </div>
  22.     <section class="ruleta" id="roulette_app"></section>
  23.     <section class="ruleta_footer">
  24.         {% if awards %}
  25.             <div class="ruleta_footer_conatiner ww_center">
  26.                 {% if home.awardTitle %}
  27.                     <h2>{{ home.awardTitle }}</h2>
  28.                 {% endif %}
  29.                 <div class="ruleta_footer_wrap">
  30.                     {% for item in awards %}
  31.                         <div class="ruleta_footer_item">
  32.                             <figure><img src="{{ asset(item.image|image_path) }}" alt="{{ item.title }}"></figure>
  33.                             <h3>{{ item.title|nl2br }}</h3>
  34.                         </div>
  35.                     {% endfor %}
  36.                 </div>
  37.             </div>
  38.         {% endif %}
  39.         {% if home.awardDisclaimer %}
  40.             <div class="ruleta_footer_txt">
  41.                 <div class="ww_center">
  42.                     {{ home.awardDisclaimer|raw }}
  43.                 </div>
  44.             </div>
  45.         {% endif %}
  46.     </section>
  47. {% endblock %}
  48. {% block jsfinal %}
  49.     <script src="{{ asset('static/js/validationform/jquery.validationEngine-es.js') }}"></script>
  50.     <script src="{{ asset('static/js/validationform/staff.validationEngine.js') }}"></script>
  51.     <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
  52.     <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
  53.     <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
  54.     <script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/2.3.8/purify.min.js" integrity="sha512-M72KfQy4kPuLYC6CeTrN0eA17U1lXEMrr5qEJC/40CLdZGC3HpwPS0esQLqBHnxty2FIcuNdP9EqwSOCLEVJXQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  55.     <script src="https://unpkg.com/html-react-parser@latest/dist/html-react-parser.min.js"></script>
  56.     {# <script src="{{ asset('static/js/ruleta.js') }}"></script> #}
  57.     <script type="text/babel" src="{{ asset('static/react/roulette_info.js') }}"></script>
  58.     <script type="text/babel" src="{{ asset('static/react/roulette.js') }}"></script>
  59.     <script type="text/babel" src="{{ asset('static/react/roulette_login_register.js') }}"></script>
  60.     <script type="text/babel" src="{{ asset('static/react/roulette_messages.js') }}"></script>
  61.     <script type="text/babel">
  62.         const App = (props) => {
  63.             const [is_logged, setIsLogged] = React.useState(props.logged);
  64.             const [current_user, setCurrentUser] = React.useState(props.currentUser);
  65.             const [current_date, setCurrentDate] = React.useState(props.currentDate);
  66.             const [selected_award, setSelectedAward] = React.useState(null);
  67.             const [login_data, setLoginData] = React.useState({'document': null});
  68.             const [register_data, setRegisterData] = React.useState({
  69.                 'document' : null,
  70.                 'email' : null,
  71.                 'phone' : null,
  72.                 'name' : null,
  73.                 'address' : null,
  74.             });
  75.             const [register_errors, setRegisterErrors] = React.useState('');
  76.             React.useEffect(() => {
  77.                 // Anything in here is fired on component mount.
  78.                 $("form").validationEngine('attach', {
  79.                     promptPosition: "bottomLeft",
  80.                     autoHidePrompt: true,
  81.                     autoHideDelay: 30000,
  82.                     binded: false,
  83.                     scroll: false
  84.                 });
  85.                 $('.soloNumber').keypress(validateNumber);
  86.                 function validateNumber(event) {
  87.                     var key = window.event ? event.keyCode : event.which;
  88.                     if (event.keyCode === 8 || event.keyCode === 46) {
  89.                         return true;
  90.                     } else if (key < 48 || key > 57) {
  91.                         return false;
  92.                     } else {
  93.                         return true;
  94.                     }
  95.                 }
  96.                 return () => {
  97.                     // Anything in here is fired on component unmount.
  98.                     $("form").validationEngine('detach');
  99.                 }
  100.             }, []);
  101.             const onSpin = (event) => {
  102.                 const btn = event.target;
  103.                 if (!is_logged) {
  104.                     firePopup('.ruleta_dni');
  105.                     return;
  106.                 } else {
  107.                     const data = {
  108.                         'user_id': current_user.id
  109.                     };
  110.                     if (current_user.blocked < current_date) {
  111.                         $('.ruleta_capa').addClass('active');
  112.                         spinRoulette(360 * 20, 20);
  113.                         const response = doPostData(props.sdUrls.spin, data);
  114.                         response.then(resp => {
  115.                             console.log(resp, '<-- award')
  116.                             if (resp.success) {
  117.                                 let tmp_user = {...current_user};
  118.                                 tmp_user['blocked'] = resp.blocked;
  119.                                 setCurrentUser(tmp_user);
  120.                                 setSelectedAward(resp.award)
  121.                                 const factor = 360 / props.awardCount;
  122.                                 const deg_base = 360 * (props.awardCount - 1)
  123.                                 const degs = deg_base + (factor * resp.award.position)
  124.                                 console.log(factor, deg_base, resp.award.position, degs, '<-- del giro')
  125.                                 spinRoulette(degs, 2);
  126.                                 if (resp.award.try_again) {
  127.                                     setTimeout(function () {
  128.                                         fireMessage('.ruleta_mensaje_try_again');
  129.                                     }, 2000)
  130.                                 } else {
  131.                                     setTimeout(function () {
  132.                                         fireMessage('.ruleta_mensaje_felicidades');
  133.                                     }, 2000)
  134.                                 }
  135.                             } else {
  136.                                 spinRoulette(360, 1);
  137.                                 fireMessage('.ruleta_mensaje_alerta');
  138.                             }
  139.                             $('.ruleta_capa').removeClass('active');
  140.                         });
  141.                     } else {
  142.                         fireMessage('.ruleta_mensaje_alerta');
  143.                     }
  144.                 }
  145.             };
  146.             const spinRoulette = (degree, duration) => {
  147.                 $('.ruleta_image').css({
  148.                     'transform': 'rotate(-' + degree.toString() + 'deg)',
  149.                     'transition': 'all ' + duration.toString() + 's cubic-bezier(0.76, 0, 0.24, 1)'
  150.                 })
  151.             }
  152.             const onLoginFormChange = (event) => {
  153.                 let data = {'document': event.target.value};
  154.                 setLoginData(data)
  155.             }
  156.             const onRegisterFormChange = (event) => {
  157.                 let data = {
  158.                     ...register_data,
  159.                     document: login_data.document
  160.                 }
  161.                 data[event.target.name] = event.target.value;
  162.                 setRegisterData(data);
  163.             }
  164.             const onLogin = (event) => {
  165.                 event.preventDefault();
  166.                 const btn = event.target;
  167.                 btn.disabled = true;
  168.                 const form = event.target.closest('form')
  169.                 const valid = $(form).validationEngine('validate');
  170.                 if (!valid) {
  171.                     btn.disabled = false;
  172.                     return false;
  173.                 } else {
  174.                     console.log(login_data);
  175.                     const response = doPostData(props.sdUrls.login, login_data);
  176.                     response.then(resp => {
  177.                         console.log(resp, '<-- login')
  178.                         closePopup('.ruleta_dni');
  179.                         if (!resp.success) {
  180.                             firePopup('.ruleta_registro');
  181.                             btn.disabled = false;
  182.                         } else {
  183.                             setCurrentUser({
  184.                                 'name': resp.name,
  185.                                 'id': resp.id,
  186.                                 'blocked': resp.blocked,
  187.                                 'current_date': resp.current_date
  188.                             });
  189.                             setIsLogged(resp.success);
  190.                             setCurrentDate(resp.current_date);
  191.                             if (resp.blocked > current_date) {
  192.                                 fireMessage('.ruleta_mensaje_alerta');
  193.                             }
  194.                         }
  195.                     });
  196.                 }
  197.             }
  198.             const onRegister = (event) => {
  199.                 event.preventDefault();
  200.                 const btn = event.target;
  201.                 btn.disabled = true;
  202.                 const form = event.target.closest('form')
  203.                 const valid = $(form).validationEngine('validate');
  204.                 if (!valid) {
  205.                     btn.disabled = false;
  206.                     return false;
  207.                 } else {
  208.                     const response = doPostData(props.sdUrls.register, register_data);
  209.                     response.then(resp => {
  210.                         if (resp.success) {
  211.                             closePopup('.ruleta_registro');
  212.                             setCurrentUser({
  213.                                 'name': resp.name,
  214.                                 'id': resp.id,
  215.                                 'blocked': resp.blocked,
  216.                                 'current_date': resp.current_date
  217.                             });
  218.                             setIsLogged(resp.success);
  219.                             setCurrentDate(resp.current_date);
  220.                         } else {
  221.                             btn.disabled = false;
  222.                             setRegisterErrors(resp.message);
  223.                         }
  224.                     })
  225.                 }
  226.             }
  227.             const onClosePopup = (event) => {
  228.                 event.target.closest('.sd_popup').classList.remove('active')
  229.             }
  230.             const firePopup = (htmlClass) => {
  231.                 document.querySelector(htmlClass).classList.add('active');
  232.             }
  233.             const closePopup = (htmlClass) => {
  234.                 document.querySelector(htmlClass).classList.remove('active');
  235.             }
  236.             const fireMessage = (htmlClass) => {
  237.                 document.querySelector(htmlClass).classList.remove('active');
  238.             }
  239.             const closeMessage = (htmlClass) => {
  240.                 document.querySelector(htmlClass).classList.add('active');
  241.             }
  242.             const onCloseMessage = (event) => {
  243.                 event.target.closest('.ruleta_mensaje').classList.add('active')
  244.             }
  245.             function doPostData(url, form_data) {
  246.                 let payload = new FormData();
  247.                 for (let key in form_data) {
  248.                     payload.append(key, form_data[key]);
  249.                 }
  250.                 return fetch(url, {
  251.                     body: payload,
  252.                     method: "post"
  253.                     //headers: {
  254.                     //    'Accept': 'application/json',
  255.                     //    'Content-Type': 'application/json',
  256.                     //}
  257.                 })
  258.                         .then(response => response.json())
  259.                         .then(res => {
  260.                             return res;
  261.                         })
  262.                         .catch(error => console.error('error fetching', error))
  263.             }
  264.             const renderLogin = () => {
  265.                 if (is_logged) {
  266.                     return null;
  267.                 }
  268.                 return <RouletteLogin title={props.login} onClose={onClosePopup} onSubmit={onLogin}
  269.                                       onFormChange={onLoginFormChange}/>
  270.             }
  271.             const renderRegister = () => {
  272.                 if (is_logged) {
  273.                     return null;
  274.                 }
  275.                 return <RouletteRegister title={props.register} onClose={onClosePopup} onSubmit={onRegister}
  276.                                          onFormChange={onRegisterFormChange}
  277.                                          termsUrl={props.sdUrls.terms} privacyUrl={props.sdUrls.privacy}
  278.                                          sderrors={register_errors} document={login_data.document} />
  279.             }
  280.             const renderSuccess = () => {
  281.                 if (!is_logged) {
  282.                     return null;
  283.                 }
  284.                 return <RouletteSuccess params={props.winParams} award={selected_award} onClose={onCloseMessage}/>
  285.             }
  286.             const renderTryAgain = () => {
  287.                 if (!is_logged) {
  288.                     return null;
  289.                 }
  290.                 return <RouletteTryAgain params={props.tryAgainParams} award={selected_award} onClose={onCloseMessage}/>
  291.             }
  292.             const renderWarning = () => {
  293.                 if (!is_logged) {
  294.                     return null;
  295.                 }
  296.                 return <RouletteWarning params={props.warParams} onClose={onCloseMessage}/>
  297.             }
  298.             return (
  299.                     <>
  300.                         <div className="ruleta_container ww_center">
  301.                             <RouletteInfo data={props.info}/>
  302.                             <Roulette params={props.rParams} onSpin={onSpin}/>
  303.                         </div>
  304.                         {renderLogin()}
  305.                         {renderRegister()}
  306.                         {renderSuccess()}
  307.                         {renderTryAgain()}
  308.                         {renderWarning()}
  309.                     </>
  310.             );
  311.         }
  312.         const r_info = {
  313.             'preffix': '{{ home.preffix }}',
  314.             'title': '{{ home.title }}',
  315.             'suffix': '{{ home.suffix }}',
  316.             'title_strong': '{{ home.titleStrong }}',
  317.             'description': '{{ home.description ? home.description|raw|replace({"<p>":"", "</p>":""}) : "" }}'
  318.         }
  319.         const r_params = {
  320.             'image': '{{ home.rouletteImage|image_path ? asset(home.rouletteImage|image_path) : "" }}',
  321.             'image_alt': '{{ home.rouletteImage|image_path ? home.rouletteImage|split_alt : "" }}',
  322.             'btn_text': '{{ home.rouletteBtn }}',
  323.         }
  324.         const home_url = "{{ path('home') }}";
  325.         const sd_urls = {
  326.             "login": "{{ path('user_login') }}",
  327.             "register": "{{ path('user_register') }}",
  328.             "terms": "{{ path('terms_and_conditions') }}",
  329.             "privacy": "{{ path('data_protection_policies') }}",
  330.             "spin": "{{ path('roulette_award') }}",
  331.         }
  332.         const login_title = "{{ home.loginTitle }}";
  333.         const register_title = "{{ home.registerTitle }}";
  334.         const win_params = {
  335.             'title': '{{ home.winTitle }}',
  336.             'btn': '{{ home.winBtn }}',
  337.             'url': home_url,
  338.             'image': '{{ asset('static/img/ruleta-deco.gif') }}',
  339.             'image_center': '{{ asset('static/img/ruleta-deco-center.gif') }}',
  340.         };
  341.         const try_again_params = {
  342.             'title': '{{ home.tryAgainTitle }}',
  343.             'btn': '{{ home.tryAgainBtn }}',
  344.         };
  345.         const war_params = {
  346.             'title': '{{ home.warningTitle }}',
  347.             'text': '{{ home.warningText ? home.warningText|raw|replace({"<p>":"", "</p>":""}) : "" }}',
  348.             'btn': '{{ home.warningBtn }}',
  349.             'url': home_url
  350.         };
  351.         const is_logged = "{{ is_granted('ROLE_USER') ? 1 : 0 }}" == "1";
  352.         const current_date = parseInt("{{ current_date }}");
  353.         const award_length = parseInt("{{ awards|length }}")
  354.         const rootElement = document.getElementById('roulette_app');
  355.         let current_user = null;
  356.         {% if app.user %}
  357.         current_user = {
  358.             "id": "{{ app.user.id }}",
  359.             "name": "{{ app.user.name }}",
  360.             "blocked": parseInt("{{ app.user.nextPlayOn ? app.user.nextPlayOn|date('U') : 0 }}"),
  361.             "current_date": current_date
  362.         }
  363.         {% endif %}
  364.         ReactDOM.render(<App info={r_info} rParams={r_params} logged={is_logged} sdUrls={sd_urls} currentDate={current_date}
  365.                              currentUser={current_user}
  366.                              login={login_title} register={register_title} winParams={win_params} tryAgainParams={try_again_params} warParams={war_params}
  367.                              awardCount={award_length}/>, rootElement);
  368.     </script>
  369. {% endblock %}