SelectEditable.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <script>
  2. import REST from '@api/rest-axios'
  3. import { mapActions, mapState } from 'pinia'
  4. import { ConcernementsStore } from '@stores/concernements'
  5. import { UserStore } from '@stores/user'
  6. export default {
  7. props: {
  8. value: String,
  9. options: Object,
  10. label: String,
  11. data: Object
  12. },
  13. emits: ['updated'],
  14. data(){
  15. return {
  16. }
  17. },
  18. computed: {
  19. ...mapState(UserStore,['csrf_token']),
  20. },
  21. created () {
  22. console.log('SelectEditable created');
  23. },
  24. mounted () {
  25. },
  26. methods: {
  27. ...mapActions(ConcernementsStore, ['reloadConcernements']),
  28. onInput(e){
  29. console.log('onInput select e', e);
  30. // let checked = e.target.checked;
  31. let value = e.target.value;
  32. // console.log('onInput checkbox checked', checked);
  33. this.save(value)
  34. },
  35. save(value){
  36. // console.log('save csrf_token', this.csrf_token);
  37. const params = {
  38. type: this.data.bundle,
  39. nid: [{"value":this.data.nid}],
  40. [this.data.field]: {value: value}
  41. };
  42. const configs = {
  43. headers: {'X-CSRF-Token': this.csrf_token}
  44. };
  45. REST.patch(`/node/${this.data.nid}?_format=json`, params, configs)
  46. .then(({ data }) => {
  47. console.log('user REST post node data', data)
  48. // TODO if success update the data in pinia
  49. // this.reloadConcernements();
  50. this.$emit('updated');
  51. })
  52. .catch(error => {
  53. console.warn(`Issue with patch node ${this.data.bundle}`, error)
  54. Promise.reject(error)
  55. })
  56. }
  57. },
  58. }
  59. </script>
  60. <template>
  61. <section class="editable">
  62. <label>{{ label }}</label>
  63. <select :value="value" @input="onInput">
  64. <template v-for="(name,value,index) in options" :key="index">
  65. <option :value="value">{{name}}</option>
  66. </template>
  67. </select>
  68. </section>
  69. </template>