forked from PanJiaChen/vue-element-admin
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Multiple request default value by sql. (#1385)
- Loading branch information
1 parent
559d648
commit 29f7ac8
Showing
5 changed files
with
152 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// ADempiere-Vue (Frontend) for ADempiere ERP & CRM Smart Business Solution | ||
// Copyright (C) 2017-Present E.R.P. Consultores y Asociados, C.A. | ||
// Contributor(s): Edwin Betancourt [email protected] www.erpya.com | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
import Vue from 'vue' | ||
|
||
// api request methods | ||
import { requestDefaultValue } from '@/api/ADempiere/user-interface/persistence.js' | ||
import { isEmptyValue } from '@/utils/ADempiere/valueUtils.js' | ||
|
||
// utils and helper methods | ||
import { parseContext } from '@/utils/ADempiere/contextUtils' | ||
|
||
const initState = { | ||
storedDefaultValue: {} | ||
} | ||
|
||
const defaultValueManager = { | ||
state: initState, | ||
|
||
mutations: { | ||
setDefaultValue(state, { clientId, parsedQuery, value }) { | ||
const key = `${clientId}_${parsedQuery}` | ||
|
||
Vue.set(state.storedDefaultValue, key, { | ||
clientId, | ||
parsedQuery, | ||
value | ||
}) | ||
}, | ||
resetStateDefaultValue(state) { | ||
state = initState | ||
} | ||
}, | ||
|
||
actions: { | ||
/** | ||
* @param {string} parentUuid | ||
* @param {string} containerUuid | ||
* @param {string} query | ||
*/ | ||
getValueBySQL({ commit, rootGetters }, { | ||
parentUuid, | ||
containerUuid, | ||
columnName, | ||
query | ||
}) { | ||
// TODO: Change to promise all | ||
return new Promise(resolve => { | ||
let parsedQuery = query | ||
if (query.includes('@')) { | ||
parsedQuery = parseContext({ | ||
parentUuid, | ||
containerUuid, | ||
isSQL: true, | ||
value: query | ||
}).query | ||
} | ||
|
||
requestDefaultValue(parsedQuery) | ||
.then(valueResponse => { | ||
const clientId = rootGetters.getPreferenceClientId | ||
commit('setDefaultValue', { | ||
clientId, | ||
parsedQuery, | ||
value: valueResponse | ||
}) | ||
|
||
commit('updateValueOfField', { | ||
parentUuid, | ||
containerUuid, | ||
columnName, | ||
value: valueResponse | ||
}) | ||
|
||
resolve(valueResponse) | ||
}) | ||
.catch(error => { | ||
console.warn(`Error getting default value from server. Error code ${error.code}: ${error.message}.`) | ||
}) | ||
}) | ||
} | ||
}, | ||
|
||
getters: { | ||
getStoredDefaultValue: (state, getters, rootState, rootGetters) => ({ | ||
parentUuid, | ||
containerUuid, | ||
query | ||
}) => { | ||
let parsedQuery = query | ||
if (!isEmptyValue(query) && query.includes('@')) { | ||
parsedQuery = parseContext({ | ||
parentUuid, | ||
containerUuid, | ||
value: query, | ||
isBooleanToString: true | ||
}).value | ||
} | ||
|
||
const clientId = rootGetters.getPreferenceClientId | ||
const key = `${clientId}_${parsedQuery}` | ||
|
||
const defaultValue = state.storedDefaultValue[key] | ||
if (!isEmptyValue(defaultValue)) { | ||
return defaultValue.value | ||
} | ||
return undefined | ||
} | ||
} | ||
} | ||
|
||
export default defaultValueManager |