CheckboxEditable.vue 1.4 KB

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