Advertisement
bobo_bobkata

Untitled

Nov 26th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1. import {post, put, del, get} from "./requester.js";
  2.  
  3. let partials = {
  4. header: "./templates/common/header.hbs",
  5. footer: "./templates/common/footer.hbs"
  6. };
  7.  
  8. const app = Sammy("#eventsHolder", function () {
  9. this.use("Handlebars", "hbs");
  10.  
  11. this.get('index.html', function (ctx) {
  12. ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
  13. ctx.username = sessionStorage.getItem("username");
  14. this.loadPartials(partials).then(function () {
  15. this.partial("./templates/notLogged/unLogged.hbs");
  16. });
  17. });
  18. this.get('#/', function (ctx) {
  19. ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
  20. ctx.username = sessionStorage.getItem("username");
  21. this.loadPartials(partials).then(function () {
  22. this.partial("./templates/notLogged/unLogged.hbs");
  23. });
  24. });
  25.  
  26. this.get("#/login", function (ctx) {
  27. ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
  28. ctx.username = sessionStorage.getItem("username");
  29. this.loadPartials(partials)
  30. .then(function () {
  31. this.partial("./templates/auth/login.hbs");
  32. });
  33. });
  34.  
  35. this.get("#/events/:id", function (ctx) {
  36. ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
  37. ctx.username = sessionStorage.getItem("username");
  38. const id = ctx.params.id;
  39. get('appdata', `events/${id}`, 'Kinvey')
  40. .then(teamInfo => {
  41. const {name, description, imageURL, dateTime, peopleInterestedIn, organizer} = teamInfo;
  42.  
  43. ctx.name = name;
  44. ctx.description = description;
  45. ctx.imageURL = imageURL;
  46. ctx._id = ctx.params.id;
  47. ctx.dateTime = dateTime;
  48. ctx.peopleInterestedIn = peopleInterestedIn;
  49. ctx.organizer = organizer;
  50.  
  51. if (organizer === sessionStorage.getItem("username")) {
  52. ctx.isOrganizer = true;
  53. }
  54.  
  55. this.loadPartials(partials)
  56. .then(function () {
  57. this.partial('./templates/main/moreDetails.hbs');
  58. });
  59. });
  60. });
  61.  
  62. this.get("#/edit/:id", function (ctx) {
  63. ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
  64. ctx.username = sessionStorage.getItem("username");
  65. const id = ctx.params.id;
  66. get('appdata', `events/${id}`, 'Kinvey')
  67. .then(teamInfo => {
  68. const {name, description, imageURL, dateTime} = teamInfo;
  69. ctx.name = name;
  70. ctx.description = description;
  71. ctx.imageURL = imageURL;
  72. ctx._id = ctx.params.id;
  73. ctx.dateTime = dateTime;
  74. this.loadPartials(partials)
  75. .then(function () {
  76. this.partial('./templates/main/editEvent.hbs');
  77. });
  78. });
  79. });
  80.  
  81. this.post("#/edit/:id", function (ctx) {
  82. ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
  83. ctx.username = sessionStorage.getItem("username");
  84.  
  85. const id = ctx.params.id;
  86.  
  87. const {name, description, imageURL, dateTime} = ctx.params;
  88. get('appdata', `events/${id}`, 'Kinvey')
  89. .then(teamInfo => {
  90. teamInfo.name = name;
  91. teamInfo.description = description;
  92. teamInfo.imageURL = imageURL;
  93. teamInfo.dateTime = dateTime;
  94.  
  95. put('appdata', `events/${id}`, teamInfo, 'Kinvey')
  96. .then(res => {
  97. console.log("here")
  98. ctx.redirect(`#/events/${id}`);
  99. }).catch(console.error);
  100. }).catch(console.error);
  101. });
  102.  
  103. this.get("#/home", function (ctx) {
  104. ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
  105. ctx.username = sessionStorage.getItem("username");
  106. get('appdata', `events`, 'Kinvey')
  107. .then(teamInfo => {
  108. teamInfo.forEach(el => {
  109. ctx.imageURL = {imageURL: el.imageURL};
  110. ctx.name = {name: el.name};
  111. });
  112. ctx.event = teamInfo;
  113. this.loadPartials(partials)
  114. .then(function () {
  115. this.partial('./templates/main/mainView.hbs');
  116. });
  117. });
  118. });
  119.  
  120. this.post("#/login", function (ctx) {
  121. let {username, password} = ctx.params;
  122. post("user", "login", {username, password}, "Basic")
  123. .then(data => {
  124.  
  125. sessionStorage.setItem("username", `${username}`);
  126. sessionStorage.setItem("authtoken", `${data._kmd.authtoken}`);
  127. ctx.redirect("#/home");
  128. }).catch(console.error);
  129. });
  130.  
  131. this.get("#/close/:id", function (ctx) {
  132. ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
  133. ctx.username = sessionStorage.getItem("username");
  134. let id = ctx.params.id;
  135. del("appdata", `events/${id}`, "Kinvey")
  136. .then(data => {
  137. ctx.redirect("#/home");
  138. }).catch(console.error);
  139. });
  140.  
  141. this.get("#/join/:id", function (ctx) {
  142. ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
  143. ctx.username = sessionStorage.getItem("username");
  144. let id = ctx.params.id;
  145. get("appdata", `events/${id}`, "Kinvey")
  146. .then(data => {
  147.  
  148. data.peopleInterestedIn = data.peopleInterestedIn + 1;
  149. put("appdata", `events/${id}`, data, "Kinvey")
  150. .then(data => {
  151. ctx.redirect(`#/events/${id}`);
  152. }).catch(console.error);
  153. }).catch(console.error);
  154. });
  155.  
  156. this.get("#/profile", function (ctx) {
  157. ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
  158. ctx.username = sessionStorage.getItem("username");
  159.  
  160. get("appdata", `events`, "Kinvey")
  161. .then(data => {
  162. ctx.count = data.filter(el => el.organizer === sessionStorage.getItem("username")).length;
  163. ctx.events = data.filter(el => el.organizer === sessionStorage.getItem("username"));
  164.  
  165. this.loadPartials(partials)
  166. .then(function () {
  167. this.partial("./templates/common/profile.hbs");
  168. });
  169.  
  170. }).catch(console.error);
  171. });
  172.  
  173.  
  174. this.get("#/register", function (ctx) {
  175. this.loadPartials(partials)
  176. .then(function () {
  177. this.partial("./templates/auth/register.hbs");
  178. });
  179. });
  180.  
  181. this.get("#/organizeEvent", function (ctx) {
  182. ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
  183. ctx.username = sessionStorage.getItem("username");
  184. this.loadPartials(partials)
  185. .then(function () {
  186. this.partial("./templates/commands/organizeEvent.hbs");
  187. });
  188. });
  189.  
  190. this.post("#/organizeEvent", function (ctx) {
  191. let {name, dateTime, description, imageURL} = ctx.params;
  192. post("appdata", "events", {
  193. name, dateTime, description, imageURL,
  194. "peopleInterestedIn": 0,
  195. "organizer": `${sessionStorage.getItem("username")}`
  196. }, "Kinvey")
  197. .then(data => {
  198. ctx.redirect("#/home");
  199. }).catch(console.error);
  200. });
  201.  
  202.  
  203. this.post("#/register", function (ctx) {
  204. let {username, password} = ctx.params;
  205. post('user', '', {username, password}, 'Basic')
  206. .then(userInfo => {
  207. ctx.redirect('#/login');
  208. })
  209. .catch(console.error);
  210. });
  211.  
  212. this.get("#/logout", function (ctx) {
  213. ctx.isLogged = sessionStorage.getItem("authtoken") !== null;
  214. ctx.username = sessionStorage.getItem("username");
  215. sessionStorage.clear();
  216. this.loadPartials(partials)
  217. .then(function () {
  218. ctx.redirect('#/index.html');
  219. });
  220. });
  221. });
  222.  
  223. app.run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement