summaryrefslogtreecommitdiff
path: root/client/util.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2019-04-25 19:46:20 +0200
committerJules Laplace <julescarbon@gmail.com>2019-04-25 19:46:20 +0200
commitc9c72cdc3128fe272edeb6ec20959b2248f33877 (patch)
treebb04bf8a90ab11417b113675426ce58ca5595289 /client/util.js
parent4d5c3d59f32b80638d82373d33a476652520e260 (diff)
frontend demo
Diffstat (limited to 'client/util.js')
-rw-r--r--client/util.js55
1 files changed, 55 insertions, 0 deletions
diff --git a/client/util.js b/client/util.js
new file mode 100644
index 0000000..5b20c2a
--- /dev/null
+++ b/client/util.js
@@ -0,0 +1,55 @@
+/* Mobile check */
+
+export const isiPhone = !!((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
+export const isiPad = !!(navigator.userAgent.match(/iPad/i))
+export const isAndroid = !!(navigator.userAgent.match(/Android/i))
+export const isMobile = isiPhone || isiPad || isAndroid
+export const isDesktop = !isMobile
+export const isFirefox = typeof InstallTrigger !== 'undefined'
+
+const htmlClassList = document.body.parentNode.classList
+htmlClassList.add(isDesktop ? 'desktop' : 'mobile')
+if (isFirefox) {
+ htmlClassList.add('firefox')
+}
+
+/* AJAX */
+
+export const get = (uri, data) => {
+ let headers = {
+ Accept: 'application/json, application/xml, text/play, text/html, *.*',
+ }
+ let opt = {
+ method: 'GET',
+ body: data,
+ headers,
+ // credentials: 'include',
+ }
+ // console.log(headers)
+ // headers['X-CSRFToken'] = csrftoken
+ return fetch(uri, opt).then(res => res.json())
+}
+
+export const post = (uri, data) => {
+ let headers
+ if (data instanceof FormData) {
+ headers = {
+ Accept: 'application/json, application/xml, text/play, text/html, *.*',
+ }
+ } else {
+ headers = {
+ Accept: 'application/json, application/xml, text/play, text/html, *.*',
+ 'Content-Type': 'application/json; charset=utf-8',
+ }
+ data = JSON.stringify(data)
+ }
+ let opt = {
+ method: 'POST',
+ body: data,
+ headers,
+ // credentials: 'include',
+ }
+ // console.log(headers)
+ // headers['X-CSRFToken'] = csrftoken
+ return fetch(uri, opt).then(res => res.json())
+}