fixed too small fractions dissappearing

This commit is contained in:
vabene1111 2022-04-17 15:35:32 +02:00
parent 0a0c0b069f
commit 2ce06a8154

View File

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