diff --git a/resources/assets/src/js/admin/common.js b/resources/assets/src/js/admin/common.js deleted file mode 100644 index c423f1cc..00000000 --- a/resources/assets/src/js/admin/common.js +++ /dev/null @@ -1,52 +0,0 @@ -'use strict'; - -$.extend(true, $.fn.dataTable.defaults, { - language: trans('vendor.datatables'), - scrollX: true, - pageLength: 25, - autoWidth: false, - processing: true, - serverSide: true -}); - -$.fn.dataTable.ext.errMode = 'none'; - -function handleDataTablesAjaxError(event, settings, json, xhr) { - if (json === null) { - showModal(xhr.responseText, trans('general.fatalError'), 'danger'); - } -} - -async function sendFeedback() { - if (document.cookie.replace(/(?:(?:^|.*;\s*)feedback_sent\s*=\s*([^;]*).*$)|^.*$/, '$1') !== '') { - return; - } - - try { - const { errno } = await fetch({ - url: 'https://work.prinzeugen.net/statistics/feedback', - type: 'POST', - dataType: 'json', - data: { - site_name: blessing.site_name, - site_url: blessing.base_url, - version: blessing.version - } - }); - if (errno === 0) { - // It will be expired when current session ends - document.cookie = 'feedback_sent=' + Date.now(); - - console.log('Feedback sent. Thank you!'); - } - } catch (error) { - // - } -} - -if (process.env.NODE_ENV === 'test') { - module.exports = { - sendFeedback, - handleDataTablesAjaxError, - }; -} diff --git a/resources/assets/src/js/feedback.js b/resources/assets/src/js/feedback.js new file mode 100644 index 00000000..65735364 --- /dev/null +++ b/resources/assets/src/js/feedback.js @@ -0,0 +1,34 @@ +import { queryStringify } from './utils'; + +async function sendFeedback() { + if (document.cookie.replace(/(?:(?:^|.*;\s*)feedback_sent\s*=\s*([^;]*).*$)|^.*$/, '$1') !== '') { + return; + } + + const response = await fetch('https://work.prinzeugen.net/statistics/feedback', { + body: queryStringify({ + site_name: blessing.site_name, + site_url: blessing.base_url, + version: blessing.version + }), + headers: { + Accept: 'application/json', + 'Content-Type': 'application/x-www-form-urlencoded' + }, + method: 'POST', + mode: 'cors' + }); + + if (response.ok) { + const { errno } = await response.json(); + + if (errno === 0) { + // It will be expired when current session ends + document.cookie = 'feedback_sent=' + Date.now(); + + console.info('Feedback sent. Thank you!'); + } + } +} + +window.sendFeedback = sendFeedback; diff --git a/resources/assets/src/js/index.js b/resources/assets/src/js/index.js index 7d5063ad..a0fc5bc5 100644 --- a/resources/assets/src/js/index.js +++ b/resources/assets/src/js/index.js @@ -3,6 +3,7 @@ import './i18n'; import './net'; import './layout'; import './logout'; +import './feedback'; console.log( `%c Blessing Skin %c v${blessing.version} %c Made with %c<3%c by printempw.%c https://blessing.studio`, diff --git a/resources/assets/src/js/net.js b/resources/assets/src/js/net.js index a85d0ad4..518186ab 100644 --- a/resources/assets/src/js/net.js +++ b/resources/assets/src/js/net.js @@ -1,4 +1,5 @@ import Vue from 'vue'; +import { queryStringify } from './utils'; import { showAjaxError } from './notify'; const csrfField = document.querySelector('meta[name="csrf-token"]'); @@ -27,10 +28,7 @@ export async function walkFetch(request) { } export async function get(url, params = empty) { - const qs = Object - .keys(params) - .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`) - .join('&'); + const qs = queryStringify(params); return walkFetch(new Request(`${blessing.base_url}${url}${qs && '?' + qs}`, init)); } diff --git a/resources/assets/src/js/utils.js b/resources/assets/src/js/utils.js index 77600737..ff04e0ad 100644 --- a/resources/assets/src/js/utils.js +++ b/resources/assets/src/js/utils.js @@ -26,3 +26,16 @@ export function queryString(key, defaultValue) { return result[1]; } } + +/** + * Serialize data to URL query string + * + * @param {object} data + * @returns {string} + */ +export function queryStringify(params) { + return Object + .keys(params) + .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`) + .join('&'); +}