use-process-state.ts 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { useRoute } from 'vue-router'
import { useAsyncState } from '@vueuse/core'
import { countProcessInstanceState } from '@/service/modules/projects-analysis'
import { format } from 'date-fns'
22 23 24 25
import { toLower } from 'lodash'
import { useI18n } from 'vue-i18n'
import type { TaskStateRes } from '@/service/modules/projects-analysis/types'
import type { StateData } from './types'
26
import { reactive, ref } from 'vue'
27 28 29

export function useProcessState() {
  const route = useRoute()
30
  const { t } = useI18n()
31 32 33
  const processVariables = reactive({
    processLoadingRef: ref(false)
  })
34 35

  const getProcessState = (date: Array<number>) => {
36 37
    if (processVariables.processLoadingRef) return
    processVariables.processLoadingRef = true
38 39
    const { state } = useAsyncState(
      countProcessInstanceState({
40 41
        startDate: !date ? '' : format(date[0], 'yyyy-MM-dd HH:mm:ss'),
        endDate: !date ? '' : format(date[1], 'yyyy-MM-dd HH:mm:ss'),
42
        projectCode: Number(route.params.projectCode)
43
      }).then((res: TaskStateRes): StateData => {
44
        const table = res.taskCountDtos.map((item, unused) => {
45
          return {
46
            state: t('home.' + toLower(item.taskStateType)),
47
            number: item.count
48 49 50 51 52 53
          }
        })

        const chart = res.taskCountDtos.map((item) => {
          return {
            value: item.count,
54
            name: t('home.' + toLower(item.taskStateType))
55 56
          }
        })
57
        processVariables.processLoadingRef = false
58 59 60 61 62 63 64 65 66

        return { table, chart }
      }),
      { table: [], chart: [] }
    )

    return state
  }

67
  return { getProcessState, processVariables }
68
}