From c2eb37506a39e5ed528852b89f1c9ae86e85269d Mon Sep 17 00:00:00 2001 From: Brett Sun Date: Tue, 14 Jun 2016 13:37:16 +0200 Subject: [PATCH] Replace utils in fetch_api with js-utility-belt's --- js/utils/cookie.js | 2 ++ js/utils/fetch_api.js | 69 ------------------------------------------- 2 files changed, 2 insertions(+), 69 deletions(-) create mode 100644 js/utils/cookie.js delete mode 100644 js/utils/fetch_api.js diff --git a/js/utils/cookie.js b/js/utils/cookie.js new file mode 100644 index 00000000..78a929f0 --- /dev/null +++ b/js/utils/cookie.js @@ -0,0 +1,2 @@ +// Re-export related utilities from js-utility-belt for easier access +export { getCookie, setCookie } from 'js-utility-belt/es6/cookie'; diff --git a/js/utils/fetch_api.js b/js/utils/fetch_api.js deleted file mode 100644 index 0b2d45dc..00000000 --- a/js/utils/fetch_api.js +++ /dev/null @@ -1,69 +0,0 @@ -'use strict'; - -import Q from 'q'; -import moment from 'moment'; - -import AppConstants from '../constants/application_constants'; - -// TODO: Create Unittests that test all functions - -export function status(response) { - if (response.status >= 200 && response.status < 300) { - return response; - } - throw new Error(response.json()); -} - -export function getCookie(name) { - let parts = document.cookie.split(';'); - - for(let i = 0; i < parts.length; i++) { - if(parts[i].indexOf(name + '=') > -1) { - return parts[i].split('=').pop(); - } - } -} - -export function setCookie(key, value, days) { - const exdate = moment(); - exdate.add(days, 'days'); - value = window.escape(value) + ((days === null) ? '' : `; expires= ${exdate.utc()}`); - document.cookie = `${key}=${value}`; -} - -/* - - 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/ - - CURRENTLY NOT USED... - - */ -export function fetchImageAsBlob(url) { - return Q.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(); - }); -}