Commit 404be9cc authored by Luko van der Maas's avatar Luko van der Maas
Browse files

Merge branch 'improve-eventscreen-code' into 'master'

Apply DRY on the computation of the event properties

Closes #82

See merge request !224
parents 3bdc4262 f2ac5998
...@@ -48,6 +48,34 @@ class EventScreen extends Component { ...@@ -48,6 +48,34 @@ class EventScreen extends Component {
); );
}; };
eventProperties = () => {
const { data } = this.props;
const nowDate = new Date();
const startRegDate = new Date(data.registration_start);
const endRegDate = new Date(data.registration_end);
const cancelDeadlineDate = new Date(data.cancel_deadline);
const registrationRequired = data.registration_start !== null || data.registration_end !== null;
const registrationStarted = startRegDate <= nowDate;
const isLateCancellation = data.user_registration
&& data.user_registration.is_late_cancellation;
const registrationAllowed = registrationRequired && endRegDate > nowDate
&& registrationStarted && data.registration_allowed
&& !isLateCancellation;
const afterCancelDeadline = data.cancel_deadline !== null && cancelDeadlineDate <= nowDate;
return {
registrationRequired,
registrationStarted,
isLateCancellation,
registrationAllowed,
afterCancelDeadline,
};
};
eventDesc = () => { eventDesc = () => {
const { t, data } = this.props; const { t, data } = this.props;
...@@ -102,7 +130,8 @@ class EventScreen extends Component { ...@@ -102,7 +130,8 @@ class EventScreen extends Component {
</View>, </View>,
); );
if (data.registration_start !== null || data.registration_end !== null) { const { registrationRequired } = this.eventProperties();
if (registrationRequired) {
const registrationDeadline = Moment(data.registration_end).format('D MMM YYYY, HH:mm'); const registrationDeadline = Moment(data.registration_end).format('D MMM YYYY, HH:mm');
const cancelDeadline = Moment(data.cancel_deadline).format('D MMM YYYY, HH:mm'); const cancelDeadline = Moment(data.cancel_deadline).format('D MMM YYYY, HH:mm');
...@@ -178,7 +207,7 @@ class EventScreen extends Component { ...@@ -178,7 +207,7 @@ class EventScreen extends Component {
infoTexts.push( infoTexts.push(
<View key="pizza-holder" style={styles.pizzaHolder}> <View key="pizza-holder" style={styles.pizzaHolder}>
<Text style={styles.pizzaText} key="pizza-title"> <Text style={styles.pizzaText} key="pizza-title">
Pizza: Pizza:
</Text> </Text>
<Button <Button
color={Colors.magenta} color={Colors.magenta}
...@@ -200,27 +229,23 @@ Pizza: ...@@ -200,27 +229,23 @@ Pizza:
const { data, t } = this.props; const { data, t } = this.props;
let text = ''; let text = '';
const nowDate = new Date(); const {
const startRegDate = new Date(data.registration_start); registrationRequired,
const endRegDate = new Date(data.registration_end); registrationStarted,
const cancelDeadlineDate = new Date(data.cancel_deadline); registrationAllowed,
isLateCancellation,
const regRequired = data.registration_start !== null || data.registration_end !== null; afterCancelDeadline,
const regStarted = startRegDate <= nowDate; } = this.eventProperties();
const regAllowed = regRequired && endRegDate > nowDate && regStarted;
const afterCancelDeadline = data.cancel_deadline !== null && cancelDeadlineDate <= nowDate; if (!registrationRequired) {
const isLateCancellation = data.user_registration
&& data.user_registration.is_late_cancellation;
if (!regRequired) {
text = t('No registration required.'); text = t('No registration required.');
if (data.no_registration_message) { if (data.no_registration_message) {
text = data.no_registration_message; text = data.no_registration_message;
} }
} else if (!regStarted) { } else if (!registrationStarted) {
const registrationStart = Moment(data.registration_start).format('D MMM YYYY, HH:mm'); const registrationStart = Moment(data.registration_start).format('D MMM YYYY, HH:mm');
text = t('Registration will open {{start}}', { start: registrationStart }); text = t('Registration will open {{start}}', { start: registrationStart });
} else if (!regAllowed) { } else if (!registrationAllowed) {
text = t('Registration is not possible anymore.'); text = t('Registration is not possible anymore.');
} else if (isLateCancellation) { } else if (isLateCancellation) {
text = t('Registration is not allowed anymore, as you cancelled your registration after the deadline.'); text = t('Registration is not allowed anymore, as you cancelled your registration after the deadline.');
...@@ -251,20 +276,14 @@ Pizza: ...@@ -251,20 +276,14 @@ Pizza:
const { const {
data, register, t, data, register, t,
} = this.props; } = this.props;
const nowDate = new Date();
const startRegDate = new Date(data.registration_start);
const endRegDate = new Date(data.registration_end);
const regRequired = data.registration_start !== null || data.registration_end !== null; const {
const regStarted = startRegDate <= nowDate; registrationRequired,
const isLateCancellation = data.user_registration registrationStarted,
&& data.user_registration.is_late_cancellation; registrationAllowed,
} = this.eventProperties();
const regAllowed = regRequired && endRegDate > nowDate
&& regStarted && data.registration_allowed
&& !isLateCancellation;
if (regAllowed) { if (registrationAllowed) {
if (data.user_registration === null || data.user_registration.is_cancelled) { if (data.user_registration === null || data.user_registration.is_cancelled) {
const text = data.max_participants && data.max_participants <= data.num_participants const text = data.max_participants && data.max_participants <= data.num_participants
? t('Put me on the waiting list') : t('Register'); ? t('Put me on the waiting list') : t('Register');
...@@ -293,8 +312,8 @@ Pizza: ...@@ -293,8 +312,8 @@ Pizza:
</View> </View>
); );
} if (data.user_registration && !data.user_registration.is_cancelled } if (data.user_registration && !data.user_registration.is_cancelled
&& regRequired && regStarted) { && registrationRequired && registrationStarted) {
if (regStarted && data.user_registration && !data.user_registration.is_cancelled if (registrationStarted && data.user_registration && !data.user_registration.is_cancelled
&& data.has_fields) { && data.has_fields) {
return ( return (
<View style={styles.registrationActions}> <View style={styles.registrationActions}>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment