日期维度切换:Daily、Hourly、Month,如果设计更优雅的-setDefaultOption?

60 阅读1分钟

场景

日期维度切换:Daily、Hourly、Month,如果设计更优雅地 set the default option

  1. Daily: Last 7 Days
  2. Hourly: Today 00:00 -> Today 23:00 (PS:存在可能,更新需求为,使用 Daily 的选择)
  3. Monthly: 1st Date of Month -> Today

现在的做法

watch: {
  'timeframe.interval': function (interval) {
    const presetDict = {
      by_month: 'This month',
      by_date: 'Last 7 days',
      by_hour: 'Today',
    };

    this.onPresetSelect(presetDict[interval]);
    this.setPresetText(presetDict[interval]);

    if (interval === 'by_hour') {
      this.timeframe.endHour = 23;
    }
  },
}