Skip to content

Instantly share code, notes, and snippets.

@gusgard
Created August 22, 2017 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gusgard/a434fb95fdfa7c95f94719d3295544ec to your computer and use it in GitHub Desktop.
Save gusgard/a434fb95fdfa7c95f94719d3295544ec to your computer and use it in GitHub Desktop.
Event wrapper
...
export default class Event {
static create(data) {
const event = new Event();
event.id = data.id;
event.name = data.name;
event.description = data.description;
event.pictureUrl = data.imageUrl;
// Flags.
event.multipleDates = Boolean(data.multipleDates);
event.multipleLocations = Boolean(data.multipleLocations);
event.guestCanPropose = Boolean(data.guestCanPropose);
event.isArchived = Boolean(data.isArchived);
event.isHosted = Boolean(data.isHosted);
event.isViewed = Boolean(data.isViewed);
event.alreadyVoted = Boolean(data.isVoted);
const {
dates,
locations,
preferredDate,
preferredLocation,
} = Event.formatDatesAndLocations(data);
// Relations.
event.invites = data.invites || [];
event.dates = dates;
event.locations = locations;
event.preferredDate = preferredDate;
event.preferredLocation = preferredLocation;
event.mostLikely = Event._formatMostLikely(event);
event.count = {
// Is Pending only if is not confirmed and don't view the event.
pending: event.invites.filter(i => !i.isConfirmed && !i.isViewed).length,
confirmed: event.invites.filter(i => i.isConfirmed).length,
// Counting host
invited: event.invites.length + 1,
views: event.invites.filter(i => i.isViewed).length,
// Max votes
votes: Math.max(event.preferredDate.voteCount, event.preferredLocation.voteCount),
};
event.status = {
invite: Event._formatInviteStatus(event.count),
hosted: event.isHosted && Event._formatHostedStatus(event.count),
};
return event;
}
/**
* Encode Event to be sent over HTTP
*
* @return Dictionary with expected data to sent.
*/
static encode(data) {
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment