Add missing feedback function

This commit is contained in:
Pig Fang 2018-08-21 10:00:53 +08:00
parent 7103d52a6e
commit 7b8b9dc379
5 changed files with 50 additions and 56 deletions

View File

@ -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,
};
}

View File

@ -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;

View File

@ -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`,

View File

@ -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));
}

View File

@ -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('&');
}