ImageEditable.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <script>
  2. import JSONAPI from '@api/json-axios'
  3. import REST from '@api/rest-axios'
  4. import { mapActions, mapState } from 'pinia'
  5. import { ConcernementsStore } from '@stores/concernements'
  6. import { UserStore } from '@stores/user'
  7. import SvgIcon from '@jamescoyle/vue-icon';
  8. import { mdiTrashCanOutline } from '@mdi/js';
  9. import { mdiImagePlus } from '@mdi/js';
  10. export default {
  11. props: {
  12. can_update: Boolean,
  13. image: Object,
  14. data: Object
  15. },
  16. emits: ['updated'],
  17. data(){
  18. return {
  19. mdiTrashCanOutline_path: mdiTrashCanOutline,
  20. mdiImagePlus_path: mdiImagePlus
  21. }
  22. },
  23. computed: {
  24. ...mapState(UserStore,['csrf_token']),
  25. },
  26. created () {
  27. console.log('ImageEditable created');
  28. },
  29. mounted () {
  30. },
  31. methods: {
  32. ...mapActions(ConcernementsStore, ['reloadConcernements']),
  33. addImage(e){
  34. console.log('addImage', this.$refs);
  35. this.$refs.image_input.click();
  36. },
  37. onInput(e){
  38. console.log('onFileInput', e);
  39. let file = e.target.files[0];
  40. console.log('onFileInput file', file);
  41. this.save(file);
  42. },
  43. save(file){
  44. const configs = {
  45. headers: {
  46. 'X-CSRF-Token': this.csrf_token,
  47. 'Content-Type': 'application/octet-stream',
  48. 'Content-Disposition': `file; filename="${file.name}"`,
  49. },
  50. };
  51. JSONAPI.post(`/${this.data.entitytype}/${this.data.bundle}/${this.data.uuid}/${this.data.field}`, file, configs)
  52. .then(({ data : { data } }) => {
  53. console.log('jsonapi post image', data)
  54. this.$emit('updated');
  55. })
  56. .catch(error => {
  57. console.warn('Issue with jsonapi post image', error)
  58. Promise.reject(error)
  59. })
  60. },
  61. onDeleteImg(e){
  62. console.log('onDeleteImg', e);
  63. const params = {
  64. type: this.data.bundle,
  65. nid: [{"value":this.data.nid}],
  66. [this.data.field]: {value: null}
  67. };
  68. const configs = {
  69. headers: {'X-CSRF-Token': this.csrf_token}
  70. };
  71. REST.patch(`/node/${this.data.nid}?_format=json`, params, configs)
  72. .then(({ data }) => {
  73. console.log(`user REST patch node ${this.data.bundle} ${this.data.field}`, data)
  74. // TODO if success update the data in pinia
  75. // this.reloadConcernements();
  76. this.$emit('updated');
  77. })
  78. .catch(error => {
  79. console.warn(`Issue with patch node ${this.data.bundle} ${this.data.field}`, error)
  80. Promise.reject(error)
  81. })
  82. // 405 JSONAPI operation not allowed, don't know why
  83. // const configs = {
  84. // headers: {
  85. // 'X-CSRF-Token': this.csrf_token,
  86. // 'Content-Type': 'application/octet-stream',
  87. // 'Content-Disposition': `file; filename=""`,
  88. // },
  89. // };
  90. // JSONAPI.delete(`/${this.data.entitytype}/${this.data.bundle}/${this.data.uuid}/${this.data.field}`, {}, configs)
  91. // .then(({ data : { data } }) => {
  92. // console.log('jsonapi delete image', data)
  93. // this.$emit('updated');
  94. // })
  95. // .catch(error => {
  96. // console.warn('Issue with jsonapi post image', error)
  97. // Promise.reject(error)
  98. // })
  99. // console.log('save csrf_token', this.csrf_token);
  100. }
  101. },
  102. components: {
  103. SvgIcon,
  104. }
  105. }
  106. </script>
  107. <template>
  108. <div class="editable-image">
  109. <!-- with img -->
  110. <template v-if="image.length">
  111. <figure>
  112. <img :src="image[0].url" :alt="image[0].alt"/>
  113. <figcaption v-if="image[0].alt">{{ image[0].alt }}</figcaption>
  114. </figure>
  115. <div v-if="can_update" @click="onDeleteImg" class="delete-btn">
  116. <svg-icon type="mdi" :path="mdiTrashCanOutline_path" />
  117. </div>
  118. </template>
  119. <!-- with out img -->
  120. <template v-else-if="can_update">
  121. <div @click="addImage" class="btn">
  122. <svg-icon type="mdi" :path="mdiImagePlus_path" @click="onDeleteImg" />
  123. <!-- <span>ajouter une image</span> -->
  124. </div>
  125. <input ref="image_input" type="file" accept ="image/jpeg, image/png, image/jpg" @input="onInput">
  126. </template>
  127. </div>
  128. </template>