Prefer using last week/last month
authorChocobozzz <me@florianbigard.com>
Tue, 4 Jun 2019 08:28:19 +0000 (10:28 +0200)
committerChocobozzz <me@florianbigard.com>
Thu, 6 Jun 2019 09:43:00 +0000 (11:43 +0200)
client/src/app/shared/video/abstract-video-list.ts
shared/core-utils/miscs/date.ts

index 8cf21e9d452f3bb22dba7e763a837e2c95210080..dc8f9cda9f8ac8f4a3c1739259f24daac0304806 100644 (file)
@@ -12,14 +12,14 @@ import { Syndication } from '@app/shared/video/syndication.model'
 import { Notifier, ServerService } from '@app/core'
 import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
 import { I18n } from '@ngx-translate/i18n-polyfill'
-import { isThisMonth, isThisWeek, isToday, isYesterday } from '@shared/core-utils/miscs/date'
+import { isLastMonth, isLastWeek, isToday, isYesterday } from '@shared/core-utils/miscs/date'
 
 enum GroupDate {
   UNKNOWN = 0,
   TODAY = 1,
   YESTERDAY = 2,
-  THIS_WEEK = 3,
-  THIS_MONTH = 4,
+  LAST_WEEK = 3,
+  LAST_MONTH = 4,
   OLDER = 5
 }
 
@@ -84,8 +84,8 @@ export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableFor
       [GroupDate.UNKNOWN]: null,
       [GroupDate.TODAY]: this.i18n('Today'),
       [GroupDate.YESTERDAY]: this.i18n('Yesterday'),
-      [GroupDate.THIS_WEEK]: this.i18n('This week'),
-      [GroupDate.THIS_MONTH]: this.i18n('This month'),
+      [GroupDate.LAST_WEEK]: this.i18n('Last week'),
+      [GroupDate.LAST_MONTH]: this.i18n('Last month'),
       [GroupDate.OLDER]: this.i18n('Older')
     }
 
@@ -183,18 +183,18 @@ export abstract class AbstractVideoList implements OnInit, OnDestroy, DisableFor
         continue
       }
 
-      if (currentGroupedDate <= GroupDate.THIS_WEEK && isThisWeek(publishedDate)) {
-        if (currentGroupedDate === GroupDate.THIS_WEEK) continue
+      if (currentGroupedDate <= GroupDate.LAST_WEEK && isLastWeek(publishedDate)) {
+        if (currentGroupedDate === GroupDate.LAST_WEEK) continue
 
-        currentGroupedDate = GroupDate.THIS_WEEK
+        currentGroupedDate = GroupDate.LAST_WEEK
         this.groupedDates[ video.id ] = currentGroupedDate
         continue
       }
 
-      if (currentGroupedDate <= GroupDate.THIS_MONTH && isThisMonth(publishedDate)) {
-        if (currentGroupedDate === GroupDate.THIS_MONTH) continue
+      if (currentGroupedDate <= GroupDate.LAST_MONTH && isLastMonth(publishedDate)) {
+        if (currentGroupedDate === GroupDate.LAST_MONTH) continue
 
-        currentGroupedDate = GroupDate.THIS_MONTH
+        currentGroupedDate = GroupDate.LAST_MONTH
         this.groupedDates[ video.id ] = currentGroupedDate
         continue
       }
index 7f0b4443b7c0533b42402d70c84bea62a20b7760..4f92f758ff665600d5b392d614e4366c0b18522a 100644 (file)
@@ -31,13 +31,27 @@ function isThisMonth (d: Date) {
   return d.getMonth() === thisMonth
 }
 
+function isLastMonth (d: Date) {
+  const now = new Date()
+
+  return getDaysDifferences(now, d) <= 30
+}
+
+function isLastWeek (d: Date) {
+  const now = new Date()
+
+  return getDaysDifferences(now, d) <= 7
+}
+
 // ---------------------------------------------------------------------------
 
 export {
   isYesterday,
   isThisWeek,
   isThisMonth,
-  isToday
+  isToday,
+  isLastMonth,
+  isLastWeek
 }
 
 // ---------------------------------------------------------------------------
@@ -47,3 +61,7 @@ function areDatesEqual (d1: Date, d2: Date) {
     d1.getMonth() === d2.getMonth() &&
     d1.getDate() === d2.getDate()
 }
+
+function getDaysDifferences (d1: Date, d2: Date) {
+  return (d1.getTime() - d2.getTime()) / (86400000)
+}