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 {
|
import {
|
||||||
isObjectLiteral,
|
|
||||||
isEmptyValue,
|
|
||||||
flattenObjectLiteral,
|
flattenObjectLiteral,
|
||||||
getFileContent,
|
getFileContent,
|
||||||
|
isEmptyValue,
|
||||||
|
isObjectLiteral,
|
||||||
} from '@/helpers/commons'
|
} from '@/helpers/commons'
|
||||||
|
import store from '@/store'
|
||||||
const NO_VALUE_FIELDS = [
|
import type { Translation } from '@/types/commons'
|
||||||
'FormFieldReadonly',
|
import type { AdressModelValue } from '@/types/form'
|
||||||
'ReadOnlyAlertItem',
|
|
||||||
'MarkdownItem',
|
|
||||||
'DisplayTextItem',
|
|
||||||
'ButtonItem',
|
|
||||||
]
|
|
||||||
|
|
||||||
export const DEFAULT_STATUS_ICON = {
|
export const DEFAULT_STATUS_ICON = {
|
||||||
[null]: null,
|
[null]: null,
|
||||||
|
@ -26,29 +17,35 @@ export const DEFAULT_STATUS_ICON = {
|
||||||
warning: 'warning',
|
warning: 'warning',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FORMAT FROM CORE
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tries to find a translation corresponding to the user's locale/fallback locale in a
|
* 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.
|
* 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
|
* @param field - A field value containing a translation object or string
|
||||||
* @return {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
|
if (typeof field === 'string') return field
|
||||||
const { locale, fallbackLocale } = store.state
|
const { locale, fallbackLocale } = store.state as {
|
||||||
return field ? field[locale] || field[fallbackLocale] || field.en : ''
|
locale: string
|
||||||
|
fallbackLocale: string
|
||||||
|
}
|
||||||
|
return field[locale] || field[fallbackLocale] || field.en || ''
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a string size declaration to a M value.
|
* Returns a string size declaration to a M value.
|
||||||
*
|
*
|
||||||
* @param {String} sizeStr - A size declared like '500M' or '56k'
|
* @param size - A size declared like '500M' or '56k'
|
||||||
* @return {Number}
|
* @return a number in M
|
||||||
*/
|
*/
|
||||||
export function sizeToM(sizeStr) {
|
export function sizeToM(size: string) {
|
||||||
const unit = sizeStr.slice(-1)
|
const unit = size.slice(-1)
|
||||||
const value = sizeStr.slice(0, -1)
|
const value = parseInt(size.slice(0, -1))
|
||||||
if (unit === 'M') return parseInt(value)
|
if (unit === 'M') return value
|
||||||
if (unit === 'b') return Math.ceil(value / (1024 * 1024))
|
if (unit === 'b') return Math.ceil(value / (1024 * 1024))
|
||||||
if (unit === 'k') return Math.ceil(value / 1024)
|
if (unit === 'k') return Math.ceil(value / 1024)
|
||||||
if (unit === 'G') 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)
|
* @param address - A string representing an adress (subdomain or email)
|
||||||
* @return {Object} - `{ localPart, separator, domain }`.
|
* @return Parsed address as `AdressModelValue`
|
||||||
*/
|
*/
|
||||||
export function adressToFormValue(address) {
|
export function formatAdress(address: string): AdressModelValue {
|
||||||
const separator = address.includes('@') ? '@' : '.'
|
const separator = address.includes('@') ? '@' : '.'
|
||||||
const [localPart, domain] = address.split(separator)
|
const [localPart, domain] = address.split(separator)
|
||||||
return { localPart, separator, domain }
|
return { localPart, separator, domain }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FORMAT TO CORE
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse a front-end value to its API equivalent. This function returns a Promise or an
|
* 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
|
* Object `{ key: Promise }` if `key` is supplied. When parsing a form, all those
|
||||||
|
|
|
@ -19,7 +19,7 @@ import {
|
||||||
sameAs,
|
sameAs,
|
||||||
} from '@/helpers/validators'
|
} from '@/helpers/validators'
|
||||||
import {
|
import {
|
||||||
adressToFormValue,
|
formatAdress,
|
||||||
formatFormData,
|
formatFormData,
|
||||||
sizeToM,
|
sizeToM,
|
||||||
} from '@/helpers/yunohostArguments'
|
} from '@/helpers/yunohostArguments'
|
||||||
|
@ -154,10 +154,10 @@ const { v, onSubmit } = useForm(form, fields)
|
||||||
|
|
||||||
function onQueriesResponse(user_: any) {
|
function onQueriesResponse(user_: any) {
|
||||||
form.value.fullname = user_.fullname
|
form.value.fullname = user_.fullname
|
||||||
form.value.mail = adressToFormValue(user_.mail)
|
form.value.mail = formatAdress(user_.mail)
|
||||||
if (user_['mail-aliases']) {
|
if (user_['mail-aliases']) {
|
||||||
form.value.mail_aliases = user_['mail-aliases'].map((mail) =>
|
form.value.mail_aliases = user_['mail-aliases'].map((mail) =>
|
||||||
adressToFormValue(mail),
|
formatAdress(mail),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if (user_['mail-forward']) {
|
if (user_['mail-forward']) {
|
||||||
|
|
Loading…
Reference in a new issue