Skip to content

Commit

Permalink
Show the list of report names as decent links
Browse files Browse the repository at this point in the history
Fixes #24
Fixes #21
Fixes #20
  • Loading branch information
tgolen committed Aug 9, 2020
1 parent 74d5455 commit da8177c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
32 changes: 24 additions & 8 deletions src/components/WithStoreSubscribeToState.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ export default function (mapStoreToStates) {

componentDidMount() {
// Subscribe each of the state properties to the proper store key
this.subscriptionIDs = _.reduce(mapStoreToStates, (finalResult, mapStoreToState, propertyName) => {
_.each(mapStoreToStates, (mapStoreToState, propertyName) => {
const {key, path} = mapStoreToState;
return [
...finalResult,
Store.bind(key, propertyName, path, null, this.wrappedComponent),
];
}, []);
this.bind(key, propertyName, path, this.wrappedComponent);
});

// Call any loaders that will fill the store with their initial data
_.each(mapStoreToStates, (mapStoreToState) => {
Expand All @@ -43,10 +40,29 @@ export default function (mapStoreToStates) {
_.each(this.subscriptionIDs, Store.unsubscribeFromState);
}

/**
* A method that is convenient to bind the state to the store. Typically used when you can't pass
* mapStoreToStates to this HOC. All subscriptions will automatically be unbound when unmounted
*
* @param {string} key
* @param {string} propertyName
* @param {string} path
* @param {object} component
*/
bind(key, propertyName, path, component) {
this.subscriptionIDs.push(Store.bind(key, propertyName, path, null, component));
}

render() {
// Spreading props and state is necessary in an HOC where the data cannot be predicted
// eslint-disable-next-line react/jsx-props-no-spreading
return <WrappedComponent {...this.props} {...this.state} ref={el => this.wrappedComponent = el} />;
return <WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...this.props}
// eslint-disable-next-line react/jsx-props-no-spreading
{...this.state}
ref={el => this.wrappedComponent = el}
bind={this.bind}
/>;
}
};
}
35 changes: 31 additions & 4 deletions src/page/HomePage/SidebarLink.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import React from 'react';
import PropTypes from 'prop-types';
import {Text, View} from 'react-native';
import {Link} from '../../lib/Router';
import * as Store from '../../store/Store';
import STOREKEYS from '../../store/STOREKEYS';
import WithStoreSubscribeToState from '../../components/WithStoreSubscribeToState';

const propTypes = {
// The ID of the report for this link
Expand All @@ -11,14 +15,37 @@ const propTypes = {
};

class SidebarLink extends React.Component {
constructor(props) {
super(props);

this.subscriptionIDS = [];

this.state = {
hasUnread: false,
};
}

componentDidMount() {
this.subscriptionIDS.push(Store.bind(`${STOREKEYS.REPORT}_${this.props.reportID}`, 'hasUnread', 'hasUnread', false, this));
}

componentWillUnmount() {

}

render() {
return (
<Link to={`/${this.props.reportID}`}>
{this.props.reportName}
</Link>
<View>
<Link to={`/${this.props.reportID}`} style={{padding: 10, textDecorationLine: 'none'}}>
<Text>{this.props.reportName}</Text>
{this.state.hasUnread && (
<Text>- Unread</Text>
)}
</Link>
</View>
);
}
}
SidebarLink.propTypes = propTypes;

export default SidebarLink;
export default WithStoreSubscribeToState()(SidebarLink);
5 changes: 5 additions & 0 deletions src/style/StyleSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const styles = StyleSheet.create({
navText: {
padding: 8,
},
// @TODO I can't get this style to work in `SidebarLink.js`
sidebarLink: {
padding: 8,
textDecorationLine: 'none',
},
});

export default styles;

0 comments on commit da8177c

Please sign in to comment.