Advertisement
Guest User

Untitled

a guest
Nov 30th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. function startApp() {
  2. //clear user auth data
  3. sessionStorage.clear()
  4.  
  5. showHideMenuLinks()
  6.  
  7. showView('viewHome')
  8.  
  9. //bind navigation menu links
  10. $('#linkHome').click(showHomeView)
  11. $('#linkLogin').click(showLoginView)
  12. $('#linkRegister').click(showRegisterView)
  13. $('#linkListAds').click(listAds)
  14. $('#linkCreateAd').click(showCreateAdView)
  15. $('#linkLogout').click(logoutUser)
  16.  
  17. //bind the form submit buttons
  18. $('#formLogin').submit(loginUser)
  19. $('#formRegister').submit(registerUser)
  20. $('#buttonCreateAd').click(createAd)
  21. $('#buttonEditAd').click(editAd)
  22.  
  23. //bind the info/error boxes: hide on click
  24. $('#infoBox, #errorBox').click(function () {
  25. $(this).fadeOut()
  26. })
  27.  
  28. // Attach AJAX "loading" event listener
  29. $(document).on({
  30. ajaxStart: function () {
  31. $("#loadingBox").show()
  32. },
  33. ajaxStop: function () {
  34. $("#loadingBox").hide()
  35. }
  36. });
  37.  
  38.  
  39. const kinveyBaseUrl = "https://baas.kinvey.com/";
  40. const kinveyAppKey = "kid_HJbWZ-hMe";
  41. const kinveyAppSecret = "2230ec6d6ff848a3b9f0c65ae46f7b09";
  42. const kinveyAppAuthHeaders = {
  43. 'Authorization': "Basic " + btoa(kinveyAppKey + ":" + kinveyAppSecret),
  44. 'Content-type': "application/json"
  45. };
  46.  
  47. function showHideMenuLinks() {
  48. $('#menu a').hide()
  49. if (sessionStorage.getItem('authToken')) {
  50. // setUsernameInLink()
  51. //logged in user
  52. $('#linkHome').show()
  53. $('#linkListAds').show()
  54. $('#linkCreateAd').show()
  55. $('#linkLogout').show()
  56. $('#loggedInUser').show()
  57. } else {
  58. //no user logged in
  59. $('#linkHome').show()
  60. $('#linkLogin').show()
  61. $('#linkRegister').show()
  62. }
  63. }
  64.  
  65. function showView(viewName) {
  66. // Hide all views and show the selected view only
  67. $('main > section').hide();
  68. $('#' + viewName).show();
  69. }
  70.  
  71. function showHomeView(){
  72. showView('viewHome')
  73. }
  74.  
  75. function showLoginView(){
  76. showView('viewLogin')
  77. $('#formLogin').trigger('reset')
  78. }
  79.  
  80. function showRegisterView(){
  81. showView('viewRegister')
  82. $('#formRegister').trigger('reset')
  83. }
  84.  
  85. function registerUser(event){
  86. event.preventDefault()
  87.  
  88. let data = {
  89. username: $('#formRegister input[name=username]').val(),
  90. password: $('#formRegister input[name=passwd]').val()
  91. }
  92.  
  93. $.ajax({
  94. method: "POST",
  95. url: kinveyBaseUrl + 'user/' + kinveyAppKey + '/',
  96. headers: kinveyAppAuthHeaders,
  97. data: JSON.stringify(data),
  98. success: registerUserSuccess,
  99. error: handleAjaxError
  100. })
  101.  
  102. function registerUserSuccess(userInfo){
  103. saveAuthInSession(userInfo)
  104. showHideMenuLinks()
  105. listAds()
  106. showInfo('User registration successful.')
  107. }
  108.  
  109. }
  110.  
  111.  
  112. function saveAuthInSession(userInfo){
  113. sessionStorage.setItem('username', userInfo.username)
  114. sessionStorage.setItem('authToken', userInfo._kmd.authtoken)
  115. sessionStorage.setItem('userId', userInfo._id)
  116. $('#loggedInUSer').text('Welcome, ' + userInfo.username)
  117. }
  118.  
  119.  
  120. function showInfo(msg){
  121. $('#infoBox').text(msg)
  122. setTimeout(function () {
  123. $('#infoBox').fadeOut()
  124. }, 3000)
  125. }
  126.  
  127.  
  128. function handleAjaxError(error){
  129. $('#errorBox').text('Error: ' + error)
  130. }
  131.  
  132. function listAds(){
  133.  
  134. }
  135.  
  136. function showCreateAdView(){
  137.  
  138. }
  139.  
  140. function logoutUser(){
  141.  
  142. }
  143.  
  144. function loginUser(){
  145.  
  146. }
  147.  
  148. function createAd(){
  149.  
  150. }
  151.  
  152. function editAd(){
  153.  
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement