Commit f9b164a8 authored by Sandro Lutz's avatar Sandro Lutz
Browse files

Refactor event models

parent b87b4aab
import m from 'mithril';
import { apiUrl } from 'config';
import { getToken, getUserId, isLoggedIn } from '../auth';
/**
* Event class
*
* Wrapper for the event object received from the API with additional functions.
*/
export default class Event {
/**
* Constructor
*
* @param {object} event object loaded from the API
*/
constructor(event) {
// Expose all properties of `event`
Object.keys(event).forEach(key => {
this[key] = event[key];
});
}
/**
* Load the signup data of the authenticated user.
*
* @return {Promise}
*/
async loadSignup() {
if (!isLoggedIn()) return undefined;
const queryString = m.buildQueryString({
where: JSON.stringify({
user: getUserId(),
event: this._id,
}),
});
const response = await m.request({
method: 'GET',
url: `${apiUrl}/eventsignups?${queryString}`,
headers: {
Authorization: getToken(),
},
});
if (response._items.length === 1) {
[this._signup] = response._items;
} else {
this._signup = undefined;
}
this.signupLoaded = true;
return this._signup;
}
/**
* Checks if the signup data has been loaded.
*
* @return {Boolean}
*/
get hasSignupDataLoaded() {
return this.signupLoaded;
}
/**
* Get signup data of the authenticated user.
*
* @return {object} signup data
*/
get signupData() {
return this._signup;
}
/**
* Sign off the authenticated user from this event.
*
* @return {Promise}
*/
async signoff() {
if (!this._signup) return;
await m.request({
method: 'DELETE',
url: `${apiUrl}/eventsignups/${this._signup._id}`,
headers: {
Authorization: getToken(),
'If-Match': this._signup._etag,
},
});
this._signup = undefined;
this.signupLoaded = false;
}
/**
* Sign up the authenticated user for this event.
*
* @param {*} additionalFields
* @param {string} email email address (required if not logged in!)
* @return {Promise}
*/
async signup(additionalFields, email = '') {
let additionalFieldsString;
if (this.additional_fields) {
additionalFieldsString = JSON.stringify(additionalFields);
}
if (this._signup) {
return this._updateSignup(additionalFieldsString);
}
return this._createSignup(additionalFieldsString, email);
}
async _createSignup(additionalFieldsString, email = '') {
const data = {
event: this._id,
additional_fields: additionalFieldsString,
};
if (isLoggedIn()) {
data.user = getUserId();
} else if (this.allow_email_signup) {
data.email = email;
} else {
throw new Error('Signup not allowed');
}
this._signup = await m.request({
method: 'POST',
url: `${apiUrl}/eventsignups`,
data,
headers: {
Authorization: getToken(),
},
});
return this._signup;
}
async _updateSignup(additionalFieldsString) {
this._signup = await m.request({
method: 'PATCH',
url: `${apiUrl}/eventsignups/${this._signup._id}`,
data: {
additional_fields: additionalFieldsString,
},
headers: {
Authorization: getToken(),
'If-Match': this._signup._etag,
},
});
return this._signup;
}
}
import m from 'mithril';
import { apiUrl } from 'config';
import { getToken, getUserId, isLoggedIn } from './auth';
import { currentLanguage } from './language';
import PaginationController from './pagination';
/**
* Event class
*/
export class Event {
/**
* Constructor
*
* @param {object} event object loaded from the API
*/
constructor(event) {
// Expose all properties of `event`
Object.keys(event).forEach(key => {
this[key] = event[key];
});
}
/**
* Load the signup data of the authenticated user.
*
* @return {Promise}
*/
async loadSignup() {
if (!isLoggedIn()) return undefined;
const queryString = m.buildQueryString({
where: JSON.stringify({
user: getUserId(),
event: this._id,
}),
});
const response = await m.request({
method: 'GET',
url: `${apiUrl}/eventsignups?${queryString}`,
headers: {
Authorization: getToken(),
},
});
if (response._items.length === 1) {
[this._signup] = response._items;
} else {
this._signup = undefined;
}
this.signupLoaded = true;
return this._signup;
}
/**
* Checks if the signup data has been loaded.
*
* @return {Boolean}
*/
get hasSignupDataLoaded() {
return this.signupLoaded;
}
/**
* Get signup data of the authenticated user.
*
* @return {object} signup data
*/
get signupData() {
return this._signup;
}
/**
* Sign off the authenticated user from this event.
*
* @return {Promise}
*/
async signoff() {
if (!this._signup) return;
await m.request({
method: 'DELETE',
url: `${apiUrl}/eventsignups/${this._signup._id}`,
headers: {
Authorization: getToken(),
'If-Match': this._signup._etag,
},
});
this._signup = undefined;
this.signupLoaded = false;
}
/**
* Sign up the authenticated user for this event.
*
* @param {*} additionalFields
* @param {string} email email address (required if not logged in!)
* @return {Promise}
*/
async signup(additionalFields, email = '') {
let additionalFieldsString;
if (this.additional_fields) {
additionalFieldsString = JSON.stringify(additionalFields);
}
if (this._signup) {
return this._updateSignup(additionalFieldsString);
}
return this._createSignup(additionalFieldsString, email);
}
async _createSignup(additionalFieldsString, email = '') {
const data = {
event: this._id,
additional_fields: additionalFieldsString,
};
if (isLoggedIn()) {
data.user = getUserId();
} else if (this.allow_email_signup) {
data.email = email;
} else {
throw new Error('Signup not allowed');
}
this._signup = await m.request({
method: 'POST',
url: `${apiUrl}/eventsignups`,
data,
headers: {
Authorization: getToken(),
},
});
return this._signup;
}
async _updateSignup(additionalFieldsString) {
this._signup = await m.request({
method: 'PATCH',
url: `${apiUrl}/eventsignups/${this._signup._id}`,
data: {
additional_fields: additionalFieldsString,
},
headers: {
Authorization: getToken(),
'If-Match': this._signup._etag,
},
});
return this._signup;
}
}
/**
* EventListController class (inherited from `PaginationController`)
*
* Used to handle a list of a specific type of event (e.g. all past events)
*/
export class EventListController extends PaginationController {
constructor(query = {}, additionalQuery = {}) {
super('events', query, additionalQuery);
}
async _loadData(query) {
const items = await super._loadData(query);
return items.map(event => {
const otherLanguage = currentLanguage() === 'en' ? 'de' : 'en';
const newEvent = Object.assign({}, event);
newEvent.getTitle = () =>
newEvent[`title_${currentLanguage()}`] || newEvent[`title_${otherLanguage}`];
newEvent.getDescription = () =>
newEvent[`description_${currentLanguage()}`] || newEvent[`description_${otherLanguage}`];
return new Event(newEvent);
});
}
}
import { getToken } from '../auth';
import { currentLanguage } from '../language';
import EventListController from './EventListController';
import Event from './Event';
/**
* EventController class
*
* Managing multiple type of event lists and handling of the currently selected event.
* Managing multiple event lists of type EventListController
* (with open registration, upcoming, past events) and
* handling of the currently selected event.
*/
export class EventController {
export default class EventController {
/**
* Constructor
*
......
import { currentLanguage } from '../language';
import PaginationController from '../pagination';
import Event from './Event';
/**
* EventListController class (inherited from `PaginationController`)
*
* Used to handle a list of a specific type of event (e.g. all past events)
*/
export default class EventListController extends PaginationController {
constructor(query = {}, additionalQuery = {}) {
super('events', query, additionalQuery);
}
async _loadData(query) {
const items = await super._loadData(query);
return items.map(event => {
const otherLanguage = currentLanguage() === 'en' ? 'de' : 'en';
const newEvent = Object.assign({}, event);
newEvent.getTitle = () =>
newEvent[`title_${currentLanguage()}`] || newEvent[`title_${otherLanguage}`];
newEvent.getDescription = () =>
newEvent[`description_${currentLanguage()}`] || newEvent[`description_${otherLanguage}`];
return new Event(newEvent);
});
}
}
export { default as Event } from './Event';
export { default as EventController } from './EventController';
export { default as EventListController } from './EventListController';
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