Commit 3d39772e authored by Christian H's avatar Christian H
Browse files

Merge branch 'feature/eventClone' of gitlab.ethz.ch:amiv/amiv-admintool into feature/eventClone

parents 48de5ae0 183122e5
import m from 'mithril';
import axios from 'axios';
import ClientOAuth2 from 'client-oauth2';
import { Snackbar } from 'polythene-mithril';
import { apiUrl, ownUrl, oAuthID } from 'networkConfig';
import * as localStorage from './localStorage';
import config from './resourceConfig.json';
......@@ -15,6 +16,7 @@ const APISession = {
const amivapi = axios.create({
baseURL: apiUrl,
headers: { 'Content-Type': 'application/json' },
validateStatus: () => true,
});
// OAuth Handler
......@@ -76,6 +78,7 @@ export function getSession() {
'Content-Type': 'application/json',
Authorization: APISession.token,
},
validateStatus: () => true,
});
resolve(authenticatedSession);
}).catch(resetSession);
......@@ -176,6 +179,19 @@ export class ResourceHandler {
return `?${m.buildQueryString(fullQuery)}`;
}
networkError(e) {
console.log(e);
Snackbar.show({ title: 'Network error, try again.', style: { color: 'red' } });
}
error422(data) {
Snackbar.show({ title: 'Errors in object, please fix.' });
}
successful(title) {
Snackbar.show({ title, style: { color: 'green' } });
}
get(query) {
return new Promise((resolve, reject) => {
getSession().then((api) => {
......@@ -184,12 +200,13 @@ export class ResourceHandler {
api.get(url).then((response) => {
if (response.status >= 400) {
resetSession();
Snackbar.show({ title: response.data, style: { color: 'red' } });
reject();
} else {
resolve(response.data);
}
}).catch((e) => {
console.log(e);
this.networkError(e);
reject(e);
});
});
......@@ -208,13 +225,14 @@ export class ResourceHandler {
}
api.get(url).then((response) => {
if (response.status >= 400) {
Snackbar.show({ title: response.data, style: { color: 'red' } });
resetSession();
reject();
} else {
resolve(response.data);
}
}).catch((e) => {
console.log(e);
this.networkError(e);
reject(e);
});
});
......@@ -226,17 +244,20 @@ export class ResourceHandler {
getSession().then((api) => {
api.post(this.resource, item).then((response) => {
if (response.code === 201) {
this.successful('Creation successful.');
resolve({});
} else if (response.status === 422) {
this.error422(response.data);
reject(response.data);
} else if (response.status >= 400) {
Snackbar.show({ title: response.data, style: { color: 'red' } });
resetSession();
reject();
} else {
resolve(response.data);
}
}).catch((e) => {
console.log(e);
this.networkError(e);
reject(e);
});
});
......@@ -265,15 +286,18 @@ export class ResourceHandler {
headers: { 'If-Match': item._etag },
}).then((response) => {
if (response.status === 422) {
this.error422(response.data);
reject(response.data);
} else if (response.status >= 400) {
Snackbar.show({ title: response.data, style: { color: 'red' } });
resetSession();
reject();
} else {
this.successful('Change successful.');
resolve(response.data);
}
}).catch((e) => {
console.log(e);
this.networkError(e);
reject(e);
});
});
......@@ -287,13 +311,15 @@ export class ResourceHandler {
headers: { 'If-Match': item._etag },
}).then((response) => {
if (response.status >= 400) {
Snackbar.show({ title: response.data, style: { color: 'red' } });
resetSession();
reject();
} else {
this.successful('Delete successful.');
resolve();
}
}).catch((e) => {
console.log(e);
this.networkError(e);
reject(e);
});
});
......
......@@ -5,6 +5,7 @@ import { styler } from 'polythene-core-css';
// eslint-disable-next-line import/extensions
import { apiUrl } from 'networkConfig';
import EditView from '../views/editView';
// import { colors } from '../style';
const style = [
{
......@@ -26,7 +27,7 @@ export default class newEvent extends EditView {
const copy = JSON.parse(this.form.data.additional_fields);
this.form.data.add_fields_sbb = 'SBB_Abo' in copy.properties;
this.form.data.add_fields_food = 'Food' in copy.properties;
this.form.data.additional_fields = {};
this.form.data.additional_fields = null;
}
// price can either not be set or set to null
......
......@@ -8,7 +8,7 @@ import {
Button,
} from 'polythene-mithril';
import { styler } from 'polythene-core-css';
import { DropdownCard } from 'amiv-web-ui-components';
import { DropdownCard, DatalistController } from 'amiv-web-ui-components';
// eslint-disable-next-line import/extensions
import { apiUrl } from 'networkConfig';
import ItemView from '../views/itemView';
......@@ -125,6 +125,9 @@ export default class viewEvent extends ItemView {
constructor(vnode) {
super(vnode);
this.signupHandler = new ResourceHandler('eventsignups');
this.signupCtrl = new DatalistController((query, search) => this.signupHandler.get({
search, ...query,
}));
this.description = false;
this.advertisement = false;
this.registration = false;
......@@ -144,8 +147,8 @@ export default class viewEvent extends ItemView {
// only show accepted
where.accepted = true;
}
this.signupHandler.get({ where }).then((data) => {
this.emaillist = (data._items.map(item => item.email));
this.signupCtrl.getFullList().then((list) => {
this.emaillist = (list.map(item => item.email));
m.redraw();
});
}
......
......@@ -9,6 +9,7 @@ import {
SVG,
Button,
IconButton,
Snackbar,
} from 'polythene-mithril';
import { styler } from 'polythene-core-css';
import { icons } from './views/elements';
......@@ -152,6 +153,7 @@ export class Layout {
// shadow over content in case drawer is out
m('div.content-hider'),
]),
m(Snackbar),
// dialog element will show when Dialog.show() is called, this is only a placeholder
m(Dialog),
]);
......
......@@ -85,7 +85,7 @@
"pdf": "PDF provided by company",
"time_end": "Application deadline",
"title_de": "Stelle auf Deutsch",
"title_de": "Position title in English",
"title_en": "Position title in English",
"show_website": "Is the job listed on the website?",
"_id":"Job ID."
},
......
......@@ -63,12 +63,13 @@ export default class EditView extends ItemView {
request.catch((error) => {
console.log(error);
// Process the API error
const { response } = error;
if (response.status === 422) {
if ('_issues' in error) {
// there are problems with some fields, display them
Object.keys(response.data._issues).forEach((field) => {
this.errors[field] = [response.data._issues[field]];
Object.keys(error._issues).forEach((field) => {
this.form.errors[field] = [error._issues[field]];
this.form.valid = false;
});
console.log(this.form.errors);
m.redraw();
} else {
console.log(error);
......
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