Skip to content
Snippets Groups Projects
utils.ts 1.92 KiB
Newer Older
  • Learn to ignore specific revisions
  • import get from 'lodash/get'
    import { Relation } from 'models'
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    import { FluidType } from '../enum/fluid.enum'
    
    import { KonnectorUpdate } from '../enum/konnectorUpdate.enum'
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    
    
    export function getFluidType(type: string) {
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      switch (type.toUpperCase()) {
        case 'ELECTRICITY':
          return FluidType.ELECTRICITY
        case 'WATER':
          return FluidType.WATER
        case 'GAS':
          return FluidType.GAS
        default:
          return FluidType.ELECTRICITY
      }
    }
    
    export function getKonnectorUpdateError(type: string) {
      switch (type.toUpperCase()) {
        case 'USER_ACTION_NEEDED.OAUTH_OUTDATED':
          return KonnectorUpdate.ERROR_UPDATE_OAUTH
        default:
          return KonnectorUpdate.ERROR_UPDATE
      }
    }
    
    export function formatNumberValues(
    
      value: number | null,
    
      fluidStyle?: string,
      toBeCompared = false
    ) {
    
    Romain CREY's avatar
    Romain CREY committed
      if (value || value === 0) {
    
        const localeValue = value.toLocaleString('fr-FR', {
    
          minimumFractionDigits: 2,
          maximumFractionDigits: 2,
        })
    
        const noSpaceValue = parseInt(localeValue.replace(/\s/g, ''))
        if (toBeCompared) return noSpaceValue
        if (fluidStyle && noSpaceValue >= 1000) {
          const convertedValue = (noSpaceValue / 1000).toFixed(2).replace('.', ',')
          return convertedValue
        } else return localeValue
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }
    
    
    /**
     * Get one relation in doc
     *
     * @param {object} doc - DocumentEntity
     * @param {string} relName - Name of the relation
     */
    export function getRelationship<D>(doc: D, relName: string): Relation {
      return get(doc, `relationships.${relName}.data`, [])
    }
    
    export const importIconbyId = async (id: string, pathType: string) => {
    
      // Les svg doivent être au format id.svg
    
      let importedChallengeIcon
    
        importedChallengeIcon = await import(
    
          /* webpackMode: "eager" */ `assets/icons/visu/${pathType}/${id}.svg`
    
      if (importedChallengeIcon) {
        return importedChallengeIcon.default