diff --git a/vue/src/utils/utils.js b/vue/src/utils/utils.js index f9f550c9..88c2b790 100644 --- a/vue/src/utils/utils.js +++ b/vue/src/utils/utils.js @@ -2,18 +2,18 @@ * Utility functions to call bootstrap toasts * */ import i18n from "@/i18n" -import { frac } from "@/utils/fractions" +import {frac} from "@/utils/fractions" /* * Utility functions to use OpenAPIs generically * */ -import { ApiApiFactory } from "@/utils/openapi/api.ts" +import {ApiApiFactory} from "@/utils/openapi/api.ts" import axios from "axios" -import { BToast } from "bootstrap-vue" +import {BToast} from "bootstrap-vue" // /* // * Utility functions to use manipulate nested components // * */ import Vue from "vue" -import { Actions, Models } from "./models" +import {Actions, Models} from "./models" export const ToastMixin = { name: "ToastMixin", @@ -170,6 +170,10 @@ export function calculateAmount(amount, factor) { let return_string = "" let fraction = frac(amount * factor, 10, true) + if (fraction[0] === 0 && fraction[1] === 0 && fraction[2] === 1) { + return roundDecimals(amount * factor) + } + if (fraction[0] > 0) { return_string += fraction[0] } @@ -238,7 +242,7 @@ export const ApiMixin = { return apiClient[func](...parameters) }, genericGetAPI: function (url, options) { - return axios.get(resolveDjangoUrl(url), { params: options, emulateJSON: true }) + return axios.get(resolveDjangoUrl(url), {params: options, emulateJSON: true}) }, genericPostAPI: function (url, form) { let data = new FormData() @@ -288,6 +292,7 @@ function formatParam(config, value, options) { } return value } + function buildParams(options, setup) { let config = setup?.config ?? {} let params = setup?.params ?? [] @@ -315,6 +320,7 @@ function buildParams(options, setup) { }) return parameters } + function getDefault(config, options) { let value = undefined value = config?.default ?? undefined @@ -342,11 +348,12 @@ function getDefault(config, options) { } return value } + export function getConfig(model, action) { let f = action.function // if not defined partialUpdate will use params from create if (f === "partialUpdate" && !model?.[f]?.params) { - model[f] = { params: [...["id"], ...model.create.params] } + model[f] = {params: [...["id"], ...model.create.params]} } let config = { @@ -354,12 +361,12 @@ export function getConfig(model, action) { apiName: model.apiName, } // spread operator merges dictionaries - last item in list takes precedence - config = { ...config, ...action, ...model.model_type?.[f], ...model?.[f] } + 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 } + 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.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 @@ -370,17 +377,17 @@ export function getConfig(model, action) { // * */ export function getForm(model, action, item1, item2) { let f = action.function - let config = { ...action?.form, ...model.model_type?.[f]?.form, ...model?.[f]?.form } + let config = {...action?.form, ...model.model_type?.[f]?.form, ...model?.[f]?.form} // if not defined partialUpdate will use form from create if (f === "partialUpdate" && Object.keys(config).length == 0) { - config = { ...Actions.CREATE?.form, ...model.model_type?.["create"]?.form, ...model?.["create"]?.form } - config["title"] = { ...action?.form_title, ...model.model_type?.[f]?.form_title, ...model?.[f]?.form_title } + config = {...Actions.CREATE?.form, ...model.model_type?.["create"]?.form, ...model?.["create"]?.form} + config["title"] = {...action?.form_title, ...model.model_type?.[f]?.form_title, ...model?.[f]?.form_title} // form functions should not be inherited if (config?.["form_function"]?.includes("Create")) { delete config["form_function"] } } - let form = { fields: [] } + let form = {fields: []} let value = "" for (const [k, v] of Object.entries(config)) { if (v?.function) { @@ -411,6 +418,7 @@ export function getForm(model, action, item1, item2) { } return form } + function formTranslate(translate, model, item1, item2) { if (typeof translate !== "object") { return i18n.t(translate) @@ -510,7 +518,7 @@ const specialCases = { let params = [] if (action.function === "partialUpdate") { API = GenericAPI - params = [Models.SUPERMARKET, Actions.FETCH, { id: options.id }] + params = [Models.SUPERMARKET, Actions.FETCH, {id: options.id}] } else if (action.function === "create") { API = new ApiApiFactory()[setup.function] params = buildParams(options, setup) @@ -547,15 +555,15 @@ const specialCases = { let order = Math.max(...existing_categories.map((x) => x?.order ?? 0), ...updated_categories.map((x) => x?.order ?? 0), 0) + 1 removed_categories.forEach((x) => { - promises.push(GenericAPI(Models.SHOPPING_CATEGORY_RELATION, Actions.DELETE, { id: x.id })) + promises.push(GenericAPI(Models.SHOPPING_CATEGORY_RELATION, Actions.DELETE, {id: x.id})) }) - let item = { supermarket: id } + let item = {supermarket: id} added_categories.forEach((x) => { item.order = x?.order ?? order if (!x?.order) { order = order + 1 } - item.category = { id: x.category.id, name: x.category.name } + item.category = {id: x.category.id, name: x.category.name} promises.push(GenericAPI(Models.SHOPPING_CATEGORY_RELATION, Actions.CREATE, item)) }) changed_categories.forEach((x) => { @@ -564,13 +572,13 @@ const specialCases = { if (!x?.order) { order = order + 1 } - item.category = { id: x.category.id, name: x.category.name } + item.category = {id: x.category.id, name: x.category.name} promises.push(GenericAPI(Models.SHOPPING_CATEGORY_RELATION, Actions.UPDATE, item)) }) return Promise.all(promises).then(() => { // finally get and return the Supermarket which everything downstream is expecting - return GenericAPI(Models.SUPERMARKET, Actions.FETCH, { id: id }) + return GenericAPI(Models.SUPERMARKET, Actions.FETCH, {id: id}) }) }) },