Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
feat: New/Undo without server request. (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwinBetanc0urt authored Jul 5, 2022
1 parent 0d2b56d commit ff96b14
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 150 deletions.
32 changes: 31 additions & 1 deletion src/store/modules/ADempiere/dictionary/window/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export default {
* @param {string} containerUuid
* TODO: Evaluate if it is necessary to parse the default values
*/
setTabDefaultValues({ commit, dispatch, rootGetters }, {
setTabDefaultValues({ commit, dispatch, getters, rootGetters }, {
parentUuid,
containerUuid,
isOverWriteParent = true
Expand Down Expand Up @@ -415,6 +415,13 @@ export default {
})
}

const recordUuid = store.getters.getUuidOfContainer(containerUuid)
// set old record
store.commit('setRecordUuidOnPanel', {
containerUuid,
recordUuid
})

// update fields values
dispatch('updateValuesOfContainer', {
parentUuid,
Expand Down Expand Up @@ -450,7 +457,30 @@ export default {
}
})

// return values
resolve(defaultAttributes)

// update records and logics on child tabs
tab.childTabs
.filter(tabItem => {
// get loaded tabs with records
return getters.getIsLoadedTabRecord({
containerUuid: tabItem.uuid
}) || getters.getIsLoadedTabOldRecord({
containerUuid: tabItem.uuid
})
})
.forEach(tabItem => {
// if loaded data refresh this data
dispatch('setTabDefaultValues', {
parentUuid,
containerUuid: tabItem.uuid
})
commit('setNewTabData', {
parentUuid,
containerUuid: tabItem.uuid
})
})
})
}

Expand Down
3 changes: 0 additions & 3 deletions src/store/modules/ADempiere/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,6 @@ const persistence = {
},

getters: {
getPersistenceMap: (state) => (tableName) => {
return state.persistence[tableName]
},
getPersistenceAttributes: (state) => (containerUuid) => {
const attributesMap = state.persistence[containerUuid]
if (!isEmptyValue(attributesMap)) {
Expand Down
129 changes: 112 additions & 17 deletions src/store/modules/ADempiere/windowManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,29 @@ import { ROW_ATTRIBUTES } from '@/utils/ADempiere/constants/table'

// utils and helper methods
import { containerManager } from '@/utils/ADempiere/dictionary/window'
import { getContextAttributes } from '@/utils/ADempiere/contextUtils.js'
import { getContextAttributes, generateContextKey } from '@/utils/ADempiere/contextUtils.js'
import { isEmptyValue } from '@/utils/ADempiere/valueUtils.js'
import { convertObjectToKeyValue } from '@/utils/ADempiere/valueFormat'
import { showMessage } from '@/utils/ADempiere/notification'
import { generatePageToken } from '@/utils/ADempiere/dataUtils'

const initState = {
tabData: {},
oldTabData: {},
// container uuid: record uuid
currentRecordOnPanel: {}
emtpyTabData: {
parentUuid: undefined,
containerUuid: undefined,
contextKey: '',
searchValue: '',
currentRecordUuid: undefined,
recordsList: [],
selectionsList: [],
nextPageToken: undefined,
recordCount: 0,
isLoaded: false,
pageNumber: 1
}
}

const windowManager = {
Expand All @@ -51,28 +64,56 @@ const windowManager = {
setTabData(state, {
parentUuid,
containerUuid,
contextKey = '',
searchValue = '',
currentRecordUuid = '',
recordsList = [],
selectionsList = [],
nextPageToken,
recordCount = 0,
isLoaded = true,
isLoadedContext = false,
pageNumber = 1
}) {
const dataTab = {
parentUuid,
containerUuid,
contextKey,
searchValue,
currentRecordUuid,
recordsList,
selectionsList,
nextPageToken,
recordCount,
isLoaded,
isLoadedContext,
pageNumber
}
Vue.set(state.tabData, containerUuid, dataTab)
Vue.set(state.oldTabData, containerUuid, dataTab)
},

setNewTabData(state, { parentUuid, containerUuid }) {
// current to old data
const currentAsOld = state.tabData[containerUuid]
Vue.set(state.oldTabData, containerUuid, currentAsOld)

// new empty as current
Vue.set(state.tabData, containerUuid, {
...state.emtpyTabData,
parentUuid,
containerUuid
})
},

setOldAsCurrentTabData(state, { containerUuid }) {
// set old to current data
const oldAsCurrent = state.oldTabData[containerUuid]
Vue.set(state.tabData, containerUuid, oldAsCurrent)

// new emty as old
Vue.set(state.oldTabData, containerUuid, {
...state.emtpyTabData,
containerUuid
})
},

setTabRow(state, { containerUuid, row, index }) {
Expand Down Expand Up @@ -101,7 +142,7 @@ const windowManager = {
containerUuid,
recordUuid
}) {
Vue.set(state.currentRecordOnPanel, containerUuid, recordUuid)
Vue.set(state.tabData[containerUuid], 'currentRecordUuid', recordUuid)
},

resetStateWindowManager(state) {
Expand Down Expand Up @@ -234,6 +275,7 @@ const windowManager = {
}
})

let currentRecordUuid
// update current record
if (!isEmptyValue(dataToStored)) {
let currentRow = dataToStored.at(0) // set first record
Expand All @@ -244,6 +286,7 @@ const windowManager = {
currentRow = recordFromUuid
}
}
currentRecordUuid = currentRow[UUID]

// remove datatables app attributes
for (const key in ROW_ATTRIBUTES) {
Expand Down Expand Up @@ -286,10 +329,13 @@ const windowManager = {
})
}

const contextKey = generateContextKey(contextAttributesList, 'key')
commit('setTabData', {
parentUuid,
containerUuid,
contextKey,
searchValue,
currentRecordUuid,
recordsList: dataToStored,
nextPageToken: dataResponse.nextPageToken,
pageNumber: currentPageNumber,
Expand All @@ -311,6 +357,35 @@ const windowManager = {
})
},

setOldAsCurrentTabData({ commit, dispatch, getters, rootGetters }, {
parentUuid,
containerUuid
}) {
const currentData = getters.getTabOldData({ containerUuid })

commit('setOldAsCurrentTabData', containerUuid)

if (!isEmptyValue(currentData.recordsList)) {
let currentRow = currentData.recordsList.at(0)
const currentRecordUuid = currentData.currentRecordUuid
if (!isEmptyValue(currentRecordUuid)) {
const recordFromUuid = currentData.recordsList.find(row => {
return row[UUID] === currentRecordUuid
})

if (!isEmptyValue(recordFromUuid)) {
currentRow = recordFromUuid
}
}

dispatch('updateValuesOfContainer', {
parentUuid,
containerUuid,
attributes: currentRow
})
}
},

/**
* Delete Entity panel
* @param {string} parentUuid
Expand Down Expand Up @@ -426,23 +501,36 @@ const windowManager = {
*/
getTabData: (state) => ({ containerUuid }) => {
return state.tabData[containerUuid] || {
parentUuid: undefined,
containerUuid,
searchValue: '',
recordsList: [],
selectionsList: [],
nextPageToken: undefined,
recordCount: 0,
isLoadedContext: false,
pageNumber: 1,
isLoaded: false
...state.emtpyTabData,
containerUuid
}
},
getTabOldData: (state) => ({ containerUuid }) => {
return state.oldTabData[containerUuid] || {
...state.emtpyTabData,
containerUuid
}
},
getIsLoadedTabRecord: (state, getters) => ({ containerUuid }) => {
return getters.getTabData({
containerUuid
}).isLoaded || false
},
getIsLoadedTabOldRecord: (state, getters) => ({ containerUuid }) => {
return getters.getTabOldData({
containerUuid
}).isLoaded || false
},
getTabContextKey: (state, getters) => ({ containerUuid }) => {
return getters.getTabData({
containerUuid
}).contextKey || ''
},
getTabOldContextKey: (state, getters) => ({ containerUuid }) => {
return getters.getTabOldData({
containerUuid
}).contextKey || ''
},
getSearchValueTabRecordsList: (state, getters) => ({ containerUuid }) => {
return getters.getTabData({
containerUuid
Expand Down Expand Up @@ -497,8 +585,15 @@ const windowManager = {
return undefined
},

getCurrentRecordOnPanel: (state) => (containerUuid) => {
return state.currentRecordOnPanel[containerUuid]
getCurrentRecordOnPanel: (state, getters) => (containerUuid) => {
return getters.getTabData({
containerUuid
}).currentRecordUuid
},
getOldRecordOnPanel: (state, getters) => (containerUuid) => {
return getters.getTabOldData({
containerUuid
}).currentRecordUuid
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/utils/ADempiere/contextUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,20 @@ export function getContextAttributes({
return contextAttributesList
}

export function generateContextKey(contextAttributes = []) {
/**
* Get context key based on attributes list
* @param {array} contextAttributes
* @param {string} keyName, default is 'columnName' or 'key'
* @returns {string} '_|key|value'
*/
export function generateContextKey(contextAttributes = [], keyName = 'columnName,') {
let contextKey = ''
if (isEmptyValue(contextAttributes)) {
return contextKey
}

contextAttributes.map(attribute => {
contextKey += '|' + attribute.columnName + '|' + attribute.value
contextAttributes.forEach(attribute => {
contextKey += '|' + attribute[keyName] + '|' + attribute.value
})
return '_' + contextKey
}
Loading

0 comments on commit ff96b14

Please sign in to comment.