You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.9 KiB
55 lines
1.9 KiB
import getConfig from 'src/config'
|
|
|
|
import {
|
|
NotificationType,
|
|
Form,
|
|
FormValue,
|
|
Config,
|
|
ClassDictionary,
|
|
} from 'src/types'
|
|
|
|
export function notificationTypeToClassName(type: NotificationType): string {
|
|
switch (type) {
|
|
case NotificationType.Info: return 'is-info'
|
|
case NotificationType.Success: return 'is-success'
|
|
case NotificationType.Error: return 'is-danger'
|
|
case NotificationType.Welcome: return 'is-success'
|
|
}
|
|
}
|
|
|
|
export const objectToQuerystring = (obj: object) => Object.entries(obj).filter(([_, value]) => value !== undefined).map(([name, value]) => `${name}=${value}`).join('&')
|
|
|
|
export function setTitle(title: string, decorate: boolean = true) {
|
|
if (decorate) {
|
|
document.title = `${title} - Flexor`
|
|
} else {
|
|
document.title = title
|
|
}
|
|
}
|
|
|
|
export function valueFromForm<T extends FormValue>(form: Form, name: string): T | undefined
|
|
export function valueFromForm<T extends FormValue>(form: Form, name: string, defaultValue: T): T
|
|
export function valueFromForm<T extends FormValue>(form: Form, name: string, defaultValue?: T): T | undefined
|
|
export function valueFromForm<T extends FormValue>(form: Form, name: string, defaultValue?: T): T | undefined {
|
|
const field = form[name]
|
|
|
|
if (!field) return defaultValue
|
|
if (field.value === undefined) return defaultValue
|
|
|
|
return field.value as T
|
|
}
|
|
|
|
export const urlForBlob = (config: Config, name: string) => `${config.blobUrl}${name}`
|
|
|
|
export async function urlForBlobAsync(name: string) {
|
|
const config = await getConfig()
|
|
return urlForBlob(config, name)
|
|
}
|
|
|
|
export function getOrigin(url: string) {
|
|
const parser = document.createElement('a')
|
|
parser.href = url
|
|
return parser.origin
|
|
}
|
|
|
|
export const classNames = (dictionary: ClassDictionary) => Object.entries(dictionary).filter(([_, value]) => !!value).map(([key, _]) => key).join(' ')
|