Skip to content

Commit

Permalink
fix: Multiple request default value by sql. (#1385)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwinBetanc0urt authored Nov 21, 2021
1 parent 559d648 commit 29f7ac8
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 47 deletions.
17 changes: 17 additions & 0 deletions src/api/ADempiere/user-interface/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,20 @@ export function getEntities({
return convertEntityList(response)
})
}

/**
* Get default value for a field, parameter or query criteria
* @param {string} query, sql to get value
*/
export function requestDefaultValue(query) {
return request({
url: '/user-interface/window/default-value',
method: 'get',
params: {
query
}
})
.then(valueResponse => {
return valueResponse
})
}
14 changes: 0 additions & 14 deletions src/api/ADempiere/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,6 @@ export function requestReferencesList({
})
}

// Get default value for a field
export function requestDefaultValue(query) {
return request({
url: '/user-interface/window/default-value',
method: 'get',
params: {
query
}
})
.then(respose => {
return respose
})
}

/**
* Get context information for a window, tab or field
* @param {string} query
Expand Down
11 changes: 10 additions & 1 deletion src/components/ADempiere/Field/mixin/mixinField.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,21 @@ export default {

async created() {
if (this.metadata.isSQLValue && (this.isEmptyValue(this.metadata.value) || this.metadata.value.isSQL)) {
const value = await this.$store.dispatch('getValueBySQL', {
let value = this.$store.getters.getStoredDefaultValue({
parentUuid: this.metadata.parentUuid,
containerUuid: this.metadata.containerUuid,
query: this.metadata.defaultValue
})

if (this.isEmptyValue(value)) {
value = await this.$store.dispatch('getValueBySQL', {
parentUuid: this.metadata.parentUuid,
containerUuid: this.metadata.containerUuid,
columnName: this.metadata.columnName,
query: this.metadata.defaultValue
})
}

// set value into component and fieldValue store
this.value = this.parseValue(value)

Expand Down
32 changes: 0 additions & 32 deletions src/store/modules/ADempiere/data/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
getEntity
} from '@/api/ADempiere/common/persistence.js'
import {
requestDefaultValue,
requestGetContextInfoValue
} from '@/api/ADempiere/window'

Expand All @@ -33,7 +32,6 @@ import { isEmptyValue } from '@/utils/ADempiere/valueUtils.js'
import { convertArrayKeyValueToObject } from '@/utils/ADempiere/valueFormat.js'
import { typeValue } from '@/utils/ADempiere/valueUtils.js'
import {
parseContext,
getPreference
} from '@/utils/ADempiere/contextUtils'
import { showMessage } from '@/utils/ADempiere/notification'
Expand Down Expand Up @@ -377,36 +375,6 @@ const actions = {
})
},

/**
* @param {string} parentUuid
* @param {string} containerUuid
* @param {string} query
*/
getValueBySQL({ commit }, {
parentUuid,
containerUuid,
query
}) {
// TODO: Change to promise all
return new Promise(resolve => {
if (query.includes('@')) {
query = parseContext({
parentUuid,
containerUuid,
isSQL: true,
value: query
}).query
}

requestDefaultValue(query)
.then(defaultValueResponse => {
resolve(defaultValueResponse)
})
.catch(error => {
console.warn(`Error getting default value from server. Error code ${error.code}: ${error.message}.`)
})
})
},
/**
* TODO: Add support to tab children
* @param {string} parentUuid
Expand Down
125 changes: 125 additions & 0 deletions src/store/modules/ADempiere/defaultValueManager.js
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

0 comments on commit 29f7ac8

Please sign in to comment.