Skip to content

Commit 013a283

Browse files
authored
Merge pull request #1839 from wix/infra/ExpandableCalendar_to_function
Infra/expandable calendar to function
2 parents d172af1 + 5871703 commit 013a283

File tree

12 files changed

+741
-767
lines changed

12 files changed

+741
-767
lines changed

example/src/screens/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import CalendarsScreen from './calendarScreen';
55
import AgendaScreen from './agendaScreen';
66
import CalendarsList from './calendarsList';
77
import HorizontalCalendarList from './horizontalCalendarList';
8-
import ExpandableCalendar from './expandableCalendar';
9-
import TimelineCalendar from './timelineCalendarScreen';
8+
import ExpandableCalendarScreen from './expandableCalendarScreen';
9+
import TimelineCalendarScreen from './timelineCalendarScreen';
1010

1111
export function registerScreens() {
1212
Navigation.registerComponent('Menu', () => MenuScreen);
1313
Navigation.registerComponent('Calendars', () => CalendarsScreen);
1414
Navigation.registerComponent('Agenda', () => AgendaScreen);
1515
Navigation.registerComponent('CalendarsList', () => CalendarsList);
1616
Navigation.registerComponent('HorizontalCalendarList', () => HorizontalCalendarList);
17-
Navigation.registerComponent('ExpandableCalendar', () => ExpandableCalendar);
18-
Navigation.registerComponent('TimelineCalendar', () => TimelineCalendar);
17+
Navigation.registerComponent('ExpandableCalendar', () => ExpandableCalendarScreen);
18+
Navigation.registerComponent('TimelineCalendar', () => TimelineCalendarScreen);
1919
}

src/expandableCalendar/Context/Presenter.spec.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
import XDate from 'xdate';
22
import {UpdateSources} from '../commons';
33
import {toMarkingFormat} from '../../interface';
4-
import Presenter from './Presenter';
4+
import {
5+
getButtonIcon,
6+
setDate,
7+
setDisabled,
8+
shouldAnimateTodayButton,
9+
getPositionAnimation,
10+
getOpacityAnimation,
11+
getTodayDate,
12+
getTodayFormatted
13+
} from './Presenter';
514

