1
1
import { orderBy } from 'lodash' ;
2
2
import * as sortTypes from '../constants/sortTypes' ;
3
+ import * as dateSortTypes from './dateSortTypes' ;
4
+ import moment from 'moment' ;
3
5
4
6
const SORT_NAMES = {
5
7
[ sortTypes . NONE ] : 'NONE' ,
6
8
[ sortTypes . SORT_PLAYS ] : 'PLAYS' ,
7
9
[ sortTypes . SORT_FAVORITES ] : 'FAVORITES' ,
8
10
[ sortTypes . SORT_REPOSTS ] : 'REPOSTS' ,
9
11
} ;
12
+ const DATE_SORT_NAMES = {
13
+ [ dateSortTypes . NONE ] : 'NONE' ,
14
+ [ dateSortTypes . PAST_6MONTH ] : 'PAST 6 MONTHS' ,
15
+ [ dateSortTypes . PAST_YEAR ] : 'PAST YEAR' ,
16
+ [ dateSortTypes . OLDER ] : 'OLDER'
17
+ } ;
18
+ const DATE_SORT_FUNCTIONS = {
19
+ [ dateSortTypes . NONE ] : ( objs ) => objs ,
20
+ [ dateSortTypes . PAST_6MONTH ] : ( activities ) => sortByMonth ( activities ) ,
21
+ [ dateSortTypes . PAST_YEAR ] : ( activities ) => sortByYear ( activities ) ,
22
+ [ dateSortTypes . OLDER ] : ( activities ) => sortByOld ( activities ) ,
23
+ } ;
10
24
11
25
const SORT_FUNCTIONS = {
12
26
[ sortTypes . NONE ] : ( objs ) => objs ,
@@ -15,6 +29,42 @@ const SORT_FUNCTIONS = {
15
29
[ sortTypes . SORT_REPOSTS ] : ( activities ) => sortByReposts ( activities ) ,
16
30
} ;
17
31
32
+ function sortDates ( dt1 , dt2 ) {
33
+ const dateA = new Date ( dt1 . created_at ) ;
34
+ const dateB = new Date ( dt2 . created_at ) ;
35
+ return dateA - dateB ;
36
+ }
37
+
38
+ function sortByMonth ( activities ) {
39
+ const sortDt = new moment ( ) . subtract ( 6 , 'months' ) . date ( 1 ) ;
40
+ const act = activities . filter ( obj => {
41
+ return moment ( obj . created_at ) >= sortDt ;
42
+ } ) ;
43
+ return act . sort ( ( a , b ) => {
44
+ return sortDates ( a , b ) ;
45
+ } ) ;
46
+ }
47
+
48
+ function sortByYear ( activities ) {
49
+ const sortDt = new moment ( ) . subtract ( 1 , 'year' ) . date ( 1 ) ;
50
+ const act = activities . filter ( obj => {
51
+ return moment ( obj . created_at ) >= sortDt ;
52
+ } ) ;
53
+ return act . sort ( ( a , b ) => {
54
+ return sortDates ( a , b ) ;
55
+ } ) ;
56
+ }
57
+
58
+ function sortByOld ( activities ) {
59
+ const sortDt = new moment ( ) . subtract ( 1 , 'year' ) . date ( 1 ) ;
60
+ const act = activities . filter ( obj => {
61
+ return moment ( obj . created_at ) < sortDt ;
62
+ } ) ;
63
+ return act . sort ( ( a , b ) => {
64
+ return sortDates ( a , b ) ;
65
+ } ) ;
66
+ }
67
+
18
68
function sortByPlays ( activities ) {
19
69
return orderBy ( activities , ( activity ) => activity . playback_count , 'desc' ) ;
20
70
}
@@ -30,4 +80,6 @@ function sortByReposts(activities) {
30
80
export {
31
81
SORT_NAMES ,
32
82
SORT_FUNCTIONS ,
83
+ DATE_SORT_NAMES ,
84
+ DATE_SORT_FUNCTIONS
33
85
} ;
0 commit comments