From 31001439361c5112337eed2fb2cf8823e65e5f50 Mon Sep 17 00:00:00 2001
From: Moritz Schneider <scmoritz@student.ethz.ch>
Date: Sat, 2 Dec 2017 18:41:45 +0100
Subject: [PATCH] inital studydocs

---
 src/index.js              |  7 +++++++
 src/models/studydocs.js   | 35 +++++++++++++++++++++++++++++++++++
 src/views/studydocList.js | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 77 insertions(+)
 create mode 100644 src/models/studydocs.js
 create mode 100644 src/views/studydocList.js

diff --git a/src/index.js b/src/index.js
index bfe37ecf..ccb2a91c 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,4 +1,6 @@
 // src/index.js
+import studydocList from './views/studydocList';
+
 const m = require('mithril');
 
 const Layout = require('./views/layout');
@@ -42,4 +44,9 @@ m.route(document.body, '/', {
       return m(Layout, m(amivLayout, m(board)));
     },
   },
+  '/studydocuments': {
+    render() {
+      return m(Layout, m(studydocList));
+    },
+  },
 });
diff --git a/src/models/studydocs.js b/src/models/studydocs.js
new file mode 100644
index 00000000..a8da678e
--- /dev/null
+++ b/src/models/studydocs.js
@@ -0,0 +1,35 @@
+import { apiUrl } from './config';
+import { log } from './log';
+import { getToken } from './auth';
+
+const m = require('mithril');
+
+let querySaved = {};
+
+
+export function getList() {
+  if (typeof this.list === 'undefined') {
+    return [];
+  }
+  return this.list;
+}
+
+export function load(query = {}) {
+  querySaved = query;
+  log('hallo');
+  const queryEncoded = m.buildQueryString(JSON.stringify(query));
+
+  return m.request({
+    method: 'GET',
+    url: `${apiUrl}/studydocuments?${queryEncoded}`,
+    headers: {
+      Authorization: `Token ${getToken()}`,
+    },
+  }).then((result) => {
+    this.list = result._items;
+  });
+}
+
+export function reload() {
+  return load(querySaved);
+}
diff --git a/src/views/studydocList.js b/src/views/studydocList.js
new file mode 100644
index 00000000..b1103ff7
--- /dev/null
+++ b/src/views/studydocList.js
@@ -0,0 +1,35 @@
+import * as studydocs from '../models/studydocs';
+import { apiUrl } from '../models/config';
+
+const m = require('mithril');
+
+const tableHeadings = [
+  'title', 'lecture', 'professor', 'semester', 'author', 'download',
+];
+
+export default class studydocList {
+  constructor(vnode) {
+    this.vnode = vnode;
+  }
+
+  static oninit() {
+    studydocs.load();
+  }
+
+  static view() {
+    return m('div', [
+      m('table', [
+        m('thead', m('tr', tableHeadings.map(header => m('th', header)))),
+        m('tbody', studydocs.getList().map(doc => m('tr', [
+          m('td', doc.title),
+          m('td', doc.lecture),
+          m('td', doc.professor),
+          m('td', doc.semester),
+          m('td', doc.author),
+          m('td', m('a', { href: apiUrl + doc.files[0].file }, 'download')),
+        ]))),
+      ]),
+
+    ]);
+  }
+}
-- 
GitLab