mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-02 04:39:54 -05:00
generic api mixin
This commit is contained in:
@@ -152,3 +152,217 @@ export function roundDecimals(num) {
|
||||
let decimals = ((getUserPreference('user_fractions')) ? getUserPreference('user_fractions') : 2);
|
||||
return +(Math.round(num + `e+${decimals}`) + `e-${decimals}`);
|
||||
}
|
||||
|
||||
/*
|
||||
* Utility functions to use OpenAPIs generically
|
||||
* */
|
||||
import {ApiApiFactory} from "@/utils/openapi/api.ts"; // TODO: is it possible to only import inside the Mixin?
|
||||
|
||||
import axios from "axios";
|
||||
axios.defaults.xsrfCookieName = 'csrftoken'
|
||||
axios.defaults.xsrfHeaderName = "X-CSRFTOKEN"
|
||||
export const ApiMixin = {
|
||||
data() {
|
||||
return {
|
||||
api_settings: {
|
||||
// TODO consider moving this to an API type dictionary that contains api types with details on the function call name, suffix, etc
|
||||
'suffix': { // if OpenApiGenerator adds a suffix to model name in function calls
|
||||
'list': 's'
|
||||
},
|
||||
// model specific settings, when not provided will use defaults instead
|
||||
'food': { // must be lowercase
|
||||
// *REQUIRED* name is name of the model used for translations and looking up APIFactory
|
||||
'name': 'Food',
|
||||
// *OPTIONAL: parameters will be built model -> model_type -> default
|
||||
'model_type': 'tree',
|
||||
// *OPTIONAL* model specific params for api, if not present will attempt modeltype_create then default_create
|
||||
// an array will create a dict of name:value pairs
|
||||
'create': [['name', 'description', 'recipe', 'ignore_shopping', 'supermarket_category']], // required: unordered array of fields that can be set during create
|
||||
// *OPTIONAL* model specific params for api, includes ordered list of parameters
|
||||
// and an unordered array that will be converted to a dictionary and passed as the 2nd param
|
||||
'partialUpdate': ['id', // required: ordered array of list params to patch existing object
|
||||
['name', 'description', 'recipe', 'ignore_shopping', 'supermarket_category'] // must include ordered array of field names that can be updated
|
||||
],
|
||||
// *OPTIONAL* provide model specific typing
|
||||
// 'typing': {},
|
||||
},
|
||||
'keyword': {},
|
||||
'unit': {},
|
||||
'recipe': {
|
||||
'name': 'Recipe',
|
||||
'list': ['query', 'keywords', 'foods', 'books', 'keywordsOr', 'foodsOr', 'booksOr', 'internal', 'random', '_new', 'page', 'pageSize', 'options'],
|
||||
'typing': {
|
||||
'list': {
|
||||
'foods': 'string',
|
||||
'keywords': 'string',
|
||||
'books': 'string',
|
||||
}
|
||||
},
|
||||
|
||||
},
|
||||
// collection of default attributes for model_type TREE. All settings except typing and values will be overwritten by model values
|
||||
'tree': {
|
||||
'values': {
|
||||
'root': 'getFunction()', // if default value is exactly 'getFunction()' will call getFunction(model_type, param, params)
|
||||
'tree': undefined,
|
||||
},
|
||||
'list': ['query', 'root', 'tree', 'page', 'pageSize'], // ordered array of list params for tree
|
||||
'typing': {
|
||||
'move': {
|
||||
'source': 'string',
|
||||
'target': 'string',
|
||||
}
|
||||
}
|
||||
},
|
||||
// collection of global defaults. All settings except typing and values will be overwritten by model_type or model values
|
||||
'default': {
|
||||
'list': ['query', 'page', 'pageSize'], // ordered array of list params for default listApi
|
||||
'destroy': ['id'], // ordered array of list params for default deleteApi
|
||||
'retrieve': ['id'], // ordered array of list params for default retrieveApi
|
||||
'merge': ['source', 'target'], // ordered array of list params for default mergeApi
|
||||
'move': ['source', 'target'], // ordered array of list params for default moveApi
|
||||
'create': [], // ordered array of list params for default createApi
|
||||
'partialUpdate': [], // ordered array of list params for default updateApi
|
||||
'values': {
|
||||
'query': undefined, // default values for list API
|
||||
'page': 1,
|
||||
'pageSize': 25,
|
||||
},
|
||||
'typing': { // optional settings to force type on parameters
|
||||
'merge': {
|
||||
'source': 'string',
|
||||
'target': 'string',
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
/**
|
||||
* constructs OpenAPI Generator function using named parameters
|
||||
* @param {string} model string to define which model API to use
|
||||
* @param {string} api string to define which of the API functions to use
|
||||
* @param {object} options dictionary to define all of the parameters necessary to use API
|
||||
*/
|
||||
genericAPI: function(model, api, options) {
|
||||
model = model.toLowerCase()
|
||||
// construct settings for this model and this api - values are assigned in order (default is overwritten by api overwritten by model)
|
||||
let settings = {...this.api_settings?.default ?? {}, ...this.api_settings?.[this.api_settings?.[model]?.model_type] ?? {}, ...this.api_settings[model]};
|
||||
// values and typing are also dicts and need to be merged,
|
||||
settings.values = {...this.api_settings?.default?.values ?? {},
|
||||
...this.api_settings?.[this.api_settings?.[model]?.model_type]?.values ?? {},
|
||||
...this.api_settings[model].values}
|
||||
settings.typing = {...this.api_settings?.default?.typing?.[api] ?? {},
|
||||
...this.api_settings?.[this.api_settings?.[model]?.model_type]?.typing?.[api] ?? {},
|
||||
...this.api_settings[model].typing?.[api]}
|
||||
let func = api + settings.name + (this.api_settings.suffix?.[api] ?? '')
|
||||
// does model params exist?
|
||||
let params = []
|
||||
let this_value = undefined
|
||||
settings[api].forEach(function (item, index) {
|
||||
|
||||
if (Array.isArray(item)) {
|
||||
this_value = {}
|
||||
for (const [k, v] of Object.entries(options)) { // filters options dict based on valus in array, I'm sure there's a better way to do this
|
||||
if (item.includes(k)) {
|
||||
this_value[k] = v
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this_value = options?.[item] ?? settings.values?.[item] ?? undefined // set the value or apply default
|
||||
}
|
||||
if (this_value ==='getFunction()') {
|
||||
this_value = getFunction(item, settings?.model_type, options)
|
||||
}
|
||||
if (Object.keys(settings?.typing).includes(item)) {
|
||||
switch (settings.typing[item]) {
|
||||
case 'string':
|
||||
if (this_value) {this_value = String(this_value)}
|
||||
break;
|
||||
}
|
||||
}
|
||||
params.push(this_value)
|
||||
});
|
||||
|
||||
let apiClient = new ApiApiFactory()
|
||||
return apiClient[func](...params)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Utility functions to calculate default value
|
||||
* */
|
||||
export function getFunction(item, model_type, params) {
|
||||
if (item==='root' && model_type==='tree') {
|
||||
if ((!params?.query ?? undefined) || params?.query?.length == 0) {
|
||||
return 0
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// /*
|
||||
// * Utility functions to use manipulate nested components
|
||||
// * */
|
||||
import Vue from 'vue'
|
||||
export const CardMixin = {
|
||||
methods: {
|
||||
findCard: function(id, card_list){
|
||||
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 c of card_list.filter(x => x.show_children == true)) {
|
||||
cards = this.findCard(id, c.children)
|
||||
if (cards) {
|
||||
return cards
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log('something terrible happened')
|
||||
}
|
||||
},
|
||||
destroyCard: function(id, card_list) {
|
||||
let card = this.findCard(id, card_list)
|
||||
let p_id = card?.parent ?? undefined
|
||||
|
||||
if (p_id) {
|
||||
let parent = this.findCard(p_id, card_list)
|
||||
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 card_list.filter(x => x.id != id)
|
||||
},
|
||||
refreshCard: function(obj, card_list){
|
||||
let target = {}
|
||||
let idx = undefined
|
||||
target = this.findCard(obj.id, card_list)
|
||||
|
||||
if (target.parent) {
|
||||
let parent = this.findCard(target.parent, card_list)
|
||||
if (parent) {
|
||||
if (parent.show_children){
|
||||
idx = parent.children.indexOf(parent.children.find(x => x.id === target.id))
|
||||
Vue.set(parent.children, idx, obj)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
idx = card_list.indexOf(card_list.find(x => x.id === target.id))
|
||||
console.log(card_list, idx, obj)
|
||||
Vue.set(card_list, idx, obj)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user