let SESSION = localStorage.getItem("user-token"), REFRESH_SESSION = localStorage.getItem("user-refresh-token"), CATEGORY_API_URL = API_URL + "category/", BLOG_CATEGORY_API_URL = API_URL + "blog_category/", BLOG_API_URL = API_URL + "blog/", COMMISSION_API_URL = API_URL + "commission/", COMPANY_API_URL = API_URL + "company/", CONFIG_API_URL = API_URL + "config/", DEPOSIT_API_URL = API_URL + "deposit/", FAQS_API_URL = API_URL + "faq/", INFO_API_URL = API_URL + "page/", USER_API_URL = API_URL + "user/", ORDER_API_URL = API_URL + "order/", VENDOR_API_URL = API_URL + "vendor/", WEBSITE_API_URL = API_URL + "website/", NOTIFICATION_API_URL = API_URL + "notification/", WITHDRAW_API_URL = API_URL + "withdraw/", WIDGET_API_URL = API_URL + "widget/", REVIEW_API_URL = API_URL + "review/", PROMO_CODE_API_URL = API_URL + `promo_code/`, RETRY_COUNT = 0, CATEGORY = [], CONFIG, PROFILE $(document).ready(function(){ renderNavbar() getConfig() getWidget() // getInfoHeader() getInfoFooter() if(SESSION){ getProfile() getNotification() } $('#summernote').summernote({ placeholder: 'Tulis konten Anda disini', tabsize: 2, height: 120, toolbar: [ ['style', ['style']], ['font', ['bold', 'underline', 'clear']], ['color', ['color']], ['para', ['ul', 'ol', 'paragraph']], ['table', ['table']], ['insert', ['link', 'picture', 'video']], ['view', ['fullscreen', 'codeview', 'help']] ] }); }) /* API */ function getConfig(){ $.ajax({ async: true, url: `${CONFIG_API_URL}`, type: 'GET', error: function(res) { const response = JSON.parse(res.responseText) }, success: function(res) { CONFIG = res.data renderIcon(res.data) renderFooter(res.data) renderAboutUs(res.data) } }); } function getWidget(){ $.ajax({ async: true, url: `${WIDGET_API_URL}all`, type: 'GET', error: function(res) { const response = JSON.parse(res.responseText) }, success: function(res) { renderWidget(res.data) } }); } function getInfoFooter(){ $.ajax({ async: true, url: `${INFO_API_URL}position?position=FOOTER,BOTH`, type: 'GET', error: function(res) { const response = JSON.parse(res.responseText) }, success: function(res) { renderFooterMenu(res.data) } }); } function getInfoHeader(){ $.ajax({ async: true, url: `${INFO_API_URL}position?position=HEADER,BOTH`, type: 'GET', error: function(res) { const response = JSON.parse(res.responseText) }, success: function(res) { renderHeaderMenu(res.data) } }); } function getProfile(){ $.ajax({ async: true, url: `${USER_API_URL}profile`, type: 'GET', beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', `Bearer ${SESSION}`); }, error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response) if(isRetry) $.ajax(this) }, success: function(res) { PROFILE = res.data renderProfile(res.data) try { renderProfileForm() }catch(e){ console.log(e) } } }); } function getCartTotal(){ $.ajax({ async: true, url: `${CART_API_URL}item-total`, type: 'GET', beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', `Bearer ${SESSION}`); }, error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response) if(isRetry) $.ajax(this) }, success: function(res) { renderCartTotal(res.data) } }); } function getNotification(){ $.ajax({ async: true, url: `${NOTIFICATION_API_URL}?page_number=0&page_size=5`, type: 'GET', beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', `Bearer ${SESSION}`); }, error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response) if(isRetry) $.ajax(this) }, success: function(res) { renderNotification(res.data) } }); } function readNotification(id){ $.ajax({ async: true, url: `${NOTIFICATION_API_URL}read/${id}`, type: 'PUT', beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', `Bearer ${SESSION}`); }, error: function(res) { const response = JSON.parse(res.responseText) let isRetry = retryRequest(response) if(isRetry) $.ajax(this) }, success: function(res) { console.log("READ NOTiFICATION") getNotification() } }); } /* API */ /* Render */ function renderWidget(widgets){ let html_code = '' for (const widget of widgets) { if(widget.type == 'SCRIPT' || widget.type == 'CSS'){ $('head').append(widget.code); }else{ html_code += widget.code } } $('#widget-container').html(html_code) } function renderProfile(data){ $('.account-name').html(data.first_name) $('.account-email').html(data.email) $('.account-balance').html(formatRupiah(data.balance, true)) // $('#buyer-balance').html(formatRupiah(data.balance, true)) $('.account-income').html(formatRupiah(data.income, true)) // $('#seller-income-total').html(formatRupiah(data.income, true)) // if(parseFloat(data.income) <= 0){ // $('#withdraw-create').hide() // } } function renderCartTotal(data){ $('#navbar-cart-count').html(data) } function renderNotification(data){ // let notificationHtml = ''; // if(data.notification.length > 0){ // for(let item of data.notification){ // const style = parseInt(item.is_read) ? "" : 'style="font-weight:bold"' // const itemHtml = `
  • ${sortText(item.message)}
  • ` // notificationHtml += itemHtml // } // notificationHtml += `
  • //
  • Lihat Semua
  • ` // }else{ // notificationHtml = `
  • Anda tidak memiliki notifikasi
  • ` // } // $('#navbar-notification-list').html(notificationHtml) $('#navbar-notification-count').html(data.total) if(parseInt(data.total) < 1) $('#navbar-notification-count').hide() } function renderAboutUs(data){ $('#footer-about-us').html(data.web_description) } function renderNavbar(){ console.log("RENDER NAVBAR") if(SESSION){ $('.nav-user').css("display", "block") $('.nav-nouser').attr("style", "display: none !important") $('#login-button-offcanvas').html(``) }else{ $('.nav-user').attr("style", "display: none !important") $('.nav-nouser').css("display", "flex") $('#login-button-offcanvas').html(`  Masuk `) } } function renderFooter(data){ //render contact if(data.address){ $('#footer-address').html(`

    ${data.address}

    `) }else{ $('#footer-address-section').hide() } if(data.phone){ $('#footer-phone').html(data.phone) }else{ $('#footer-phone-section').hide() } if(data.email){ $('#footer-email').html(data.email) }else{ $('#footer-email-section').hide() } if(!data.address && !data.phone && !data.email){ $('#footer-contact').hide() } //render social media let footerSosmed = `` if(data.sm_facebook){ footerSosmed += `` } if(data.sm_instagram){ footerSosmed += `` } if(data.sm_twitter){ footerSosmed += `` } if(footerSosmed){ $('#social-links').html(footerSosmed) }else{ $('#social-links-section').hide() } } function renderFooterMenu(data){ let footerMenuHtml = "" for(let menu of data){ let menuHtml = `` footerMenuHtml += menuHtml } footerMenuHtml += `` $('#footer-info').html(footerMenuHtml) } function renderHeaderMenu(data){ if(data.length > 0){ let headerMenuHtml = "" for(let item of data){ const menuHtml = `
  • ${item.title}
  • ` headerMenuHtml += menuHtml } $('#navbar-information-list').html(headerMenuHtml) $('#header-information').show() } } function renderIcon(data){ if(data.web_logo){ $('.brand-logo').prop("src", API_URL+data.web_logo) }else{ $('.brand-logo-section').hide() } if(data.web_icon){ $(".favicon").attr("href", API_URL+data.web_icon); } } function generateRatingStar(rating){ let ratingStar = "" for(let i=0; i