Commit 68227c0c authored by adietmue's avatar adietmue
Browse files

Add first frontend steps, separate front- and backend code.

parent 686c7d98
......@@ -99,3 +99,83 @@ ENV/
# mypy
.mypy_cache/
# Js ignore
Skip to content
This repository
Search
Pull requests
Issues
Marketplace
Explore
@NotSpecial
Sign out
Watch 2,277
Star 56,377 Fork 24,811 github/gitignore
Code Pull requests 160 Projects 0 Insights
Branch: master Find file Copy pathgitignore/Node.gitignore
e68f3c0 on Sep 1
@rdela rdela Node - update Compiled binary addons link to https
33 contributors @stuartpb @shiftkey @ro31337 @melonmanchan @Gouthamve @jucrouzet @cheddar @arcresu @sroberts @rsolomo @peat @noinkling @MikeRalphson @shtr0m @kerimdzhanov @joreyaesh @jenrik @izuzak @iamsebastian @rdela @luiscoms @levrik @cyrbil @morrislaptop @indexzero @bbodenmiller and others
RawBlameHistory
60 lines (42 sloc) 885 Bytes
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Typescript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
This diff is collapsed.
<html>
<head>
<title>Hello world</title>
</head>
<body>
<script src="bin/app.js"></script>
</body>
</html>
\ No newline at end of file
{
"name": "pvk_tool_frontend",
"version": "1.0.0",
"description": "Mithril.js frontend for the new PVK tool.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "budo src/index.js --live --open",
"build": "webpack -p"
},
"dependencies": {
"mithril": "^1.1.5"
},
"devDependencies": {
"webpack": "^3.8.1"
}
}
// Api calls
var m = require("mithril");
var apiUrl = "https://amiv-api.ethz.ch";
var adminGroupName = "PVK Admins";
var Session = {
// Quick check if session exists
active() {return this.data.token},
// User data
user: {},
// Session data
data: {},
// Admin?
admin: false,
// Login and Logout
login(username, password) {
// Login and request user data along with the session
return m.request({
method: "POST",
url: apiUrl + '/sessions?embedded={"user": 1}',
data: {
username: username,
password: password
},
}).then(data => {
// Session data
this.data = {
token: data.token,
_id: data._id,
_etag: data._etag,
}
// User data
this.user = {
name: data.user.firstname,
nethz: data.user.nethz,
department: data.user.department,
_id: data.user._id,
}
// Check if admin
this.check_admin(data.token);
})
},
logout() {
if (this.data.token) {
return m.request({
method: "DELETE",
url: apiUrl + "/sessions/" + this.data._id,
headers: {
Authorization: "Token " + this.data.token,
"If-Match": this.data._etag,
},
}).then((data) => {
this.data = this.user = {};
this.admin = false;
})
} else {
// Nothing to do, return promise that always resolves
return Promise.resolve();
}
},
check_admin(token) {
// First: Look for Admin Group
let projection = "projection=" + JSON.stringify({'_id': 1});
let where = "where=" + JSON.stringify({'name': adminGroupName});
m.request({
method: "GET",
url: apiUrl + "/groups?" + projection + "&" + where,
headers: {Authorization: "Token " + token},
}).then((data) => {
if (data._meta.total !== 0) {
// Admin group exists and we have it's id, check membership
let groupId = data._items[0]._id;
let where = 'where=' + JSON.stringify({
user: this.user._id,
group: groupId,
});
m.request({
method: "GET",
url: apiUrl + "/groupmemberships?" + where,
headers: {Authorization: "Token " + token},
}).then((data) => {
if (data._meta.total !== 0) {
// User is admin!
this.admin = true;
};
})
}
});
},
};
module.exports = {
Session: Session,
};
var m = require("mithril")
var root = document.body
var Layout = require("./layout.js")
var user = {}
// var MyComponent = require("./mycomponent")
var count = 0 // added a variable
var Hello = {
view: function() {
return m("main", [
m("h1", {class: "title"}, "My first app"),
// changed the next line
m("button", {onclick: function() {count++}}, count + " clicks"),
])
}
}
var Splash = {
view: function() {
return m("a", {href: "#!/hello"}, "Enter!")
}
}
m.route(root, "/courses", {
"/courses": {render() {return m(Layout, m(Splash))}},
"/hello": {render() {return m(Layout, m(Hello))}},
})
// Basic Layout with sidebar
// Idea from https://mithril.js.org/simple-application.html
var m = require("mithril");
var api = require("./api.js");
var Session = api.Session;
var nethz = "";
var password = "";
var error = "";
var busy = false;
function ready() {return !busy && nethz && password;};
var Login = {
view() {
return Session.active() ? [
// User is logged in
m("p", "Hello, " + Session.user.name),
Session.admin ? m("div", "You are an admin!") : [],
m("button", {onclick() {
busy= true;
Session.logout().then(data => {
busy = false;
err = "";
}).catch(err => {
error = err._error.message;
busy = false;
})
}}, "Abmelden"),
] : [
// User is not logged in
m("div", error),
m("form", [
m("label", "nethz"),
m("input[type=text][placeholder=nethz]", {
oninput: m.withAttr("value", value => {nethz = value}),
value: nethz,
}),
m("label", "password"),
m("input[type=text][placeholder=password]", {
oninput: m.withAttr("value", value => {password = value}),
value: password,
}),
]),
ready() ? m("button", {
onclick() {
if (ready()) {
busy = true;
Session.login(nethz, password).then(data => {
// nethz("");
// password("");
error = "";
busy = false;
}).catch(err => {
error = err._error.message;
busy = false;
})
}
}}, "Anmelden") : m("button[disabled]", "Anmelden"),
]
},
};
module.exports = {
view(vnode) {
return m("main", [
m("div", m(Login)),
m("div", vnode.children),
])
}
};
module.exports = {
view: function() {return "hello from a module"}
}
\ No newline at end of file
......@@ -2,6 +2,11 @@
A new AMIV PVK tool using Eve and authenticating users with AMIVAPI.
## Frontend
WIP.
## Backend
The backend is implemented using [Eve](http://python-eve.org), a python
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment