bookmark page converted to hooks

This commit is contained in:
feruz 2019-12-19 18:03:32 +02:00
parent 38c134d43e
commit e405520582

View File

@ -1,4 +1,4 @@
import React, { Component } from 'react'; import React, { useState, useRef, useEffect } from 'react';
import { injectIntl } from 'react-intl'; import { injectIntl } from 'react-intl';
import { View, FlatList, Text } from 'react-native'; import { View, FlatList, Text } from 'react-native';
import ScrollableTabView from 'react-native-scrollable-tab-view'; import ScrollableTabView from 'react-native-scrollable-tab-view';
@ -11,32 +11,39 @@ import { UserListItem, WalletDetailsPlaceHolder, BasicHeader, TabBar } from '../
import globalStyles from '../../../globalStyles'; import globalStyles from '../../../globalStyles';
import styles from './bookmarksStyles'; import styles from './bookmarksStyles';
class BookmarksScreen extends Component { const BookmarksScreen = ({
/* Props isLoading,
* ------------------------------------------------ intl,
* @prop { type } name - Description.... handleOnFavoritePress,
*/ handleOnBookarkPress,
favorites,
bookmarks,
removeFavorite,
removeBookmark,
}) => {
const [selectedItemId, setSelectedItemId] = useState(null);
const [activeTab, setActiveTab] = useState(0);
const ActionSheetRef = useRef(null);
const firstMount = useRef(true);
constructor(props) { useEffect(() => {
super(props); if (firstMount.current) {
this.state = { firstMount.current = false;
selectedItemId: null, return;
activeTab: 0, }
}; if (ActionSheetRef.current) {
} ActionSheetRef.current.show();
}
}, [selectedItemId]);
// Component Life Cycles const _renderItem = (item, index, itemType) => {
// Component Functions
_renderItem = (item, index, itemType) => {
const { handleOnFavoritePress, handleOnBookarkPress } = this.props;
const isFavorites = itemType === 'favorites'; const isFavorites = itemType === 'favorites';
const text = isFavorites ? item.account : `${item.author}/${item.permlink}`; const text = isFavorites ? item.account : `${item.author}/${item.permlink}`;
if (item.author || item.account) { if (item.author || item.account) {
return ( return (
<UserListItem <UserListItem
handleOnLongPress={() => this._handleLongPress(isFavorites ? item.account : item._id)} handleOnLongPress={() => _handleLongPress(isFavorites ? item.account : item._id)}
handleOnPress={() => handleOnPress={() =>
isFavorites isFavorites
? handleOnFavoritePress(item.account) ? handleOnFavoritePress(item.account)
@ -51,9 +58,7 @@ class BookmarksScreen extends Component {
} }
}; };
_renderEmptyContent = () => { const _renderEmptyContent = () => {
const { isLoading, intl } = this.props;
if (isLoading) { if (isLoading) {
return <WalletDetailsPlaceHolder />; return <WalletDetailsPlaceHolder />;
} }
@ -67,7 +72,7 @@ class BookmarksScreen extends Component {
); );
}; };
_getTabItem = (data, type) => { const _getTabItem = (data, type) => {
const isFavorites = type === 'favorites'; const isFavorites = type === 'favorites';
return ( return (
@ -80,78 +85,70 @@ class BookmarksScreen extends Component {
)} )}
keyExtractor={item => item._id} keyExtractor={item => item._id}
removeClippedSubviews={false} removeClippedSubviews={false}
renderItem={({ item, index }) => this._renderItem(item, index, type)} renderItem={({ item, index }) => _renderItem(item, index, type)}
ListEmptyComponent={this._renderEmptyContent()} ListEmptyComponent={_renderEmptyContent()}
/> />
</View> </View>
); );
}; };
const _handleLongPress = _selectedItemId => {
_handleLongPress = selectedItemId => { setSelectedItemId(_selectedItemId);
this.setState({ selectedItemId }, () => {
this.ActionSheet.show();
});
}; };
render() { return (
const { favorites, bookmarks, intl, removeFavorite, removeBookmark } = this.props; <View style={globalStyles.container}>
const { selectedItemId, activeTab } = this.state; <BasicHeader
title={intl.formatMessage({
id: 'bookmarks.title',
})}
/>
return ( <ScrollableTabView
<View style={globalStyles.container}> onChangeTab={event => setActiveTab(event.i)}
<BasicHeader style={globalStyles.tabView}
title={intl.formatMessage({ renderTabBar={() => (
<TabBar
style={styles.tabbar}
tabUnderlineDefaultWidth={80}
tabUnderlineScaleX={2}
tabBarPosition="overlayTop"
/>
)}
>
<View
tabLabel={intl.formatMessage({
id: 'bookmarks.title', id: 'bookmarks.title',
})} })}
/> style={styles.tabbarItem}
<ScrollableTabView
onChangeTab={event => this.setState({ activeTab: event.i })}
style={globalStyles.tabView}
renderTabBar={() => (
<TabBar
style={styles.tabbar}
tabUnderlineDefaultWidth={80}
tabUnderlineScaleX={2}
tabBarPosition="overlayTop"
/>
)}
> >
<View {_getTabItem(bookmarks, 'bookmarks')}
tabLabel={intl.formatMessage({ </View>
id: 'bookmarks.title', <View
})} tabLabel={intl.formatMessage({
style={styles.tabbarItem} id: 'favorites.title',
> })}
{this._getTabItem(bookmarks, 'bookmarks')} style={styles.tabbarItem}
</View> >
<View {_getTabItem(favorites, 'favorites')}
tabLabel={intl.formatMessage({ </View>
id: 'favorites.title', </ScrollableTabView>
})} <ActionSheet
style={styles.tabbarItem} ref={ActionSheetRef}
> options={[
{this._getTabItem(favorites, 'favorites')} intl.formatMessage({ id: 'alert.delete' }),
</View> intl.formatMessage({ id: 'alert.cancel' }),
</ScrollableTabView> ]}
<ActionSheet title={intl.formatMessage({ id: 'alert.remove_alert' })}
ref={o => (this.ActionSheet = o)} cancelButtonIndex={1}
options={[ destructiveButtonIndex={0}
intl.formatMessage({ id: 'alert.delete' }), onPress={index => {
intl.formatMessage({ id: 'alert.cancel' }), if (index === 0) {
]} activeTab === 0 ? removeBookmark(selectedItemId) : removeFavorite(selectedItemId);
title={intl.formatMessage({ id: 'alert.remove_alert' })} }
cancelButtonIndex={1} }}
destructiveButtonIndex={0} />
onPress={index => { </View>
if (index === 0) { );
activeTab === 0 ? removeBookmark(selectedItemId) : removeFavorite(selectedItemId); };
}
}}
/>
</View>
);
}
}
export default injectIntl(BookmarksScreen); export default injectIntl(BookmarksScreen);