actions.js
127 lines
| 1 | window.jfbEventBus = window.jfbEventBus || new Vue( {} ); |
| 2 | const { apiFetch } = wp; |
| 3 | |
| 4 | const getActionFromRecord = ( record, slug ) => { |
| 5 | return record.actions.value.find( action => slug === action.value ); |
| 6 | }; |
| 7 | |
| 8 | const apiOptions = getters => { |
| 9 | const { action, payload } = getters.currentProcess; |
| 10 | const [ checked ] = payload; |
| 11 | |
| 12 | let actionEndpoint = getters.getAction( action ); |
| 13 | |
| 14 | if ( !actionEndpoint ) { |
| 15 | const record = payload[ 3 ] ?? {}; |
| 16 | |
| 17 | actionEndpoint = getActionFromRecord( record, action ); |
| 18 | } |
| 19 | |
| 20 | const options = getters.fetchListOptions( actionEndpoint?.endpoint ); |
| 21 | |
| 22 | return { |
| 23 | ...options, |
| 24 | ...getters.apiOptions, |
| 25 | data: { |
| 26 | checked, |
| 27 | ...getters.apiData, |
| 28 | }, |
| 29 | }; |
| 30 | }; |
| 31 | |
| 32 | export default { |
| 33 | fetchPage( { commit, getters, dispatch } ) { |
| 34 | commit( 'toggleLoading', 'page' ); |
| 35 | const url = getters.receiveEndpoint; |
| 36 | |
| 37 | dispatch( 'fetch', getters.fetchListOptions( url ) ).then( response => { |
| 38 | dispatch( 'updateList', response ); |
| 39 | dispatch( 'updateQueryState' ); |
| 40 | |
| 41 | // clear checked rows |
| 42 | commit( 'unChooseHead' ); |
| 43 | commit( 'setChecked', [] ); |
| 44 | } ).finally( () => { |
| 45 | commit( 'toggleLoading', 'page' ); |
| 46 | } ); |
| 47 | }, |
| 48 | fetchPageWithFilters( { commit, getters, dispatch, state } ) { |
| 49 | commit( 'toggleLoading', 'page' ); |
| 50 | dispatch( 'updateQueryState', 1 ); |
| 51 | const url = getters.receiveEndpoint; |
| 52 | |
| 53 | dispatch( 'fetch', getters.fetchListOptions( url ) ).then( response => { |
| 54 | dispatch( 'updateList', response ); |
| 55 | jfbEventBus.reactiveCounter++; |
| 56 | } ).catch( response => { |
| 57 | if ( !response?.hasOwnProperty?.( 'code' ) ) { |
| 58 | return; |
| 59 | } |
| 60 | switch ( response.code ) { |
| 61 | case 'not_found': |
| 62 | dispatch( 'updateList', { list: [], total: 0 } ); |
| 63 | break; |
| 64 | } |
| 65 | } ).finally( response => { |
| 66 | commit( 'toggleLoading', 'page' ); |
| 67 | } ); |
| 68 | }, |
| 69 | updateList( { commit, getters, dispatch, state }, response ) { |
| 70 | commit( 'setList', response.list ); |
| 71 | |
| 72 | if ( getters.queryState ) { |
| 73 | commit( 'setTotal', response?.total ?? getters.queryState.total ); |
| 74 | } |
| 75 | |
| 76 | if ( response.list.length > getters.getLimit ) { |
| 77 | commit( 'setLimit', response.list.length ); |
| 78 | } |
| 79 | |
| 80 | commit( 'setOffset', 0 ); |
| 81 | }, |
| 82 | fetch( { commit, getters }, options ) { |
| 83 | return new Promise( ( resolve, reject ) => { |
| 84 | apiFetch( options ).then( resolve ).catch( error => { |
| 85 | jfbEventBus.$CXNotice.add( { |
| 86 | message: error.message, |
| 87 | type: 'error', |
| 88 | duration: 4000, |
| 89 | } ); |
| 90 | |
| 91 | reject( error ); |
| 92 | } ).finally( reject ); |
| 93 | } ); |
| 94 | }, |
| 95 | apiFetch( { getters, dispatch } ) { |
| 96 | dispatch( 'beforeRunFetch' ); |
| 97 | |
| 98 | return apiFetch( apiOptions( getters ) ); |
| 99 | }, |
| 100 | maybeFetchFilters( props, endpoint ) { |
| 101 | const { commit, getters, rootGetters } = props; |
| 102 | |
| 103 | if ( getters.hasFilters || rootGetters.isDoing ) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | commit( 'toggleDoingAction', null, { root: true } ); |
| 108 | |
| 109 | apiFetch( endpoint ).then( response => { |
| 110 | commit( 'setFilters', response.filters ); |
| 111 | jfbEventBus.reactiveCounter++; |
| 112 | } ).finally( () => { |
| 113 | commit( 'toggleDoingAction', null, { root: true } ); |
| 114 | } ); |
| 115 | }, |
| 116 | activeAll( { commit, getters } ) { |
| 117 | const idsList = getters.list.map( row => ( |
| 118 | row?.choose?.value |
| 119 | ) ); |
| 120 | |
| 121 | commit( 'setChecked', idsList ); |
| 122 | }, |
| 123 | clearFiltersWithFetch( { commit, dispatch }, replaceMap ) { |
| 124 | commit( 'clearSelectedFilters', replaceMap ); |
| 125 | dispatch( 'fetchPageWithFilters' ); |
| 126 | }, |
| 127 | }; |