SearchBlock.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <template v-slot="default">
  2. <div v-bind:id="blockid">
  3. <SearchForm
  4. v-if="displayform"
  5. v-bind:form="form"></SearchForm>
  6. </div>
  7. </template>
  8. <script>
  9. import qs from 'querystring-es3'
  10. import SearchForm from 'vuejs/components/Form/SearchForm'
  11. import { mapState, mapActions } from 'vuex'
  12. import MA from 'vuejs/api/ma-axios'
  13. export default {
  14. props: ['blockid', 'formhtml'],
  15. data(){
  16. return {
  17. form: null
  18. }
  19. },
  20. computed: {
  21. ...mapState({
  22. canSearch: state => state.User.canSearch,
  23. keys: state => state.Search.keys,
  24. term: state => state.Search.term,
  25. filters: state => state.Search.filters
  26. }),
  27. displayform(){
  28. // console.log('computed displayform')
  29. return this.canSearch && this.form
  30. }
  31. },
  32. beforeMount() {
  33. // console.log('SearchBlock beforeMount')
  34. this.form = this.formhtml
  35. },
  36. watch: {
  37. canSearch(new_value, old_value) {
  38. // console.log('canSearch changed, old: '+old_value+", new: "+new_value)
  39. if (new_value && !this.form) {
  40. this.getSearchForm()
  41. }
  42. if (!new_value && this.form) {
  43. this.form = null
  44. }
  45. }
  46. },
  47. methods: {
  48. getSearchForm(){
  49. console.log('getSearchForm')
  50. // var urlParams = new URLSearchParams(window.location.search);
  51. // var urlParamsKeys = urlParams.keys()
  52. const params = {
  53. keys: this.keys,
  54. term: this.term,
  55. filters: this.filters
  56. }
  57. console.log('Search getSearchForm params', params)
  58. const q = qs.stringify(params)
  59. MA.get(`/materio_sapi/search_form?`+q)
  60. .then(({data}) => {
  61. // console.log('getSearchForm')
  62. this.form = data.rendered
  63. })
  64. .catch((error) => {
  65. console.warn('Issue with get searchform', error)
  66. })
  67. }
  68. },
  69. components: {
  70. SearchForm
  71. }
  72. }
  73. </script>
  74. <style lang="scss" scoped>
  75. </style>