Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {post, put, del, get} from "./requester.js";
- let partials = {
- header: "./templates/common/header.hbs",
- footer: "./templates/common/footer.hbs"
- };
- const app = Sammy("#eventsHolder", function () {
- this.use("Handlebars", "hbs");
- this.get('index.html', function (ctx) {
- ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
- ctx.username = sessionStorage.getItem("username");
- this.loadPartials(partials).then(function () {
- this.partial("./templates/notLogged/unLogged.hbs");
- });
- });
- this.get('#/', function (ctx) {
- ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
- ctx.username = sessionStorage.getItem("username");
- this.loadPartials(partials).then(function () {
- this.partial("./templates/notLogged/unLogged.hbs");
- });
- });
- this.get("#/login", function (ctx) {
- ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
- ctx.username = sessionStorage.getItem("username");
- this.loadPartials(partials)
- .then(function () {
- this.partial("./templates/auth/login.hbs");
- });
- });
- this.get("#/events/:id", function (ctx) {
- ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
- ctx.username = sessionStorage.getItem("username");
- const id = ctx.params.id;
- get('appdata', `events/${id}`, 'Kinvey')
- .then(teamInfo => {
- const {name, description, imageURL, dateTime, peopleInterestedIn, organizer} = teamInfo;
- ctx.name = name;
- ctx.description = description;
- ctx.imageURL = imageURL;
- ctx._id = ctx.params.id;
- ctx.dateTime = dateTime;
- ctx.peopleInterestedIn = peopleInterestedIn;
- ctx.organizer = organizer;
- if (organizer === sessionStorage.getItem("username")) {
- ctx.isOrganizer = true;
- }
- this.loadPartials(partials)
- .then(function () {
- this.partial('./templates/main/moreDetails.hbs');
- });
- });
- });
- this.get("#/edit/:id", function (ctx) {
- ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
- ctx.username = sessionStorage.getItem("username");
- const id = ctx.params.id;
- get('appdata', `events/${id}`, 'Kinvey')
- .then(teamInfo => {
- const {name, description, imageURL, dateTime} = teamInfo;
- ctx.name = name;
- ctx.description = description;
- ctx.imageURL = imageURL;
- ctx._id = ctx.params.id;
- ctx.dateTime = dateTime;
- this.loadPartials(partials)
- .then(function () {
- this.partial('./templates/main/editEvent.hbs');
- });
- });
- });
- this.post("#/edit/:id", function (ctx) {
- ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
- ctx.username = sessionStorage.getItem("username");
- const id = ctx.params.id;
- const {name, description, imageURL, dateTime} = ctx.params;
- get('appdata', `events/${id}`, 'Kinvey')
- .then(teamInfo => {
- teamInfo.name = name;
- teamInfo.description = description;
- teamInfo.imageURL = imageURL;
- teamInfo.dateTime = dateTime;
- put('appdata', `events/${id}`, teamInfo, 'Kinvey')
- .then(res => {
- console.log("here")
- ctx.redirect(`#/events/${id}`);
- }).catch(console.error);
- }).catch(console.error);
- });
- this.get("#/home", function (ctx) {
- ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
- ctx.username = sessionStorage.getItem("username");
- get('appdata', `events`, 'Kinvey')
- .then(teamInfo => {
- teamInfo.forEach(el => {
- ctx.imageURL = {imageURL: el.imageURL};
- ctx.name = {name: el.name};
- });
- ctx.event = teamInfo;
- this.loadPartials(partials)
- .then(function () {
- this.partial('./templates/main/mainView.hbs');
- });
- });
- });
- this.post("#/login", function (ctx) {
- let {username, password} = ctx.params;
- post("user", "login", {username, password}, "Basic")
- .then(data => {
- sessionStorage.setItem("username", `${username}`);
- sessionStorage.setItem("authtoken", `${data._kmd.authtoken}`);
- ctx.redirect("#/home");
- }).catch(console.error);
- });
- this.get("#/close/:id", function (ctx) {
- ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
- ctx.username = sessionStorage.getItem("username");
- let id = ctx.params.id;
- del("appdata", `events/${id}`, "Kinvey")
- .then(data => {
- ctx.redirect("#/home");
- }).catch(console.error);
- });
- this.get("#/join/:id", function (ctx) {
- ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
- ctx.username = sessionStorage.getItem("username");
- let id = ctx.params.id;
- get("appdata", `events/${id}`, "Kinvey")
- .then(data => {
- data.peopleInterestedIn = data.peopleInterestedIn + 1;
- put("appdata", `events/${id}`, data, "Kinvey")
- .then(data => {
- ctx.redirect(`#/events/${id}`);
- }).catch(console.error);
- }).catch(console.error);
- });
- this.get("#/profile", function (ctx) {
- ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
- ctx.username = sessionStorage.getItem("username");
- get("appdata", `events`, "Kinvey")
- .then(data => {
- ctx.count = data.filter(el => el.organizer === sessionStorage.getItem("username")).length;
- ctx.events = data.filter(el => el.organizer === sessionStorage.getItem("username"));
- this.loadPartials(partials)
- .then(function () {
- this.partial("./templates/common/profile.hbs");
- });
- }).catch(console.error);
- });
- this.get("#/register", function (ctx) {
- this.loadPartials(partials)
- .then(function () {
- this.partial("./templates/auth/register.hbs");
- });
- });
- this.get("#/organizeEvent", function (ctx) {
- ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
- ctx.username = sessionStorage.getItem("username");
- this.loadPartials(partials)
- .then(function () {
- this.partial("./templates/commands/organizeEvent.hbs");
- });
- });
- this.post("#/organizeEvent", function (ctx) {
- let {name, dateTime, description, imageURL} = ctx.params;
- post("appdata", "events", {
- name, dateTime, description, imageURL,
- "peopleInterestedIn": 0,
- "organizer": `${sessionStorage.getItem("username")}`
- }, "Kinvey")
- .then(data => {
- ctx.redirect("#/home");
- }).catch(console.error);
- });
- this.post("#/register", function (ctx) {
- let {username, password} = ctx.params;
- post('user', '', {username, password}, 'Basic')
- .then(userInfo => {
- ctx.redirect('#/login');
- })
- .catch(console.error);
- });
- this.get("#/logout", function (ctx) {
- ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
- ctx.username = sessionStorage.getItem("username");
- sessionStorage.clear();
- this.loadPartials(partials)
- .then(function () {
- ctx.redirect('#/index.html');
- });
- });
- });
- app.run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement