From 3d57361908fcc8582032f07f6a25e5cd2c963476 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Sun, 12 Apr 2020 19:56:29 +0200 Subject: [PATCH] Improved home screen items render cycle --- src/components/Home/ActionsDashboardItem.js | 12 +- src/components/Home/EventDashboardItem.js | 93 ++++++--- src/components/Home/FeedItem.js | 18 +- .../Home/PreviewEventDashboardItem.js | 96 ++++----- src/components/Home/SmallDashboardItem.js | 78 +++++--- src/screens/HomeScreen.js | 189 ++++++++---------- 6 files changed, 263 insertions(+), 223 deletions(-) diff --git a/src/components/Home/ActionsDashboardItem.js b/src/components/Home/ActionsDashboardItem.js index 04a0926..2540849 100644 --- a/src/components/Home/ActionsDashboardItem.js +++ b/src/components/Home/ActionsDashboardItem.js @@ -10,13 +10,10 @@ type Props = { theme: Object, } -class ActionsDashBoardItem extends React.PureComponent { +class ActionsDashBoardItem extends React.Component { - colors: Object; - - constructor(props) { - super(props); - this.colors = this.props.theme.colors; + shouldComponentUpdate(nextProps: Props): boolean { + return (nextProps.theme.dark !== this.props.theme.dark) } openDrawer = () => this.props.navigation.openDrawer(); @@ -24,10 +21,11 @@ class ActionsDashBoardItem extends React.PureComponent { gotToSettings = () => this.props.navigation.navigate("settings"); render() { + console.log('render action dashboard'); return ( - - - ); - } else - return + if (props.event !== undefined && props.event !== null) { + const hasImage = props.event['logo'] !== '' && props.event['logo'] !== null; + const getImage = () => ; + return ( + + {hasImage ? + : + } + {!isEmpty ? + + + : null} + + + + + + ); + } else + return null; + } } const styles = StyleSheet.create({ diff --git a/src/components/Home/SmallDashboardItem.js b/src/components/Home/SmallDashboardItem.js index 6c425e2..9da4928 100644 --- a/src/components/Home/SmallDashboardItem.js +++ b/src/components/Home/SmallDashboardItem.js @@ -1,40 +1,60 @@ +// @flow + import * as React from 'react'; import {Badge, IconButton, withTheme} from 'react-native-paper'; import {View} from "react-native"; + +type Props = { + color: string, + icon: string, + clickAction: Function, + isAvailable: boolean, + badgeNumber: number, + theme: Object, +}; + /** * Component used to render a small dashboard item - * - * @param props Props to pass to the component - * @return {*} */ -function SmallDashboardItem(props) { - const {colors} = props.theme; - return ( - - { + + shouldComponentUpdate(nextProps: Props) { + return (nextProps.theme.dark !== this.props.theme.dark) + || (nextProps.isAvailable !== this.props.isAvailable) + || (nextProps.badgeNumber !== this.props.badgeNumber); + } + + render() { + const props = this.props; + const colors = props.theme.colors; + return ( + + + { + props.badgeNumber > 0 ? + + {props.badgeNumber} + : null } - size={35} - onPress={props.clickAction} - /> - { - props.badgeNumber > 0 ? - - {props.badgeNumber} - : null - } - - ); + + ); + } + } export default withTheme(SmallDashboardItem); diff --git a/src/screens/HomeScreen.js b/src/screens/HomeScreen.js index a3f5d2e..1fe73aa 100644 --- a/src/screens/HomeScreen.js +++ b/src/screens/HomeScreen.js @@ -1,11 +1,11 @@ // @flow import * as React from 'react'; -import {StyleSheet, View} from 'react-native'; +import {FlatList, StyleSheet, View} from 'react-native'; import i18n from "i18n-js"; import DashboardItem from "../components/Home/EventDashboardItem"; import WebSectionList from "../components/Lists/WebSectionList"; -import {FAB, Text, withTheme} from 'react-native-paper'; +import {FAB, withTheme} from 'react-native-paper'; import FeedItem from "../components/Home/FeedItem"; import SquareDashboardItem from "../components/Home/SmallDashboardItem"; import PreviewEventDashboardItem from "../components/Home/PreviewEventDashboardItem"; @@ -159,24 +159,58 @@ class HomeScreen extends React.Component { content: undefined }, ]; - for (let [key, value] of Object.entries(dashboardData)) { + for (let [key, value: number | Object | Array] of Object.entries(dashboardData)) { switch (key) { + case 'available_machines': + dataset[0]['content'][0] = { + id: 'washers', + data: value.washers, + icon: 'washing-machine', + color: this.colors.proxiwashColor, + onPress: this.onProxiwashClick, + isAvailable: value.washers > 0 + }; + dataset[0]['content'][1] = { + ...dataset[0]['content'][0], + id: 'dryers', + data: value.dryers, + icon: 'tumble-dryer', + isAvailable: value.dryers > 0 + }; + break; + case 'available_tutorials': + dataset[0]['content'][2] = { + id: key, + data: value, + icon: 'school', + color: this.colors.tutorinsaColor, + onPress: this.onTutorInsaClick, + isAvailable: parseInt(value) > 0 + }; + break; + case 'proximo_articles': + dataset[0]['content'][3] = { + id: key, + data: value, + icon: 'shopping', + color: this.colors.proximoColor, + onPress: this.onProximoClick, + isAvailable: parseInt(value) > 0 + }; + break; + case 'today_menu': + dataset[0]['content'][4] = { + id: key, + data: 0, + icon: 'silverware-fork-knife', + color: this.colors.menu, + onPress: this.onMenuClick, + isAvailable: value.length > 0 + }; + break; case 'today_events': dataset[2]['content'] = value; break; - case 'available_machines': - dataset[0]['content'][0] = {id: key, data: value}; - break; - case 'available_tutorials': - dataset[0]['content'][1] = {id: key, data: value}; - break; - case 'proximo_articles': - dataset[0]['content'][2] = {id: key, data: value}; - break; - case 'today_menu': - dataset[0]['content'][3] = {id: key, data: value}; - break; - } } return dataset @@ -191,14 +225,14 @@ class HomeScreen extends React.Component { getDashboardItem(item: Object) { let content = item['content']; if (item['id'] === 'event') - return this.getDashboardEventItem(content); + return this.getDashboardEvent(content); else if (item['id'] === 'top') - return this.getDashboardTopItem(content); + return this.getDashboardRow(content); else - return this.getActionsDashboardItem(); + return this.getDashboardActions(); } - getActionsDashboardItem() { + getDashboardActions() { return ; } @@ -317,6 +351,8 @@ class HomeScreen extends React.Component { return displayEvent; } + onEventContainerClick = () => this.props.navigation.navigate('planning'); + /** * Gets the event render item. * If a preview is available, it will be rendered inside @@ -324,42 +360,17 @@ class HomeScreen extends React.Component { * @param content * @return {*} */ - getDashboardEventItem(content: Array) { - let icon = 'calendar-range'; - let title = i18n.t('homeScreen.dashboard.todayEventsTitle'); - let subtitle; + getDashboardEvent(content: Array) { let futureEvents = this.getFutureEvents(content); - let isAvailable = futureEvents.length > 0; - if (isAvailable) { - subtitle = - - {futureEvents.length} - - { - futureEvents.length > 1 ? - i18n.t('homeScreen.dashboard.todayEventsSubtitlePlural') : - i18n.t('homeScreen.dashboard.todayEventsSubtitle') - } - - ; - } else - subtitle = i18n.t('homeScreen.dashboard.todayEventsSubtitleNA'); - let displayEvent = this.getDisplayEvent(futureEvents); - const clickContainerAction = () => this.props.navigation.navigate('planning'); - const clickPreviewAction = () => this.props.navigation.navigate('home-planning-information', {data: displayEvent}); - + const clickPreviewAction = () => + this.props.navigation.navigate('home-planning-information', {data: displayEvent}); return ( @@ -367,66 +378,34 @@ class HomeScreen extends React.Component { ); } + dashboardRowRenderItem = ({item}: Object) => { + return( + + ); + }; + /** * Gets a classic dashboard item. * * @param content * @return {*} */ - getDashboardTopItem(content: Array) { - let proxiwashData = content[0]['data']; - let tutorinsaData = content[1]['data']; - let proximoData = content[2]['data']; - let menuData = content[3]['data']; - return ( - - 0} - badgeNumber={proxiwashData['washers']} - /> - 0} - badgeNumber={proxiwashData['dryers']} - /> - 0} - badgeNumber={tutorinsaData} - /> - 0} - badgeNumber={parseInt(proximoData)} - /> - 0} - badgeNumber={0} - /> - - ); - } - - openLink(link: string) { - Linking.openURL(link); + getDashboardRow(content: Array) { + return ; } /** @@ -436,7 +415,7 @@ class HomeScreen extends React.Component { * @return {*} */ getFeedItem(item: Object) { - const onOutLinkPress = this.openLink.bind(this, item.permalink_url); + const onOutLinkPress = () => Linking.openURL(item.permalink_url); return (