enumProcessor.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import {ApiResponse, DataProviderArgs, EnumChoice, EnumChoices, Processor} from '~/types/interfaces'
  2. import BaseProcessor from '~/services/data/processor/baseProcessor'
  3. import { QUERY_TYPE } from '~/types/enums'
  4. class EnumProcessor extends BaseProcessor implements Processor {
  5. /**
  6. * Is the given argument a supported model
  7. * @param args
  8. */
  9. public static support (args:DataProviderArgs): boolean {
  10. return args.type === QUERY_TYPE.ENUM
  11. }
  12. /**
  13. *
  14. * @param data
  15. */
  16. // eslint-disable-next-line require-await
  17. async process (payload: ApiResponse): Promise<any> {
  18. const enums: EnumChoices = []
  19. useEach(payload.data.items, (item, key) => {
  20. const entry:EnumChoice = {
  21. value: key,
  22. label: this.ctx.app.i18n.t(item) as string
  23. }
  24. enums.push(entry)
  25. })
  26. return EnumProcessor.sortEnum(enums)
  27. }
  28. /**
  29. * Sort the given enum by its elements labels
  30. * @param enums
  31. * @private
  32. */
  33. private static sortEnum (enums: EnumChoices) {
  34. return enums.sort((a, b) => {
  35. if (a.label > b.label) { return 1 }
  36. if (a.label < b.label) { return -1 }
  37. return 0
  38. })
  39. }
  40. }
  41. export default EnumProcessor