edit SupermarketCategoryRelations
This commit is contained in:
@ -256,7 +256,11 @@ export default {
|
||||
})
|
||||
} else {
|
||||
this.genericAPI(this.this_model, this.Actions.UPDATE, thisItem).then((result) => {
|
||||
this.refreshThis(thisItem.id)
|
||||
// using form data to refresh the card
|
||||
// when there are complicated functions (SuperMarket Relations) the actions don't
|
||||
// always complete first. TODO: wrap all that in a Promise and wait for it to complete before using refreshThis instead
|
||||
this.refreshCard(thisItem, this.items_left)
|
||||
this.refreshCard({...thisItem}, this.items_right)
|
||||
StandardToasts.makeStandardToast(StandardToasts.SUCCESS_UPDATE)
|
||||
}).catch((err) => {
|
||||
console.log(err, err.response)
|
||||
|
@ -6,12 +6,8 @@
|
||||
<p v-if="f.type=='instruction'">{{f.label}}</p>
|
||||
<!-- this lookup is single selection -->
|
||||
<lookup-input v-if="f.type=='lookup'"
|
||||
:label="f.label"
|
||||
:value="f.value"
|
||||
:field="f.field"
|
||||
:form="f"
|
||||
:model="listModel(f.list)"
|
||||
:sticky_options="f.sticky_options || undefined"
|
||||
:create_new="f.allow_create"
|
||||
@change="storeValue"/> <!-- TODO add ability to create new items associated with lookup -->
|
||||
<!-- TODO: add multi-selection input list -->
|
||||
<checkbox-input v-if="f.type=='checkbox'"
|
||||
@ -65,7 +61,8 @@ export default {
|
||||
id: undefined,
|
||||
form_data: {},
|
||||
form: {},
|
||||
dirty: false
|
||||
dirty: false,
|
||||
special_handling: false
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
@ -93,7 +90,7 @@ export default {
|
||||
methods: {
|
||||
doAction: function(){
|
||||
this.dirty = false
|
||||
this.$emit('finish-action', {'form_data': this.form_data })
|
||||
this.$emit('finish-action', {'form_data': this.detectOverride(this.form_data) })
|
||||
},
|
||||
cancelAction: function() {
|
||||
if (this.dirty) {
|
||||
@ -110,6 +107,14 @@ export default {
|
||||
} else {
|
||||
return Models[m]
|
||||
}
|
||||
},
|
||||
detectOverride: function(form) {
|
||||
for (const [k, v] of Object.entries(form)) {
|
||||
if (form[k].__override__) {
|
||||
form[k] = form[k].__override__
|
||||
}
|
||||
}
|
||||
return form
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,14 @@
|
||||
<template>
|
||||
<div>
|
||||
<b-form-group
|
||||
v-bind:label="label"
|
||||
v-bind:label="form.label"
|
||||
class="mb-3">
|
||||
<generic-multiselect
|
||||
@change="new_value=$event.val"
|
||||
@remove="new_value=undefined"
|
||||
:initial_selection="initialSelection"
|
||||
:model="model"
|
||||
:multiple="false"
|
||||
:multiple="useMultiple"
|
||||
:sticky_options="sticky_options"
|
||||
:allow_create="create_new"
|
||||
:create_placeholder="createPlaceholder"
|
||||
@ -29,12 +29,9 @@ export default {
|
||||
components: {GenericMultiselect},
|
||||
mixins: [ApiMixin],
|
||||
props: {
|
||||
field: {type: String, default: 'You Forgot To Set Field Name'},
|
||||
label: {type: String, default: ''},
|
||||
value: {type: Object, default () {return undefined}},
|
||||
form: {type: Object, default () {return undefined}},
|
||||
model: {type: Object, default () {return undefined}},
|
||||
create_new: {type: Boolean, default: false},
|
||||
sticky_options: {type:Array, default(){return []}},
|
||||
|
||||
// TODO: include create_new and create_text props and associated functionality to create objects for drop down
|
||||
// see 'tagging' here: https://vue-multiselect.js.org/#sub-tagging
|
||||
// perfect world would have it trigger a new modal associated with the associated item model
|
||||
@ -42,25 +39,43 @@ export default {
|
||||
data() {
|
||||
return {
|
||||
new_value: undefined,
|
||||
field: undefined,
|
||||
label: undefined,
|
||||
sticky_options: undefined,
|
||||
first_run: true
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.new_value = this.value
|
||||
this.new_value = this.form?.value
|
||||
this.field = this.form?.field ?? 'You Forgot To Set Field Name'
|
||||
this.label = this.form?.label ?? ''
|
||||
this.sticky_options = this.form?.sticky_options ?? []
|
||||
},
|
||||
computed: {
|
||||
modelName() {
|
||||
return this?.model?.name ?? this.$t('Search')
|
||||
},
|
||||
useMultiple() {
|
||||
return this.form?.multiple || this.form?.ordered || false
|
||||
},
|
||||
initialSelection() {
|
||||
let this_value = this.form.value
|
||||
let arrayValues = undefined
|
||||
// multiselect is expect to get an array of objects - make sure it gets one
|
||||
if (Array.isArray(this.new_value)) {
|
||||
return this.new_value
|
||||
} else if (!this.new_value) {
|
||||
return []
|
||||
} else if (typeof(this.new_value) === 'object') {
|
||||
return [this.new_value]
|
||||
if (Array.isArray(this_value)) {
|
||||
arrayValues = this_value
|
||||
} else if (!this_value) {
|
||||
arrayValues = []
|
||||
} else if (typeof(this_value) === 'object') {
|
||||
arrayValues = [this_value]
|
||||
} else {
|
||||
return [{'id': -1, 'name': this.new_value}]
|
||||
arrayValues = [{'id': -1, 'name': this_value}]
|
||||
}
|
||||
|
||||
if (this.form?.ordered && this.first_run) {
|
||||
return this.flattenItems(arrayValues)
|
||||
} else {
|
||||
return arrayValues
|
||||
}
|
||||
},
|
||||
createPlaceholder() {
|
||||
@ -69,7 +84,10 @@ export default {
|
||||
},
|
||||
watch: {
|
||||
'new_value': function () {
|
||||
this.$root.$emit('change', this.field, this.new_value ?? null)
|
||||
let x = this?.new_value
|
||||
// pass the unflattened attributes that can be restored when ready to save/update
|
||||
x['__override__'] = this.unflattenItem(this?.new_value)
|
||||
this.$root.$emit('change', this.form.field, x)
|
||||
},
|
||||
},
|
||||
methods: {
|
||||
@ -84,6 +102,54 @@ export default {
|
||||
StandardToasts.makeStandardToast(StandardToasts.FAIL_CREATE)
|
||||
})
|
||||
},
|
||||
// ordered lookups have nested attributes that need flattened attributes to drive lookup
|
||||
flattenItems: function(itemlist) {
|
||||
let flat_items = []
|
||||
let item = undefined
|
||||
let label = this.form.list_label.split('::')
|
||||
itemlist.forEach(x => {
|
||||
item = {}
|
||||
for (const [k, v] of Object.entries(x)) {
|
||||
if (k == label[0]) {
|
||||
item['id'] = v.id
|
||||
item[label[1]] = v[label[1]]
|
||||
} else {
|
||||
item[this.form.field + '__' + k] = v
|
||||
}
|
||||
}
|
||||
flat_items.push(item)
|
||||
});
|
||||
this.first_run = false
|
||||
return flat_items
|
||||
},
|
||||
unflattenItem: function(itemList) {
|
||||
let unflat_items = []
|
||||
let item = undefined
|
||||
let this_label = undefined
|
||||
let label = this.form.list_label.split('::')
|
||||
let order = 0
|
||||
itemList.forEach(x => {
|
||||
item = {}
|
||||
item[label[0]] = {}
|
||||
for (const [k, v] of Object.entries(x)) {
|
||||
switch(k) {
|
||||
case 'id':
|
||||
item[label[0]]['id'] = v
|
||||
break;
|
||||
case label[1]:
|
||||
item[label[0]][label[1]] = v
|
||||
break;
|
||||
default:
|
||||
this_label = k.replace(this.form.field + '__', '')
|
||||
}
|
||||
|
||||
}
|
||||
item['order'] = order
|
||||
order++
|
||||
unflat_items.push(item)
|
||||
});
|
||||
return unflat_items
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
0
vue/src/components/Modals/OrderedLookup.vue
Normal file
0
vue/src/components/Modals/OrderedLookup.vue
Normal file
@ -229,8 +229,8 @@ export class Models {
|
||||
},
|
||||
}
|
||||
static SHOPPING_CATEGORY_RELATION = {
|
||||
'name': i18n.t('Shopping_Category'),
|
||||
'apiName': 'SupermarketCategory',
|
||||
'name': i18n.t('Shopping_Category_Relation'),
|
||||
'apiName': 'SupermarketCategoryRelation',
|
||||
'create': {
|
||||
'params': [['category', 'supermarket', 'order']],
|
||||
'form': {
|
||||
@ -272,6 +272,19 @@ export class Models {
|
||||
'label': i18n.t('Description'),
|
||||
'placeholder': ''
|
||||
},
|
||||
'categories': {
|
||||
'form_field': true,
|
||||
'type': 'lookup',
|
||||
'list': 'SHOPPING_CATEGORY',
|
||||
'list_label': 'category::name',
|
||||
'ordered': true, // ordered lookups assume working with relation field
|
||||
'field': 'category_to_supermarket',
|
||||
'label': i18n.t('Categories'),
|
||||
'placeholder': ''
|
||||
},
|
||||
},
|
||||
'config': {
|
||||
'category_to_supermarket': {'function': 'handleSuperMarketCategory'}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
@ -189,11 +189,11 @@ export const ApiMixin = {
|
||||
// maybe map/reduce is better?
|
||||
for (const [k, v] of Object.entries(options)) {
|
||||
if (item.includes(k)) {
|
||||
this_value[k] = formatParam(config?.[k], v)
|
||||
this_value[k] = formatParam(config?.[k], v, options)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this_value = formatParam(config?.[item], options?.[item] ?? undefined)
|
||||
this_value = formatParam(config?.[item], options?.[item] ?? undefined, options)
|
||||
}
|
||||
// if no value is found so far, get the default if it exists
|
||||
if (this_value === undefined) {
|
||||
@ -210,7 +210,7 @@ export const ApiMixin = {
|
||||
// /*
|
||||
// * local functions for ApiMixin
|
||||
// * */
|
||||
function formatParam(config, value) {
|
||||
function formatParam(config, value, options) {
|
||||
if (config) {
|
||||
for (const [k, v] of Object.entries(config)) {
|
||||
switch(k) {
|
||||
@ -236,6 +236,10 @@ function formatParam(config, value) {
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 'function':
|
||||
// needs wrapped in a promise and wait for the called function to complete before moving on
|
||||
specialCases[v](value, options)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -272,6 +276,7 @@ function getDefault(config, options) {
|
||||
return value
|
||||
}
|
||||
function getConfig(model, action) {
|
||||
|
||||
let f = action.function
|
||||
// if not defined partialUpdate will use params from create
|
||||
if (f === 'partialUpdate' && !model?.[f]?.params) {
|
||||
@ -286,6 +291,10 @@ function getConfig(model, action) {
|
||||
config = {...config, ...action, ...model.model_type?.[f], ...model?.[f]}
|
||||
// nested dictionaries are not merged - so merge again on any nested keys
|
||||
config.config = {...action?.config, ...model.model_type?.[f]?.config, ...model?.[f]?.config}
|
||||
// look in partialUpdate again if necessary
|
||||
if (f === 'partialUpdate' && Object.keys(config.config).length === 0) {
|
||||
config.config = {...model.model_type?.create?.config, ...model?.create?.config}
|
||||
}
|
||||
config['function'] = f + config.apiName + (config?.suffix ?? '') // parens are required to force optional chaining to evaluate before concat
|
||||
return config
|
||||
}
|
||||
@ -415,3 +424,31 @@ export const CardMixin = {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const specialCases = {
|
||||
handleSuperMarketCategory: function(updatedRelationships, supermarket) {
|
||||
let API = ApiMixin.methods.genericAPI
|
||||
if (updatedRelationships.length === 0) {
|
||||
return
|
||||
}
|
||||
// get current relationship mappings
|
||||
API(Models.SUPERMARKET, Actions.FETCH, {'id': supermarket.id}).then((result) => {
|
||||
let currentRelationships = result.data.category_to_supermarket
|
||||
let removed = currentRelationships.map(x => x.id).filter(x => !updatedRelationships.map(x => x.id).includes(x))
|
||||
removed.forEach(x => {
|
||||
API(Models.SHOPPING_CATEGORY_RELATION, Actions.DELETE, {'id': x})//.then((result)=> console.log('delete', result))
|
||||
})
|
||||
let item = {'supermarket': supermarket.id}
|
||||
updatedRelationships.forEach(x => {
|
||||
item.order = x.order
|
||||
item.category = {'id': x.category.id, 'name': x.category.name}
|
||||
if (x.id) {
|
||||
item.id = x.id
|
||||
API(Models.SHOPPING_CATEGORY_RELATION, Actions.UPDATE, item)//.then((result)=> console.log('update', result))
|
||||
} else {
|
||||
API(Models.SHOPPING_CATEGORY_RELATION, Actions.CREATE, item)//.then((result)=> console.log('create', result))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user