Advertisement
Guest User

Untitled

a guest
May 16th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. const app = new Vue({
  2. el: '#app',
  3. data: {
  4. msg: 'Make Post:',
  5. content: '', updatedContent:'',
  6. posts: [],
  7. postId: '',
  8. successMsg: '',
  9. commentData:{},
  10. commentSeen: false,
  11. image:'',
  12. bUrl: 'http://localhost:8888/larabook',
  13. },
  14.  
  15. ready: function(){
  16. this.created();
  17. },
  18.  
  19. created(){
  20. axios.get(this.bUrl +'/posts')
  21. .then(response => {
  22. console.log(response); // show if success
  23. this.posts = response.data; //we are putting data into our posts array
  24. Vue.filter('myOwnTime', function(value){
  25. return moment(value).fromNow();
  26. });
  27. })
  28. .catch(function (error) {
  29. console.log(error); // run if we have error
  30. });
  31. },
  32.  
  33. methods:{
  34.  
  35. addPost(){
  36.  
  37. axios.post(this.bUrl +'/addPost', {
  38. content: this.content
  39. })
  40. .then( (response) =>{
  41. this.content="";
  42. console.log('saved successfully'); // show if success
  43. if(response.status===200){
  44. app.posts = response.data;
  45. }
  46. })
  47. .catch(function (error) {
  48. console.log(error); // run if we have error
  49. });
  50. },
  51. openModal(id){
  52. //console.log(id);
  53. axios.get(this.bUrl +'/posts/' + id)
  54. .then(response => {
  55. console.log(response); // show if success
  56. this.updatedContent = response.data; //we are putting data into our posts array
  57. })
  58. .catch(function (error) {
  59. console.log(error); // run if we have error
  60. });
  61. },
  62. updatePost(id){
  63. axios.post(this.bUrl +'/updatePost/' + id, {
  64. updatedContent: this.updatedContent
  65. })
  66. .then( (response) =>{
  67. this.content="";
  68. console.log('Changes saved successfully'); // show if success
  69. if(response.status===200){
  70. app.posts = response.data;
  71. }
  72. })
  73. .catch(function (error) {
  74. console.log(error); // run if we have error
  75. });
  76. },
  77.  
  78. deletePost(id){
  79. //alert(id);
  80. axios.get(this.bUrl +'/deletePost/' + id)
  81. .then(response => {
  82. console.log(response); // show if success
  83. this.posts = response.data; //we are putting data into our posts array
  84. })
  85. .catch(function (error) {
  86. console.log(error); // run if we have error
  87. });
  88.  
  89. },
  90. likePost(id){
  91. axios.get(this.bUrl +'/likePost/' + id)
  92. .then(response => {
  93. console.log(response); // show if success
  94. this.posts = response.data; //we are putting data into our posts array
  95. })
  96. .catch(function (error) {
  97. console.log(error); // run if we have error
  98. });
  99. },
  100. addComment(post,key){
  101.  
  102. axios.post(this.bUrl +'/addComment', {
  103. comment: this.commentData[key],
  104. id: post.id
  105. })
  106. .then(function (response) {
  107. console.log('saved successfully'); // show if success
  108. if(response.status===200){
  109. app.posts = response.data;
  110. }
  111. })
  112. .catch(function (error) {
  113. console.log(error); // run if we have error
  114. });
  115.  
  116. },
  117.  
  118. onFileChange(e){
  119. var files = e.target.files || e.dataTransfer.files;
  120. this.createImg(files[0]); // files the image/ file value to our function
  121.  
  122. },
  123. createImg(file){
  124. // we will preview our image before upload
  125. var image = new Image;
  126. var reader = new FileReader;
  127.  
  128. reader.onload = (e) =>{
  129. this.image = e.target.result;
  130. };
  131. reader.readAsDataURL(file);
  132. },
  133.  
  134. uploadImg(){
  135. axios.post('http://localhost:8888/larabook/saveImg', {
  136. image: this.image,
  137. content: this.content
  138. })
  139. .then( (response) =>{
  140. console.log('saved successfully'); // show if success
  141. this.image= "";
  142. this.content = "";
  143. if(response.status===200){
  144. app.posts = response.data;
  145. }
  146. })
  147. .catch(function (error) {
  148. console.log(error); // run if we have error
  149. });
  150. },
  151. removeImg(){
  152. this.image=""
  153. }
  154.  
  155.  
  156.  
  157. }
  158. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement