data.ts 1.68 KB
import { forEach } from '/@/utils/helper/treeHelper';
import type { UserInfo } from '/#/store';
import { defineStore } from 'pinia';
import { store } from '/@/store';
import { RoleEnum } from '/@/enums/roleEnum';

import { getApiData, getChartData } from '/@/api/project/global';
import { formatToDate } from '/@/utils/dateUtil';

interface UserState {
  userInfo: Nullable<UserInfo>;
  token?: string;
  roleList: RoleEnum[];
  sessionTimeout?: boolean;
  lastUpdateTime: number;
}

export const useDataStore = defineStore({
  id: 'app-data',
  state: (): UserState => ({
    // user info
    data: {},
    // token
    chartData: {},
  }),
  getters: {
    getData(state) {
      return state.data;
    },
    getChartData(state) {
      return state.chartData;
    },
  },
  actions: {
    setData(info) {
      this.data = info ? info : '';
    },
    setChartData(data) {
      this.chartData = data;
    },

    async getFetchData(): Promise<any> {
      try {
        const data = await getApiData();
        this.data = data;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    async getFetchChartData(): Promise<any> {
      try {
        const data = await getChartData();
        console.log(data, '5656chartData');
        const x = [],
          y = [];
        data.forEach((value) => {
          x.push(formatToDate(value.dateTime));
          y.push(value.orderCount);
        });
        this.chartData = { x, y };
        console.log(this.getChartData, '5656 this.chartData');
      } catch (error) {
        return Promise.reject(error);
      }
    },
  },
});

// Need to be used outside the setup
export function useDataStoreWithOut() {
  return useDataStore(store);
}