615
describe('Context provider tests', () => {
7-
const makeUUT = () => {
8-
return new Presenter();
9-
};
10-
1116
const pastDate = '2021-04-04';
1217
const futureDate = '2050-04-04';
1318
const todayDate = toMarkingFormat(XDate());
1419
const updateSources = UpdateSources.CALENDAR_INIT;
1520

1621
describe('Button Icon test', () => {
1722
it('Expect to get down button on past date', () => {
18-
const {getButtonIcon} = makeUUT();
1923
const imageUp = '../../../src/img/up.png';
2024
const imageDown = '../../../src/img/down.png';
2125

@@ -24,7 +28,6 @@ describe('Context provider tests', () => {
2428
});
2529

2630
it('Expect no image when showTodayButton is false', () => {
27-
const {getButtonIcon} = makeUUT();
2831
expect(getButtonIcon(pastDate, false)).toBe(undefined);
2932
});
3033
});
@@ -39,7 +42,6 @@ describe('Context provider tests', () => {
3942
});
4043

4144
it('Expect onDataChanged and updateState to be called on same months dates passed', () => {
42-
const {setDate} = makeUUT();
4345
const date = '2021-01-01';
4446
const sameMonthDate = '2021-01-20';
4547
const props = {onDateChanged, onMonthChange, showTodayButton: false};
@@ -52,7 +54,6 @@ describe('Context provider tests', () => {
5254
});
5355

5456
it('Expect onMonthChange to be called when different months date passed', () => {
55-
const {setDate} = makeUUT();
5657
const date = '2021-01-01';
5758
const differentMonth = '2021-02-20';
5859
const props = {onDateChanged, onMonthChange, showTodayButton: false};
@@ -67,7 +68,6 @@ describe('Context provider tests', () => {
6768

6869
describe('Set Disabled test suit', () => {
6970
it('Expect setDisabled would not call updateState when showTodayButton', () => {
70-
const {setDisabled} = makeUUT();
7171
const updateStateMock = jest.fn();
7272
const showTodayButton = false;
7373

@@ -76,7 +76,6 @@ describe('Context provider tests', () => {
7676
});
7777

7878
it('Expect setDisabled will call updateState when showTodayButton is true and disabled value changed', () => {
79-
const {setDisabled} = makeUUT();
8079
const updateStateMock = jest.fn();
8180
const showTodayButton = true;
8281

@@ -88,7 +87,6 @@ describe('Context provider tests', () => {
8887
});
8988

9089
it('Expect setDisabled will NOT call updateState when showTodayButton is true and disabled value is the same', () => {
91-
const {setDisabled} = makeUUT();
9290
const updateStateMock = jest.fn();
9391
const showTodayButton = true;
9492

@@ -102,14 +100,11 @@ describe('Context provider tests', () => {
102100

103101
describe("Animate Today's Button", () => {
104102
it('Expect shouldAnimateTodayButton to return same value as props.showTodayButton', () => {
105-
const {shouldAnimateTodayButton} = makeUUT();
106-
107103
expect(shouldAnimateTodayButton({showTodayButton: false})).toBe(false);
108104
expect(shouldAnimateTodayButton({showTodayButton: true})).toBe(true);
109105
});
110106

111107
it("Expect animation value to be top position when today's date passed", () => {
112-
const {getPositionAnimation} = makeUUT();
113108
const TOP_POSITION = 65;
114109
const {tension, friction, useNativeDriver} = getPositionAnimation(todayDate, 10);
115110

@@ -120,7 +115,6 @@ describe('Context provider tests', () => {
120115
});
121116

122117
it('Expect animation value to be minus value of bottomMargin when past or future date passed', () => {
123-
const {getPositionAnimation} = makeUUT();
124118
const TOP_POSITION = 65;
125119

126120
expect(getPositionAnimation(futureDate, 999).toValue).toEqual(-999);
@@ -130,7 +124,6 @@ describe('Context provider tests', () => {
130124
});
131125

132126
it('Expect opacity animation value', () => {
133-
const {getOpacityAnimation} = makeUUT();
134127
const disabledOpacity = 0.5;
135128
let data = getOpacityAnimation({disabledOpacity}, true);
136129

@@ -146,13 +139,11 @@ describe('Context provider tests', () => {
146139

147140
describe('onTodayPressed tests', () => {
148141
it("Expect return value to be XDate today's date", () => {
149-
const {getTodayDate} = makeUUT();
150142
expect(getTodayDate()).toEqual(todayDate);
151143
});
152144
});
153145

154146
it("Today's date formatted", () => {
155-
const {getTodayFormatted} = makeUUT();
156147
expect(getTodayFormatted()).toEqual('Today');
157148
});
158149
});

src/expandableCalendar/Context/Presenter.ts

Lines changed: 76 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,88 +9,84 @@ import {CalendarContextProviderProps} from './Provider';
99
const commons = require('../commons');
1010
const TOP_POSITION = 65;
1111

12-
class Presenter {
13-
_getIconDown = () => {
14-
return require('../../img/down.png');
12+
export const _getIconDown = () => {
13+
return require('../../img/down.png');
14+
};
15+
16+
export const _getIconUp = () => {
17+
return require('../../img/up.png');
18+
};
19+
20+
export const getButtonIcon = (date: string, showTodayButton = true) => {
21+
if (!showTodayButton) {
22+
return undefined;
23+
}
24+
const icon = isPastDate(date) ? _getIconDown() : _getIconUp();
25+
return icon;
26+
};
27+
28+
export const setDate = (
29+
props: CalendarContextProviderProps,
30+
date: string,
31+
newDate: string,
32+
updateState: (buttonIcon: number) => void,
33+
updateSource: UpdateSources
34+
) => {
35+
const buttonIcon = getButtonIcon(date, props.showTodayButton);
36+
37+
updateState(buttonIcon);
38+
39+
props.onDateChanged?.(date, updateSource);
40+
41+
if (!sameMonth(new XDate(date), new XDate(newDate))) {
42+
props.onMonthChange?.(xdateToData(new XDate(date)), updateSource);
43+
}
44+
};
45+
46+
export const setDisabled = (
47+
showTodayButton: boolean,
48+
newDisabledValue: boolean,
49+
oldDisabledValue: boolean,
50+
updateState: (disabled: boolean) => void
51+
) => {
52+
if (!showTodayButton || newDisabledValue === oldDisabledValue) {
53+
return;
54+
}
55+
updateState(newDisabledValue);
56+
};
57+
58+
export const shouldAnimateTodayButton = (props: CalendarContextProviderProps) => {
59+
return props.showTodayButton;
60+
};
61+
62+
export const getTodayDate = () => {
63+
return toMarkingFormat(new XDate());
64+
};
65+
66+
export const getPositionAnimation = (date: string, todayBottomMargin = 0) => {
67+
const toValue = isToday(new XDate(date)) ? TOP_POSITION : -todayBottomMargin || -TOP_POSITION;
68+
return {
69+
toValue,
70+
tension: 30,
71+
friction: 8,
72+
useNativeDriver: true
1573
};
74+
};
1675

17-
_getIconUp = () => {
18-
return require('../../img/up.png');
19-
};
20-
21-
getButtonIcon = (date: string, showTodayButton = true) => {
22-
if (!showTodayButton) {
23-
return undefined;
24-
}
25-
const icon = isPastDate(date) ? this._getIconDown() : this._getIconUp();
26-
return icon;
27-
};
28-
29-
setDate = (
30-
props: CalendarContextProviderProps,
31-
date: string,
32-
newDate: string,
33-
updateState: (buttonIcon: number) => void,
34-
updateSource: UpdateSources
35-
) => {
36-
const buttonIcon = this.getButtonIcon(date, props.showTodayButton);
37-
38-
updateState(buttonIcon);
39-
40-
props.onDateChanged?.(date, updateSource);
41-
42-
if (!sameMonth(new XDate(date), new XDate(newDate))) {
43-
props.onMonthChange?.(xdateToData(new XDate(date)), updateSource);
44-
}
45-
};
46-
47-
setDisabled = (
48-
showTodayButton: boolean,
49-
newDisabledValue: boolean,
50-
oldDisabledValue: boolean,
51-
updateState: (disabled: boolean) => void
52-
) => {
53-
if (!showTodayButton || newDisabledValue === oldDisabledValue) {
54-
return;
55-
}
56-
updateState(newDisabledValue);
57-
};
58-
59-
shouldAnimateTodayButton = (props: CalendarContextProviderProps) => {
60-
return props.showTodayButton;
61-
};
62-
63-
getTodayDate = () => {
64-
return toMarkingFormat(new XDate());
65-
};
66-
67-
getPositionAnimation = (date: string, todayBottomMargin = 0) => {
68-
const toValue = isToday(new XDate(date)) ? TOP_POSITION : -todayBottomMargin || -TOP_POSITION;
69-
return {
70-
toValue,
71-
tension: 30,
72-
friction: 8,
73-
useNativeDriver: true
74-
};
75-
};
76-
77-
shouldAnimateOpacity = (props: CalendarContextProviderProps) => {
78-
return props.disabledOpacity;
79-
};
80-
81-
getOpacityAnimation = ({disabledOpacity = 0}: CalendarContextProviderProps, disabled: boolean) => {
82-
return {
83-
toValue: disabled ? disabledOpacity : 1,
84-
duration: 500,
85-
useNativeDriver: true
86-
};
87-
};
76+
export const shouldAnimateOpacity = (props: CalendarContextProviderProps) => {
77+
return props.disabledOpacity;
78+
};
8879

89-
getTodayFormatted = () => {
90-
const todayString = getDefaultLocale().today || commons.todayString;
91-
const today = todayString.charAt(0).toUpperCase() + todayString.slice(1);
92-
return today;
80+
export const getOpacityAnimation = ({disabledOpacity = 0}: CalendarContextProviderProps, disabled: boolean) => {
81+
return {
82+
toValue: disabled ? disabledOpacity : 1,
83+
duration: 500,
84+
useNativeDriver: true
9385
};
94-
}
86+
};
9587

96-
export default Presenter;
88+
export const getTodayFormatted = () => {
89+
const todayString = getDefaultLocale().today || commons.todayString;
90+
const today = todayString.charAt(0).toUpperCase() + todayString.slice(1);
91+
return today;
92+
};

0 commit comments

Comments
 (0)