Advertisement
geromero

Untitled

Apr 10th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.60 KB | None | 0 0
  1. $(() => {
  2. const app = Sammy('#main', function () {
  3. this.use('Handlebars', 'hbs');
  4.  
  5. this.get('#/index.html',(ctx)=>{
  6.  
  7. ctx.teamId = sessionStorage.getItem('teamId');
  8. ctx.loggedIn = auth.isAuth();
  9. ctx.username = sessionStorage.getItem('username');
  10. ctx.hasTeam = sessionStorage.getItem('teamId')!==null;
  11. ctx.loadPartials({
  12. header: './templates/common/header.hbs',
  13. footer: './templates/common/footer.hbs',
  14. }).then(function () {
  15. this.partial('./templates/home/home.hbs')
  16. })
  17. })
  18.  
  19. this.get('#/login',(ctx)=>{
  20. ctx.loggedIn = auth.isAuth();
  21. ctx.username = sessionStorage.getItem('username');
  22.  
  23. ctx.loadPartials({
  24. header: './templates/common/header.hbs',
  25. footer: './templates/common/footer.hbs',
  26. loginForm: './templates/login/loginForm.hbs'
  27. }).then(function () {
  28. this.partial('./templates/login/loginPage.hbs')
  29. })
  30. })
  31.  
  32. this.post('#/login',(ctx)=>{
  33. let name = ctx.params.username;
  34. let pass = ctx.params.password;
  35. auth.login(name,pass)
  36. .then((userData)=>{
  37. console.log(userData);
  38. auth.saveSession(userData);
  39. auth.showInfo('Login Successful!')
  40. ctx.redirect('#/index.html');
  41. }).catch((err)=>auth.handleError(err))
  42. })
  43.  
  44. this.get('#/register',(ctx)=>{
  45. ctx.loggedIn = auth.isAuth();
  46. ctx.username = sessionStorage.getItem('username');
  47.  
  48. ctx.loadPartials({
  49. header: './templates/common/header.hbs',
  50. footer: './templates/common/footer.hbs',
  51. registerForm: './templates/register/registerForm.hbs'
  52. }).then(function () {
  53. this.partial('./templates/register/registerPage.hbs')
  54. })
  55.  
  56. });
  57.  
  58. this.post('#/register',(ctx)=>{
  59. let name = ctx.params.username;
  60. let pass = ctx.params.password;
  61. let pass2 = ctx.params.repeatPassword;
  62. if(pass===pass2){
  63. console.log(pass2)
  64. console.log(pass)
  65. auth.register(name,pass)
  66. .then((userData)=>{
  67. auth.saveSession(userData);
  68. auth.showInfo('Register Successful!');
  69. ctx.redirect('#/index.html')
  70. }).catch((err)=>auth.handleError(err))
  71. }
  72. else {
  73. auth.showError('Parolite Ne Pasvat')
  74. }
  75. })
  76.  
  77. this.get('#/logout',(ctx)=>{
  78. auth.logout()
  79. .then(()=>{
  80. sessionStorage.clear();
  81. auth.showInfo('Logout Successful!');
  82. ctx.redirect('#/index.html')
  83. })
  84. .catch((err)=>{
  85. auth.handleError(err);
  86. })
  87. })
  88.  
  89. this.get('#/home',(ctx)=>{
  90. ctx.teamId = sessionStorage.getItem('teamId');
  91. ctx.loggedIn = auth.isAuth();
  92. ctx.username = sessionStorage.getItem('username');
  93. ctx.hasTeam = sessionStorage.getItem('teamId')!==null;
  94. ctx.loadPartials({
  95. header: './templates/common/header.hbs',
  96. footer: './templates/common/footer.hbs',
  97. }).then(function () {
  98. this.partial('./templates/home/home.hbs')
  99. })
  100. })
  101.  
  102. this.get('#/catalog',(ctx)=>{
  103. ctx.loggedIn = auth.isAuth();
  104. ctx.username = sessionStorage.getItem('username');
  105. teamsService.loadTeams()
  106. .then((teams)=>{
  107. ctx.teams = teams.reverse();
  108. ctx.hasNoTeam = sessionStorage.getItem('teamId')===null;
  109. ctx.loadPartials({
  110. header: './templates/common/header.hbs',
  111. footer: './templates/common/footer.hbs',
  112. team: './templates/catalog/team.hbs',
  113.  
  114.  
  115. }).then(function () {
  116. this.partial('./templates/catalog/teamCatalog.hbs')
  117. })
  118. })
  119.  
  120.  
  121. })
  122.  
  123. this.get('#/create',(ctx)=>{
  124.  
  125. ctx.loadPartials({
  126. header: './templates/common/header.hbs',
  127. footer: './templates/common/footer.hbs',
  128. createForm: './templates/create/createForm.hbs',
  129.  
  130. }).then(function () {
  131. this.partial('./templates/create/createPage.hbs')
  132. })
  133. })
  134.  
  135. this.post('#/create',(ctx)=>{
  136. let name = ctx.params.name;
  137. let comment = ctx.params.comment;
  138. teamsService.createTeam(name,comment)
  139. .then((res)=>{
  140.  
  141. let id = res._id
  142. let userData = {
  143. username: sessionStorage.getItem('username'),
  144. teamId: id
  145. };
  146.  
  147. requester.update('user', sessionStorage.getItem('userId'),'kinvey',userData);
  148. sessionStorage.setItem('teamId',res._id);
  149. auth.showInfo('Team created successfully!');
  150. ctx.redirect('#/home')
  151. })
  152. })
  153.  
  154. this.get('#/catalog/:teamId',(ctx)=>{
  155. ctx.loggedIn = auth.isAuth();
  156. ctx.username = sessionStorage.getItem('username');
  157. ctx.isOnTeam = false;
  158. ctx.isAuthor = false;
  159.  
  160. requester.get('user','','kinvey').then((members)=>{
  161. let id = ctx.params.teamId;
  162. id = id.slice(1);
  163.  
  164. ctx.members = members.filter((a)=>a.teamId===id)
  165.  
  166. teamsService.loadTeamDetails(id).then((team)=>{
  167. console.log('ot basata')
  168. console.log(team._id);
  169.  
  170.  
  171.  
  172. console.log(sessionStorage.getItem('teamId'));
  173. if(team._id === sessionStorage.getItem('teamId')){
  174. ctx.isOnTeam = true
  175. }
  176. if(team._acl.creator===sessionStorage.getItem('userId')){
  177. ctx.isAuthor = true;
  178. }
  179. ctx.teamId = team._id;
  180. ctx.name = team.name;
  181. ctx.comment = team.comment;
  182. ctx.loadPartials({
  183. header: './templates/common/header.hbs',
  184. footer: './templates/common/footer.hbs',
  185. teamMember: './templates/catalog/teamMember.hbs',
  186. teamControls: './templates/catalog/teamControls.hbs'
  187. }).then(function () {
  188. this.partial('./templates/catalog/details.hbs')
  189. })
  190. })
  191. });
  192.  
  193.  
  194.  
  195.  
  196.  
  197. })
  198.  
  199. this.get('#/edit/:teamId',(ctx)=>{
  200. ctx.loggedIn = auth.isAuth();
  201. ctx.username = sessionStorage.getItem('username');
  202.  
  203. let id = ctx.params.teamId;
  204. id = id.slice(1);
  205.  
  206. ctx.teamId = id
  207.  
  208.  
  209.  
  210. teamsService.loadTeamDetails(id).then((team)=>{
  211.  
  212. ctx.name = team.name;
  213. ctx.comment = team.comment;
  214.  
  215. ctx.loadPartials({
  216. editForm: './templates/edit/editForm.hbs',
  217. header: './templates/common/header.hbs',
  218. footer: './templates/common/footer.hbs',
  219. }).then(function () {
  220. this.partial('./templates/edit/editPage.hbs')
  221. })
  222. })
  223. })
  224.  
  225. this.post('#/edit/:teamId',(ctx)=>{
  226. ctx.loggedIn = auth.isAuth();
  227. ctx.username = sessionStorage.getItem('username');
  228.  
  229. let teamId = ctx.params.teamId;
  230. teamId = teamId.slice(1);
  231. console.log(teamId)
  232.  
  233.  
  234. let comment = ctx.params.comment;
  235. let name = ctx.params.name;
  236.  
  237. teamsService.edit(teamId,name,comment).then(()=>{
  238. auth.showInfo('Team Edited Successfully!');
  239. ctx.redirect('#/home')
  240. }).catch((reason)=>{
  241. auth.handleError(reason)
  242. })
  243.  
  244. })
  245.  
  246. this.get('#/leave',(ctx)=>{
  247. teamsService.leaveTeam().then((userData)=>{
  248. auth.showInfo('team leaved successfully');
  249. sessionStorage.clear();
  250. auth.saveSession(userData)
  251. ctx.redirect('#/home')
  252. })
  253. })
  254.  
  255. this.get('#/join/:teamId',(ctx)=>{
  256. let id = ctx.params.teamId
  257. id = id.slice(1);
  258. teamsService.joinTeam(id).then((userData)=>{
  259. auth.showInfo('team joined successfully');
  260. sessionStorage.clear();
  261. auth.saveSession(userData)
  262. ctx.redirect('#/home')
  263. })
  264. })
  265.  
  266. this.get('#/about',(ctx)=>{
  267. ctx.loggedIn = auth.isAuth();
  268. ctx.username = sessionStorage.getItem('username');
  269. ctx.loadPartials({
  270. header: './templates/common/header.hbs',
  271. footer: './templates/common/footer.hbs',
  272. }).then(function () {
  273. this.partial('./templates/about/about.hbs')
  274. })
  275. })
  276.  
  277. });
  278.  
  279. app.run();
  280. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement