Guest User

Untitled

a guest
Dec 3rd, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { get, post, put, del } from './requester.js';
  2.  
  3. function getPartials() {
  4.     return {
  5.         header: './templates/common/header.hbs',
  6.         footer: './templates/common/footer.hbs',
  7.     }
  8. }
  9.  
  10. const app = Sammy('#rooter', function () {
  11.     this.use('Handlebars', 'hbs');
  12.  
  13.     this.get('/', function (ctx) {
  14.         setHeaderInfo(ctx);
  15.  
  16.         if (ctx.isAuth) {
  17.             get('appdata', 'recipes', 'Kinvey')
  18.                 .then(recipes => {
  19.                     ctx.recipes = recipes;
  20.  
  21.                     this.loadPartials(getPartials())
  22.                         .partial('./templates/home.hbs');
  23.                 })
  24.         } else {
  25.             this.loadPartials(getPartials())
  26.                 .partial('./templates/home.hbs');
  27.         }
  28.     });
  29.  
  30.  
  31.     this.get('/login', function (ctx) {
  32.         setHeaderInfo(ctx);
  33.  
  34.         this.loadPartials(getPartials())
  35.             .partial('./templates/auth/login.hbs');
  36.     });
  37.  
  38.     this.post('/login', function (ctx) {
  39.         const { username, password } = ctx.params;
  40.  
  41.         if (username && password) {
  42.             post('user', 'login', { username, password }, 'Basic')
  43.                 .then(userInfo => {
  44.                     saveAuthInfo(userInfo);
  45.                     ctx.redirect('/');
  46.                 })
  47.                 .catch(console.error);
  48.         }
  49.     })
  50.  
  51.     this.get('/register', function () {
  52.  
  53.         this.loadPartials(getPartials())
  54.             .partial('./templates/auth/register.hbs');
  55.     })
  56.  
  57.     this.post('/register', function (ctx) {
  58.         const { firstName, lastName, username, password, repeatPassword } = ctx.params;
  59.  
  60.         if (firstName && lastName && username && password === repeatPassword) {
  61.             post('user', '', { firstName, lastName, username, password }, 'Basic')
  62.                 .then(userInfo => {
  63.                     saveAuthInfo(userInfo);
  64.                     ctx.redirect('/');
  65.                 })
  66.                 .catch(console.error);
  67.         } else {
  68.             alert('Passwords doesn\'t match')
  69.         }
  70.     })
  71.  
  72.     this.get('/logout', function (ctx) {
  73.  
  74.         post('user', '_logout', {}, 'Kinvey')
  75.             .then(() => {
  76.                 sessionStorage.clear();
  77.                 ctx.redirect('/');
  78.             })
  79.     })
  80.  
  81.     this.get('/share', function (ctx) {
  82.         setHeaderInfo(ctx);
  83.         this.loadPartials(getPartials())
  84.             .partial('./templates/recipes/shareRecipe.hbs')
  85.     });
  86.  
  87.     this.post('/share', function (ctx) {
  88.         setHeaderInfo(ctx);
  89.  
  90.         const { meal, ingredients, prepMethod, description, foodImageURL, category } = ctx.params;
  91.         const categories = {
  92.             'Vegetables and legumes/beans': 'https://cdn.pixabay.com/photo/2017/10/09/19/29/eat-2834549__340.jpg',
  93.             'Grain Food': 'https://cdn.pixabay.com/photo/2014/12/11/02/55/corn-syrup-563796__340.jpg',
  94.             'Fruits': 'https://cdn.pixabay.com/photo/2017/06/02/18/24/fruit-2367029__340.jpg',
  95.             'Milk, chees, eggs and alternatives': 'https://image.shutterstock.com/image-photo/assorted-dairy-products-milk-yogurt-260nw-530162824.jpg',
  96.             'Lean meats and poultry, fish and alternatives': 'https://t3.ftcdn.net/jpg/01/18/84/52/240_F_118845283_n9uWnb81tg8cG7Rf9y3McWT1DT1ZKTDx.jpg'
  97.         }
  98.         if (meal && ingredients && prepMethod && description && foodImageURL && category) {
  99.             post('appdata', 'recipes',
  100.                 {
  101.                     meal,
  102.                     ingredients: ingredients.split(' '),
  103.                     prepMethod, description, foodImageURL,
  104.                     category,
  105.                     likesCounter: 0,
  106.                     categoryImageURL: categories[category]
  107.                 }, 'Kinvey').then(() => {
  108.                     ctx.redirect('/')
  109.                         .catch(console.error);
  110.                 })
  111.         }
  112.     });
  113.  
  114.     this.get('/recipe/:id', function (ctx) {
  115.         setHeaderInfo(ctx);
  116.         const id = ctx.params.id;
  117.  
  118.         get('appdata', `recipes/${id}`, 'Kinvey')
  119.             .then(data => {
  120.                 data.isCreator = sessionStorage.getItem('userId') === data._acl.creator;
  121.                 ctx.recipe = data;
  122.  
  123.                 this.loadPartials(getPartials())
  124.                     .partial('../templates/recipes/recipe-details.hbs')
  125.             }).catch(console.error)
  126.     });
  127.  
  128.     this.post('/like/:id', function (ctx) {
  129.         setHeaderInfo(ctx);
  130.         const id = ctx.params.id;
  131.         let { meal, ingredients, prepMethod, description, foodImageURL, likesCounter, category } = ctx.params;
  132.         const categories = {
  133.             'Vegetables and legumes/beans': 'https://cdn.pixabay.com/photo/2017/10/09/19/29/eat-2834549__340.jpg',
  134.             'Grain Food': 'https://cdn.pixabay.com/photo/2014/12/11/02/55/corn-syrup-563796__340.jpg',
  135.             'Fruits': 'https://cdn.pixabay.com/photo/2017/06/02/18/24/fruit-2367029__340.jpg',
  136.             'Milk, chees, eggs and alternatives': 'https://image.shutterstock.com/image-photo/assorted-dairy-products-milk-yogurt-260nw-530162824.jpg',
  137.             'Lean meats and poultry, fish and alternatives': 'https://t3.ftcdn.net/jpg/01/18/84/52/240_F_118845283_n9uWnb81tg8cG7Rf9y3McWT1DT1ZKTDx.jpg'
  138.         }
  139.  
  140.         put('appdata', `recipes/${id}`,
  141.             {
  142.                 meal,
  143.                 ingredients: ingredients.split(' '),
  144.                 prepMethod, description, foodImageURL,
  145.                 category,
  146.                 likesCounter,
  147.                 categoryImageURL: categories[category]
  148.             }, 'Kinvey').catch(console.error);
  149.     })
  150.  
  151.  
  152.  
  153.     this.get('/edit/:id', function (ctx) {
  154.         setHeaderInfo(ctx);
  155.         const id = ctx.params.id;
  156.  
  157.         get('appdata', `recipes/${id}`, 'Kinvey')
  158.             .then(recipe => {
  159.                 ctx.recipe = recipe;
  160.  
  161.                 this.loadPartials(getPartials())
  162.                     .partial('../templates/recipes/editForm.hbs')
  163.             })
  164.     })
  165.  
  166.     this.post('/edit/:id', function (ctx) {
  167.         setHeaderInfo(ctx);
  168.         const id = ctx.params.id;
  169.  
  170.         let { meal, ingredients, prepMethod, description, foodImageURL, likesCounter, category } = ctx.params;
  171.         const categories = {
  172.             'Vegetables and legumes/beans': 'https://cdn.pixabay.com/photo/2017/10/09/19/29/eat-2834549__340.jpg',
  173.             'Grain Food': 'https://cdn.pixabay.com/photo/2014/12/11/02/55/corn-syrup-563796__340.jpg',
  174.             'Fruits': 'https://cdn.pixabay.com/photo/2017/06/02/18/24/fruit-2367029__340.jpg',
  175.             'Milk, chees, eggs and alternatives': 'https://image.shutterstock.com/image-photo/assorted-dairy-products-milk-yogurt-260nw-530162824.jpg',
  176.             'Lean meats and poultry, fish and alternatives': 'https://t3.ftcdn.net/jpg/01/18/84/52/240_F_118845283_n9uWnb81tg8cG7Rf9y3McWT1DT1ZKTDx.jpg'
  177.         }
  178.         console.log(ctx.params);
  179.         put('appdata', `recipes/${id}`,
  180.             {
  181.                 meal,
  182.                 ingredients: ingredients.split(' '),
  183.                 prepMethod, description, foodImageURL,
  184.                 category,
  185.                 likesCounter,
  186.                 categoryImageURL: categories[category]
  187.             }, 'Kinvey').then(() => {
  188.                 ctx.redirect('/')
  189.                 .catch(console.error);
  190.             })
  191.     })
  192.  
  193.     function setHeaderInfo(ctx) {
  194.         ctx.isAuth = sessionStorage.getItem('authtoken') !== null;
  195.         ctx.fullName = sessionStorage.getItem('fullName');
  196.     }
  197.  
  198.     function saveAuthInfo(userInfo) {
  199.         sessionStorage.setItem('authtoken', userInfo._kmd.authtoken);
  200.         sessionStorage.setItem('fullName', `${userInfo.firstName} ${userInfo.lastName}`);
  201.         sessionStorage.setItem('userId', userInfo._id);
  202.     }
  203. })
  204. app.run()
Add Comment
Please, Sign In to add comment