generic api mixin

This commit is contained in:
smilerz
2021-08-24 17:30:09 -05:00
parent f7b2af7f97
commit 35dc981713
17 changed files with 288 additions and 247 deletions

View File

@@ -74,7 +74,7 @@
</div>
</div>
<!-- this should be made a generic component, would also require mixin for functions that generate the popup and put in parent container-->
<b-list-group ref="tooltip" variant="light" v-show="show_menu" v-on-clickaway="closeMenu" style="z-index:999; cursor:pointer">
<b-list-group ref="tooltip" variant="light" v-show="show_menu" v-on-clickaway="closeMenu" style="z-index:9999; cursor:pointer">
<b-list-group-item v-if="move" action v-on:click="$emit('item-action',{'action': 'move', 'target': model, 'source': source}); closeMenu()">
{{$t('Move')}}: {{$t('move_confirmation', {'child': source.name,'parent':model.name})}}
</b-list-group-item>
@@ -91,7 +91,6 @@
</template>
<script>
import {ApiApiFactory} from "@/utils/openapi/api.ts";
import GenericContextMenu from "@/components/GenericContextMenu";
import RecipeCard from "@/components/RecipeCard";
import { mixin as clickaway } from 'vue-clickaway';
@@ -111,9 +110,8 @@ export default {
children: {type: String, default: 'children'},
recipe_count: {type: String, default: 'numrecipe'},
recipes: {type: String, default: 'recipes'},
merge: {type: Boolean, default: false},
move: {type: Boolean, default: false},
tree: {type: Boolean, default: false},
merge: {type: Boolean, default: false},
},
data() {
return {
@@ -125,8 +123,8 @@ export default {
source: {'id': undefined, 'name': undefined},
target: {'id': undefined, 'name': undefined},
text: {
'hide_children': '',
}
'hide_children': '',
},
}
},
mounted() {
@@ -135,9 +133,6 @@ export default {
this.text.hide_children = this.$t('Hide_' + this.model_name)
},
methods: {
emitAction: function(m) {
},
handleDragStart: function(e) {
this.isError = false
e.dataTransfer.setData('source', JSON.stringify(this.model))
@@ -199,38 +194,6 @@ export default {
closeMenu: function(){
this.show_menu = false
},
deleteObject: function(id, model, callback) {
let apiClient = new ApiApiFactory()
let promise = apiClient['destroy' + model](id).then(() => {
}).catch((err) => {
console.log(err)
this.makeToast(this.$t('Error'), err.bodyText, 'danger')
})
callback(promise)
},
async listObjects(model, options) {
let apiClient = new ApiApiFactory()
let query = options?.query ?? ''
let page = options?.page ?? 1
let root = options?.root ?? undefined
let tree = options?.tree ?? undefined
let pageSize = options?.pageSize ?? 25
if (this.tree) {
if (query === '') {
query = undefined
root = 0
}
await apiClient.listFoods(query, root, tree, page, pageSize).then((result) => {
return result
})
} else {
await apiClient.listFoods(query, page, pageSize).then((result) => {
return result
})
}
}
}
}
</script>

View File

@@ -108,11 +108,6 @@ export default {
list_name: {type: String, default: 'Blank List'}, // TODO update translations to handle plural translations
left_list: {type:Array, default(){return []}},
right_list: {type:Array, default(){return []}},
load_more_left: {type: Boolean, default: false},
load_more_right: {type: Boolean, default: false},
//merge: {type: Boolean, default: false},
//move: {type: Boolean, default: false},
//action: Object
},
data() {
return {
@@ -120,8 +115,8 @@ export default {
show_split: false,
search_right: '',
search_left: '',
right_page: 1,
left_page: 1,
right_page: 0,
left_page: 0,
right: +new Date(),
left: +new Date(),
isDirtyright: false,
@@ -175,54 +170,17 @@ export default {
'page': (col==='left') ? this.left_page + 1 : this.right_page + 1,
'column': col
}
const result = new Promise((callback) => this.$emit('get-list', params, callback))
result.then((result) => {
console.log(result)
new Promise((callback) => this.$emit('get-list', params, callback)).then((result) => {
this[col+'_page']+=1
$state.loaded();
if (!this['load_more_' + col]) {
if (!result) { // callback needs to return true if handler should continue loading more data
$state.complete();
}
}).catch(() => {
$state.complete();
})
},
findCard: function(card_list, id){
let card_length = card_list?.length ?? 0
if (card_length == 0) {
return false
}
let cards = card_list.filter(obj => obj.id == id)
if (cards.length == 1) {
return cards[0]
} else if (cards.length == 0) {
for (const o of card_list.filter(o => o.show_children == true)) {
cards = this.findCard(o.children, id)
if (cards) {
return cards
}
}
} else {
console.log('something terrible happened')
}
},
destroyCard: function(id, cardList) {
let card = this.findCard(cardList, id)
let card_id = undefined
let p_id = card?.parent ?? undefined
if (p_id) {
let parent = this.findCard(cardList, p_id)
if (parent){
Vue.set(parent, 'numchild', parent.numchild - 1)
if (parent.show_children) {
let idx = parent.children.indexOf(parent.children.find(x => x.id === id))
Vue.delete(parent.children, idx)
}
}
}
return cardList.filter(kw => kw.id != id)
},
}
}