~cypheon/nextcloud-chores-app

5fff2047da5cd51afc176133cf799e2d3458c87b — Johann Rudloff 2 months ago 7205a09
Extract some chore filtering logic
2 files changed, 37 insertions(+), 30 deletions(-)

A src/logic.js
M src/views/Dashboard.vue
A src/logic.js => src/logic.js +33 -0
@@ 0,0 1,33 @@
export function daysUntilDueDate(chore, now) {
  const MILLIS_PER_DAY = (3600 * 1000 * 24);
  const diff = new Date(chore.due) - now;
  return Math.round(diff / MILLIS_PER_DAY);
}

export function calcDue(chore, now) {
  const days = daysUntilDueDate(chore, now);
  if (chore.repeat.startsWith('o:')) {
    return ['on-demand', false];
  }
  if (days === -1) {
    return ['yesterday', true];
  }
  if (days === 0) {
    return ['today', false];
  }
  if (days === 1) {
    return ['tomorrow', false];
  }
  if (days < (-1)) {
    return [`${-days} days ago`, true];
  }
  if (days > 1) {
    return [`in ${days} days`, false];
  }
  return ['', false];
}

export function isOverdue(chore, now) {
  const [, overdue] = calcDue(chore, now);
  return overdue;
}

M src/views/Dashboard.vue => src/views/Dashboard.vue +4 -30
@@ 86,6 86,7 @@
<script>

import { Actions, ActionButton, ActionRouter, Modal, UserBubble } from '@nextcloud/vue';
import { calcDue, daysUntilDueDate } from '../logic';

export default {
  name: 'Dashboard',


@@ 126,7 127,7 @@ export default {
            result.push(chore)
          }
        } else if (mode === 'upcoming') {
          if (this.daysUntilDueDate(chore, this.now) <= 2) {
          if (daysUntilDueDate(chore, this.now) <= 2) {
            result.push(chore)
          }
        } else {


@@ 138,39 139,12 @@ export default {
    formatDueDateAbsolute(chore) {
      return chore.due;
    },
    daysUntilDueDate(chore, now) {
      const MILLIS_PER_DAY = (3600 * 1000 * 24);
      const diff = new Date(chore.due) - now;
      return Math.round(diff / MILLIS_PER_DAY);
    },
    calcDue(chore) {
      const days = this.daysUntilDueDate(chore, this.now);
      if (chore.repeat.startsWith('o:')) {
        return ['on-demand', false];
      }
      if (days === -1) {
        return ['yesterday', true];
      }
      if (days === 0) {
        return ['today', false];
      }
      if (days === 1) {
        return ['tomorrow', false];
      }
      if (days < (-1)) {
        return [`${-days} days ago`, true];
      }
      if (days > 1) {
        return [`in ${days} days`, false];
      }
      return ['', false];
    },
    formatDueDateRelative(chore) {
      const [msg, ] = this.calcDue(chore);
      const [msg, ] = calcDue(chore, this.now);
      return msg;
    },
    isOverdue(chore) {
      const [, overdue] = this.calcDue(chore);
      const [, overdue] = calcDue(chore, this.now);
      return overdue;
    },
    formatRepeat(repeat) {