@@ -29,61 +29,82 @@ export const getPlaceholder = (date: Date) => ({ ...CalendarPlaceholder, id: `em
29
29
export const getLoadingPlaceholder = ( date : Date ) =>
30
30
( { ...getPlaceholder ( date ) , id : `loading-${ date . getTime ( ) } ` , type : ListScrollItemType . loading } ) as CalendarItem ;
31
31
32
- export const getEmptyWeeks = ( fromDate : Date , loading ?: boolean ) => {
33
- return Array ( 14 )
32
+ export const getEmptyWeeks = ( { startDate , loading = false , days = 14 } : { startDate : Date ; loading ?: boolean ; days ?: number } ) => {
33
+ return Array ( days )
34
34
. fill ( CalendarPlaceholder )
35
35
. map ( ( _ , index ) => {
36
- const date = DateUtils . next ( index , fromDate ) ;
36
+ const date = DateUtils . next ( index , startDate ) ;
37
37
return loading ? getLoadingPlaceholder ( date ) : getPlaceholder ( date ) ;
38
38
} ) ;
39
39
} ;
40
40
41
- export const spaceDate = ( data : CalendarItem [ ] , startDate : Date , endDate : Date ) : CalendarItem [ ] => {
42
- const spacedData : CalendarItem [ ] = [ ] ;
43
- data ?. forEach ( ( item , index ) => {
44
- if ( index === 0 ) {
45
- // if the first item isn't the start date, add placeholders
46
- if ( item . date . getTime ( ) > startDate . getTime ( ) && item . date . toLocaleDateString ( ) !== startDate . toLocaleDateString ( ) ) {
47
- let previousDate : Date = item . date ;
48
- while ( previousDate . toLocaleDateString ( ) !== startDate . toLocaleDateString ( ) ) {
49
- previousDate = DateUtils . previous ( 1 , previousDate ) ;
50
- spacedData . push ( getPlaceholder ( previousDate ) ) ;
51
- }
52
- }
53
- return spacedData . push ( item ) ;
41
+ const padStartInterval = ( spacedData : CalendarItem [ ] , { item, startDate, added = 0 } : { item : CalendarItem ; startDate : Date ; added ?: number } ) => {
42
+ let _added = added ;
43
+ // if the first item isn't the start date, add placeholders
44
+ if ( item . date . getTime ( ) > startDate . getTime ( ) && item . date . toLocaleDateString ( ) !== startDate . toLocaleDateString ( ) ) {
45
+ let currentDate : Date = startDate ;
46
+ while ( item . date . toLocaleDateString ( ) !== currentDate . toLocaleDateString ( ) ) {
47
+ _added += spacedData . push ( getPlaceholder ( currentDate ) ) ;
48
+ currentDate = DateUtils . next ( 1 , currentDate ) ;
54
49
}
50
+ }
51
+ return _added ;
52
+ } ;
55
53
56
- if ( index === data . length - 1 ) {
57
- spacedData . push ( item ) ;
58
-
59
- // if the last item isn't one day before the end date, add placeholders
60
- const dayBeforeEnd = DateUtils . previous ( 1 , endDate ) ;
61
- if ( item . date . getTime ( ) < dayBeforeEnd . getTime ( ) && item . date . toLocaleDateString ( ) !== dayBeforeEnd . toLocaleDateString ( ) ) {
62
- let nextDate : Date = item . date ;
63
- while ( nextDate . toLocaleDateString ( ) !== dayBeforeEnd . toLocaleDateString ( ) ) {
64
- nextDate = DateUtils . next ( 1 , nextDate ) ;
65
- spacedData . push ( getPlaceholder ( nextDate ) ) ;
66
- }
67
- }
68
- return ;
54
+ const padEndInterval = ( spacedData : CalendarItem [ ] , { item, endDate, added = 0 } : { item : CalendarItem ; endDate : Date ; added ?: number } ) => {
55
+ let _added = added ;
56
+ // if the last item isn't one day before the end date, add placeholders
57
+ if ( item . date . getTime ( ) < endDate . getTime ( ) && item . date . toLocaleDateString ( ) !== endDate . toLocaleDateString ( ) ) {
58
+ let currentDate : Date = DateUtils . next ( 1 , item . date ) ;
59
+ while ( currentDate . toLocaleDateString ( ) !== endDate . toLocaleDateString ( ) ) {
60
+ _added += spacedData . push ( getPlaceholder ( currentDate ) ) ;
61
+ currentDate = DateUtils . next ( 1 , currentDate ) ;
69
62
}
63
+ }
64
+ return _added ;
65
+ } ;
70
66
71
- const previous = data [ index - 1 ] ;
67
+ const padBetweenInterval = ( spacedData : CalendarItem [ ] , { item, previous } : { previous : CalendarItem ; item : CalendarItem } ) => {
68
+ // if the item us the same date as the previous one, no need to pad
69
+ if ( item . date . toLocaleDateString ( ) === previous . date . toLocaleDateString ( ) ) return 0 ;
70
+
71
+ // if the item is the same as the day after the previous one, no need to pad
72
+ let nextDate : Date = DateUtils . next ( 1 , previous . date ) ;
73
+ if ( item . date . toLocaleDateString ( ) === nextDate . toLocaleDateString ( ) ) return 0 ;
74
+
75
+ let added = 0 ;
76
+ // if the item isn't at least 1 day after the previous date, add placeholders
77
+ while ( item . date . toLocaleDateString ( ) !== nextDate . toLocaleDateString ( ) ) {
78
+ added += spacedData . push ( getPlaceholder ( nextDate ) ) ;
79
+ nextDate = DateUtils . next ( 1 , nextDate ) ;
80
+ }
81
+ return added ;
82
+ } ;
72
83
73
- if ( item . date . toLocaleDateString ( ) === previous . date . toLocaleDateString ( ) ) return spacedData . push ( item ) ;
74
- if ( item . date . toLocaleDateString ( ) === DateUtils . next ( 1 , previous . date ) . toLocaleDateString ( ) ) return spacedData . push ( item ) ;
84
+ export const spaceDate = ( data : CalendarItem [ ] , { startDate, endDate, days } : { startDate : Date ; endDate : Date ; days ?: number } ) : CalendarItem [ ] => {
85
+ const spacedData : CalendarItem [ ] = [ ] ;
86
+ data ?. forEach ( ( item , index , array ) => {
87
+ // if we are on the first item
88
+ if ( index === 0 ) {
89
+ padStartInterval ( spacedData , { item, startDate } ) ;
90
+ // If we have more than 1 item, we can return (end padding in the next iteration)
91
+ if ( array . length > 1 ) return spacedData . push ( item ) ;
92
+ }
75
93
76
- // if the item isn't at least 1 day after the previous date, add placeholders
77
- let previousDate : Date = previous . date ;
78
- while ( item . date . toLocaleDateString ( ) !== DateUtils . next ( 1 , previousDate ) . toLocaleDateString ( ) ) {
79
- previousDate = DateUtils . next ( 1 , previousDate ) ;
80
- spacedData . push ( getPlaceholder ( previousDate ) ) ;
94
+ // if we have are at least on the 2 item
95
+ if ( index && array . length > 1 ) padBetweenInterval ( spacedData , { item, previous : array [ index - 1 ] } ) ;
96
+
97
+ // if we are on the last item
98
+ if ( index === array . length - 1 ) {
99
+ spacedData . push ( item ) ;
100
+ return padEndInterval ( spacedData , { item, endDate } ) ;
81
101
}
82
102
spacedData . push ( item ) ;
83
103
} ) ;
84
- if ( ! spacedData . length ) spacedData . push ( ...getEmptyWeeks ( startDate ) ) ;
85
104
86
105
// if no data in response fill with placeholders
106
+ if ( ! spacedData . length ) spacedData . push ( ...getEmptyWeeks ( { startDate, days } ) ) ;
107
+
87
108
return spacedData ;
88
109
} ;
89
110
0 commit comments