2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-06-02 13:42:17 +02:00
|
|
|
import { sanitize } from './general_utils';
|
2015-05-19 17:01:28 +02:00
|
|
|
|
|
|
|
// TODO: Create Unittests that test all functions
|
2015-05-19 13:45:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a key-value object of this form:
|
|
|
|
*
|
|
|
|
* {
|
|
|
|
* 'page': 1,
|
|
|
|
* 'pageSize': 10
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* and converts it to a query-parameter, which you can append to your URL.
|
2015-05-19 14:09:10 +02:00
|
|
|
* The return looks like this:
|
|
|
|
*
|
|
|
|
* ?page=1&page_size=10
|
|
|
|
*
|
|
|
|
* CamelCase gets converted to snake_case!
|
2015-06-05 11:06:36 +02:00
|
|
|
*
|
2015-05-19 13:45:19 +02:00
|
|
|
*/
|
2015-06-02 13:48:01 +02:00
|
|
|
export function argsToQueryParams(obj) {
|
2015-05-19 17:01:28 +02:00
|
|
|
|
2015-06-02 13:48:01 +02:00
|
|
|
obj = sanitize(obj);
|
2015-05-19 17:01:28 +02:00
|
|
|
|
2015-06-02 13:48:01 +02:00
|
|
|
return Object
|
|
|
|
.keys(obj)
|
|
|
|
.map((key, i) => {
|
|
|
|
let s = '';
|
2015-05-19 13:45:19 +02:00
|
|
|
|
2015-06-02 13:48:01 +02:00
|
|
|
if(i === 0) {
|
|
|
|
s += '?';
|
|
|
|
} else {
|
|
|
|
s += '&';
|
|
|
|
}
|
2015-05-19 13:45:19 +02:00
|
|
|
|
2015-06-02 13:48:01 +02:00
|
|
|
let snakeCaseKey = key.replace(/[A-Z]/, (match) => '_' + match.toLowerCase());
|
2015-05-19 14:09:10 +02:00
|
|
|
|
2015-06-02 13:48:01 +02:00
|
|
|
return s + snakeCaseKey + '=' + encodeURIComponent(obj[key]);
|
|
|
|
})
|
|
|
|
.join('');
|
2015-06-05 11:06:36 +02:00
|
|
|
}
|
2015-05-21 12:12:25 +02:00
|
|
|
|
2015-06-02 13:48:01 +02:00
|
|
|
/**
|
2015-06-05 11:06:36 +02:00
|
|
|
* Takes a string and a boolean and generates a string query parameter for
|
2015-06-02 13:48:01 +02:00
|
|
|
* an API call.
|
|
|
|
*/
|
|
|
|
export function generateOrderingQueryParams(orderBy, orderAsc) {
|
|
|
|
let interpolation = '';
|
2015-05-21 12:12:25 +02:00
|
|
|
|
2015-06-02 13:48:01 +02:00
|
|
|
if(!orderAsc) {
|
|
|
|
interpolation += '-';
|
|
|
|
}
|
2015-05-21 12:12:25 +02:00
|
|
|
|
2015-06-02 13:48:01 +02:00
|
|
|
return interpolation + orderBy;
|
2015-06-05 11:06:36 +02:00
|
|
|
}
|
2015-05-27 17:33:15 +02:00
|
|
|
|
2015-06-02 13:48:01 +02:00
|
|
|
export function status(response) {
|
|
|
|
if (response.status >= 200 && response.status < 300) {
|
2015-06-05 11:06:36 +02:00
|
|
|
return response;
|
2015-05-19 13:45:19 +02:00
|
|
|
}
|
2015-06-05 11:06:36 +02:00
|
|
|
throw new Error(response.json());
|
|
|
|
}
|
2015-06-16 14:01:53 +02:00
|
|
|
|
|
|
|
export function getCookie(name) {
|
|
|
|
let value = '; ' + document.cookie;
|
|
|
|
let parts = value.split('; ' + name + '=');
|
|
|
|
if (parts.length === 2) {
|
|
|
|
return parts.pop().split(';').shift();
|
|
|
|
}
|
2015-06-30 15:41:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Given a url for an image, this method fetches it and returns a promise that resolves to
|
|
|
|
a blob object.
|
|
|
|
It can be used to create a 64base encoded data url.
|
|
|
|
|
|
|
|
Taken from: http://jsfiddle.net/jan_miksovsky/yy7zs/
|
|
|
|
|
2015-07-14 17:12:32 +02:00
|
|
|
CURRENTLY NOT USED...
|
|
|
|
|
2015-06-30 15:41:39 +02:00
|
|
|
*/
|
|
|
|
export function fetchImageAsBlob(url) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr.open('GET', url, true);
|
|
|
|
|
|
|
|
// Ask for the result as an ArrayBuffer.
|
|
|
|
xhr.responseType = 'arraybuffer';
|
|
|
|
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if(xhr.readyState === 4 && xhr.status >= 400) {
|
|
|
|
reject(xhr.statusText);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.onload = function() {
|
|
|
|
// Obtain a blob: URL for the image data.
|
|
|
|
let arrayBufferView = new Uint8Array(this.response);
|
|
|
|
let blob = new Blob([arrayBufferView], {type: 'image/jpeg'});
|
|
|
|
resolve(blob);
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send();
|
|
|
|
});
|
2015-06-16 14:01:53 +02:00
|
|
|
}
|