Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.     <div class="row col-12">
  3.         <div class="col-12">
  4.             <div class="alert alert-danger" v-if="error.show">{{ error.message }}</div>
  5.             <div class="alert alert-success" v-if="success.show">{{ success.message }}</div>
  6.         </div>
  7.         <div class="col-lg-6 mx-auto">
  8.             <div class="form-group row">
  9.                 <label class="col-sm-3 col-form-label">Автор:</label>
  10.                 <div class="col-sm-9">
  11.                     <input type="text" v-model="review.name" class="form-control" placeholder="Иван Петров...">
  12.                 </div>
  13.             </div>
  14.             <div class="form-group row">
  15.                 <label class="col-sm-3 col-form-label">Позиция:</label>
  16.                 <div class="col-sm-9">
  17.                     <input type="text" v-model="review.position" class="form-control" placeholder="Работал аниматором...">
  18.                 </div>
  19.             </div>
  20.             <div class="form-group row">
  21.                 <label class="col-sm-3 col-form-label">Текст:</label>
  22.                 <div class="col-sm-9">
  23.                     <textarea rows="5" class="form-control" v-model="review.description" placeholder="Все очень понравилось..."></textarea>
  24.                 </div>
  25.             </div>
  26.             <div class="text-center" v-if="!editMode">
  27.                 <button class="btn btn-success px-4">
  28.                     <i class="fas fa-circle-notch fa-spin" v-if="!loaded"></i>
  29.                     <span v-if="loaded" @click.stop="sendData">Создать</span>
  30.                 </button>
  31.             </div>
  32.             <div class="text-center" v-if="editMode">
  33.                 <button class="btn btn-success px-4">
  34.                     <i class="fas fa-circle-notch fa-spin" v-if="!loaded"></i>
  35.                     <span v-if="loaded" @click.stop="saveData">Сохранить</span>
  36.                 </button>
  37.             </div>
  38.         </div>
  39.         <div class="col-lg-6 mx-auto">
  40.             <div class="form-group row">
  41.                 <label class="col-sm-3 col-form-label">Ссылка на видео:</label>
  42.                 <div class="col-sm-9">
  43.                     <input type="text" v-model="review.video_url" class="form-control" placeholder="(не обязательно)">
  44.                 </div>
  45.             </div>
  46.             <div class="form-group row">
  47.                 <label class="col-sm-3 col-form-label">Изображения:</label>
  48.                 <div class="input-group col-sm-9">
  49.                     <div class="custom-file">
  50.                         <input type="file" ref="file_input" @change="addFile" class="custom-file-input" id="inputGroupFile" accept="image/*">
  51.                         <label class="custom-file-label" for="inputGroupFile" aria-describedby="inputGroupFileAddon02">Выберите файл...</label>
  52.                     </div>
  53.                 </div>
  54.             </div>
  55.             <div class="form-group row">
  56.                 <div class="card col-6 mb-3 px-0" v-for="(image, index) in images">
  57.                     <img class="card-img-top" :src="image">
  58.                     <div class="card-body">
  59.                         <a href="#" class="btn btn-danger" @click.stop="removeImage(index)">Удалить</a>
  60.                     </div>
  61.                 </div>
  62.             </div>
  63.         </div>
  64.     </div>
  65. </template>
  66.  
  67. <script>
  68.     export default {
  69.         data: function () {
  70.             return {
  71.                 loaded: false,
  72.                 error: {
  73.                     message: "",
  74.                     show: false,
  75.                 },
  76.                 success: {
  77.                     message: "",
  78.                     show: false,
  79.                 },
  80.  
  81.                 review: {
  82.                     id: null,
  83.                     name: "",
  84.                     position: "",
  85.                     description: "",
  86.                     video_url: "",
  87.                     images: [],
  88.                 },
  89.  
  90.                 images: [],
  91.  
  92.                 editMode: false
  93.             }
  94.         },
  95.  
  96.         props: ['reviewId'],
  97.  
  98.         mounted() {
  99.             console.log("Page mounted...");
  100.  
  101.             if(this.reviewId != undefined) {
  102.                 this.editMode = true;
  103.                 this.review.id = this.reviewId;
  104.                 this.loadReview();
  105.             }
  106.  
  107.             this.loaded = true;
  108.         },
  109.  
  110.         methods: {
  111.             loadReview() {
  112.                 this.loaded = false;
  113.  
  114.                 axios.get('/admin/review/info/' + this.review.id).then(response => {
  115.                     console.log(response);
  116.  
  117.                     this.review = response.data.data;
  118.                 }).catch(error => {
  119.                     console.log(error, error.response);
  120.                 });
  121.             },
  122.  
  123.             addFile() {
  124.                 this.review.images.push(this.$refs.file_input.files[0]);
  125.  
  126.                 let input = event.target;
  127.  
  128.                 let reader = new FileReader();
  129.  
  130.                 reader.onload = (e) => {
  131.                     this.images.push(e.target.result);
  132.                 };
  133.  
  134.                 reader.readAsDataURL(input.files[0]);
  135.             },
  136.  
  137.             sendData() {
  138.  
  139.                 this.loaded = false;
  140.  
  141.                 console.log(this.review);
  142.  
  143.                 let formData = new FormData();
  144.  
  145.                 formData.append('images', this.review.images);
  146.  
  147.                 let headers = {'Content-Type': 'multipart/form-data'};
  148.  
  149.                 axios.post('/admin/review/create', formData, headers).then(response => {
  150.                     console.log(response, response.data);
  151.  
  152.                     this.loaded = true;
  153.                 }).catch(error => {
  154.                     console.log(error, error.response);
  155.  
  156.                     if(error.response.data.status == 'error') {
  157.                         this.error = {
  158.                             'message': error.response.data.error,
  159.                             'show': true,
  160.                         };
  161.  
  162.                         setTimeout(() => {
  163.                             this.error.show = false;
  164.                         }, 5000);
  165.                     }
  166.  
  167.                     this.loaded = true;
  168.                 });
  169.             },
  170.  
  171.             saveData() {
  172.  
  173.                 this.loaded = false;
  174.  
  175.                 axios.post('/admin/tag/edit/' + this.tag.id, this.review).then(response => {
  176.                     console.log(response);
  177.  
  178.                     this.success = {
  179.                         'message': "Данные успешно сохранены",
  180.                         'show': true,
  181.                     };
  182.  
  183.                     setTimeout(() => {
  184.                         this.success.show = false;
  185.                     }, 5000);
  186.  
  187.                     this.loaded = true;
  188.                 }).catch(error => {
  189.                     console.log(error, error.response);
  190.  
  191.                     if(error.response.data.status == 'error') {
  192.                         this.error = {
  193.                             'message': error.response.data.error,
  194.                             'show': true,
  195.                         };
  196.  
  197.                         setTimeout(() => {
  198.                             this.error.show = false;
  199.                         }, 5000);
  200.                     }
  201.  
  202.                     this.loaded = true;
  203.                 });
  204.             },
  205.  
  206.             removeImage(index) {
  207.                 this.images.splice(index, 1);
  208.                 this.review.images.splice(index, 1);
  209.             }
  210.         },
  211.     }
  212. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement