mirror of
https://github.com/YunoHost/yunohost-admin.git
synced 2024-09-03 20:06:15 +02:00
ts: type core data formating functions
This commit is contained in:
parent
016acc7b50
commit
ab30cb8c04
2 changed files with 31 additions and 32 deletions
|
@ -1,21 +1,12 @@
|
|||
import i18n from '@/i18n'
|
||||
import store from '@/store'
|
||||
import evaluate from 'simple-evaluate'
|
||||
import * as validators from '@/helpers/validators'
|
||||
import {
|
||||
isObjectLiteral,
|
||||
isEmptyValue,
|
||||
flattenObjectLiteral,
|
||||
getFileContent,
|
||||
isEmptyValue,
|
||||
isObjectLiteral,
|
||||
} from '@/helpers/commons'
|
||||
|
||||
const NO_VALUE_FIELDS = [
|
||||
'FormFieldReadonly',
|
||||
'ReadOnlyAlertItem',
|
||||
'MarkdownItem',
|
||||
'DisplayTextItem',
|
||||
'ButtonItem',
|
||||
]
|
||||
import store from '@/store'
|
||||
import type { Translation } from '@/types/commons'
|
||||
import type { AdressModelValue } from '@/types/form'
|
||||
|
||||
export const DEFAULT_STATUS_ICON = {
|
||||
[null]: null,
|
||||
|
@ -26,29 +17,35 @@ export const DEFAULT_STATUS_ICON = {
|
|||
warning: 'warning',
|
||||
}
|
||||
|
||||
// FORMAT FROM CORE
|
||||
|
||||
/**
|
||||
* Tries to find a translation corresponding to the user's locale/fallback locale in a
|
||||
* Yunohost argument or simply return the string if it's not an object literal.
|
||||
*
|
||||
* @param {(Object|String|undefined)} field - A field value containing a translation object or string
|
||||
* @return {String}
|
||||
* @param field - A field value containing a translation object or string
|
||||
* @return translated field or empty string
|
||||
*/
|
||||
export function formatI18nField(field) {
|
||||
export function formatI18nField(field?: Translation): string {
|
||||
if (!field) return ''
|
||||
if (typeof field === 'string') return field
|
||||
const { locale, fallbackLocale } = store.state
|
||||
return field ? field[locale] || field[fallbackLocale] || field.en : ''
|
||||
const { locale, fallbackLocale } = store.state as {
|
||||
locale: string
|
||||
fallbackLocale: string
|
||||
}
|
||||
return field[locale] || field[fallbackLocale] || field.en || ''
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string size declaration to a M value.
|
||||
*
|
||||
* @param {String} sizeStr - A size declared like '500M' or '56k'
|
||||
* @return {Number}
|
||||
* @param size - A size declared like '500M' or '56k'
|
||||
* @return a number in M
|
||||
*/
|
||||
export function sizeToM(sizeStr) {
|
||||
const unit = sizeStr.slice(-1)
|
||||
const value = sizeStr.slice(0, -1)
|
||||
if (unit === 'M') return parseInt(value)
|
||||
export function sizeToM(size: string) {
|
||||
const unit = size.slice(-1)
|
||||
const value = parseInt(size.slice(0, -1))
|
||||
if (unit === 'M') return value
|
||||
if (unit === 'b') return Math.ceil(value / (1024 * 1024))
|
||||
if (unit === 'k') return Math.ceil(value / 1024)
|
||||
if (unit === 'G') return Math.ceil(value * 1024)
|
||||
|
@ -56,17 +53,19 @@ export function sizeToM(sizeStr) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns a formatted address element to be used by AdressItem component.
|
||||
* Returns an address as AdressModelValue to be used by AdressItem component.
|
||||
*
|
||||
* @param {String} address - A string representing an adress (subdomain or email)
|
||||
* @return {Object} - `{ localPart, separator, domain }`.
|
||||
* @param address - A string representing an adress (subdomain or email)
|
||||
* @return Parsed address as `AdressModelValue`
|
||||
*/
|
||||
export function adressToFormValue(address) {
|
||||
export function formatAdress(address: string): AdressModelValue {
|
||||
const separator = address.includes('@') ? '@' : '.'
|
||||
const [localPart, domain] = address.split(separator)
|
||||
return { localPart, separator, domain }
|
||||
}
|
||||
|
||||
// FORMAT TO CORE
|
||||
|
||||
/**
|
||||
* Parse a front-end value to its API equivalent. This function returns a Promise or an
|
||||
* Object `{ key: Promise }` if `key` is supplied. When parsing a form, all those
|
||||
|
|
|
@ -19,7 +19,7 @@ import {
|
|||
sameAs,
|
||||
} from '@/helpers/validators'
|
||||
import {
|
||||
adressToFormValue,
|
||||
formatAdress,
|
||||
formatFormData,
|
||||
sizeToM,
|
||||
} from '@/helpers/yunohostArguments'
|
||||
|
@ -154,10 +154,10 @@ const { v, onSubmit } = useForm(form, fields)
|
|||
|
||||
function onQueriesResponse(user_: any) {
|
||||
form.value.fullname = user_.fullname
|
||||
form.value.mail = adressToFormValue(user_.mail)
|
||||
form.value.mail = formatAdress(user_.mail)
|
||||
if (user_['mail-aliases']) {
|
||||
form.value.mail_aliases = user_['mail-aliases'].map((mail) =>
|
||||
adressToFormValue(mail),
|
||||
formatAdress(mail),
|
||||
)
|
||||
}
|
||||
if (user_['mail-forward']) {
|
||||
|
|
Loading…
Reference in a new issue