enumDataProvider.ts 964 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import {EnumChoice, EnumChoices} from "~/types/types";
  2. import * as _ from "lodash";
  3. import {Rest} from "~/services/queries/rest";
  4. class EnumDataProvider{
  5. private $rest: Rest
  6. private i18n:any
  7. constructor($rest: Rest, i18n:any) {
  8. this.$rest = $rest
  9. this.i18n = i18n
  10. }
  11. public async get(type: string){
  12. const enums:EnumChoices = []
  13. const response = await this.$rest.getCollection(`/api/enum/${type}`)
  14. if(response){
  15. _.each(response['items'], (item, key) =>{
  16. const entry:EnumChoice = {
  17. value: key,
  18. label: this.i18n.t(item)
  19. };
  20. enums.push(entry)
  21. })
  22. }
  23. return this.sortEnum(enums)
  24. }
  25. public sortEnum(enums:EnumChoices){
  26. return enums.sort( (a, b) => {
  27. if (a.label > b.label)
  28. return 1;
  29. if (a.label < b.label)
  30. return -1;
  31. return 0;
  32. });
  33. }
  34. }
  35. export const $enumDataProvider = ($rest:Rest, i18n:any) => new EnumDataProvider($rest,i18n);