Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. <template>
  2. <div class="hello container">
  3. <div v-for="tweet in msg">
  4. <div class="card">
  5. <div class="card-image">
  6. <figure class="image is-128x128px">
  7. <img :src="tweet.image">
  8. </figure>
  9. </div>
  10.  
  11. <div class="card-content">
  12. <div class="media">
  13. <div class="media-left">
  14. <figure class="image is-48x48">
  15. <img src="http://bulma.io/images/placeholders/96x96.png">
  16. </figure>
  17. </div>
  18. <div class="media-content">
  19. <p class="title is-4">John Smith</p>
  20. <p class="subtitle is-6">@johnsmith</p>
  21. </div>
  22. </div>
  23. </div>
  24.  
  25. <div class="content">
  26. {{tweet.text}}
  27. <br>
  28. <small>{{tweet.date_created}}</small>
  29. </div>
  30.  
  31. <footer class="card-footer">
  32. <a class="card-footer-item" :id="tweet.id" @click="deletePost($event)">
  33. Delete
  34. </a>
  35. </footer>
  36. </div>
  37. <br> <br>
  38. </div>
  39.  
  40. <br><br>
  41.  
  42.  
  43. <div class="field">
  44. <form enctype="multipart/form-data" v-on:submit.prevent="makePost" method="POST">
  45. <p class="control">
  46. <input class="input is-hovered" type="text" ref="postTitle" placeholder="Title" />
  47. </p>
  48. <br>
  49. <p class="control">
  50. <textarea class="textarea is-hovered" ref="postText" placeholder="What's up?">
  51. </textarea>
  52. </p>
  53. <br>
  54.  
  55. <input type="file" @change="onFileChange" accept="image/*">
  56. </input>
  57.  
  58. <button class="button is-primary" type="submit">Submit</button>
  59.  
  60. <div>
  61. <img :src="this.img" />
  62. </div>
  63. </form>
  64. </div>
  65. </div>
  66. </template>
  67.  
  68. <script>
  69. import axios from 'axios';
  70.  
  71.  
  72.  
  73. const BASE_URL = 'http://localhost:8000/tweets/';
  74.  
  75. export default {
  76. name: 'hello',
  77. data () {
  78. return {
  79. msg: [],
  80. info: '',
  81. img: '',
  82. }
  83. },
  84.  
  85. computed: {
  86.  
  87.  
  88. },
  89.  
  90. methods: {
  91.  
  92. //invoked when input receives a file and puts the formData into
  93. //the info property inside the data object
  94. //Also calls createdImage() with the file as a parameter
  95. onFileChange(event) {
  96. event.preventDefault();
  97. const files = event.target.files || event.dataTransfer.files;
  98. const formData = new FormData();
  99.  
  100. formData.append('title', this.$refs.postTitle.value);
  101. formData.append('text', this.$refs.postText.value);
  102. formData.append('image', files[0]);
  103.  
  104. this.createdImage(files[0]);
  105.  
  106. return this.info = formData
  107.  
  108. },
  109.  
  110. //takes in the first file from onFileChange() and returns the image which is used
  111. //to preview the image inside a div
  112. createdImage(file) {
  113. //const image = new Image();
  114. const reader = new FileReader();
  115.  
  116. reader.onload = (e => {
  117. return this.img = e.target.result;
  118. });
  119.  
  120. return reader.readAsDataURL(file);
  121.  
  122.  
  123. },
  124.  
  125. //resets info inside the data object
  126. reset() {
  127. this.info = '';
  128. },
  129.  
  130. //makes a post request using the formData in this.info inside the data object
  131. makePost() {
  132. axios.post(BASE_URL, this.info).then(response => {
  133. console.log(response);
  134. this.reset();
  135. window.location.href="http://localhost:8080";
  136. }).catch(err => { console.log(err) })
  137. },
  138.  
  139. //delete request using the :id of the event
  140. deletePost(event) {
  141.  
  142. const targetID = event.currentTarget.id;
  143.  
  144. axios.delete(`${BASE_URL}${targetID}/`).then(res => {
  145. console.log(res);
  146. window.location.href="http://localhost:8080";
  147. });
  148. },
  149.  
  150.  
  151. },
  152.  
  153. //when mounted, makes an API call to DjangoRestFramework backend
  154. mounted() {
  155. axios.get(BASE_URL).then(response => {this.msg = response.data});
  156. },
  157.  
  158.  
  159.  
  160. }
  161.  
  162.  
  163. </script>
  164.  
  165.  
  166. <!-- Add "scoped" attribute to limit CSS to this component only -->
  167. <style lang="css">
  168. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement