From 1d0fa0f613e184982ac6719b554abde1edbde738 Mon Sep 17 00:00:00 2001 From: u-e Date: Wed, 23 Jan 2019 18:00:26 +0300 Subject: [PATCH] created bookmarks feature --- .../view/userListItem/userListItem.js | 3 +- .../container/postDropdownContainer.js | 18 +++++++- .../postElements/body/view/postBodyView.js | 2 +- src/config/locales/en-US.json | 7 ++- src/constants/options/post.js | 2 +- src/providers/esteem/esteem.js | 2 +- .../bookmarks/container/bookmarksContainer.js | 46 ++++++++++++++++++- .../bookmarks/screen/bookmarksScreen.js | 36 +++++++++------ 8 files changed, 93 insertions(+), 23 deletions(-) diff --git a/src/components/basicUIElements/view/userListItem/userListItem.js b/src/components/basicUIElements/view/userListItem/userListItem.js index e46147f03..8d4b2eb0e 100644 --- a/src/components/basicUIElements/view/userListItem/userListItem.js +++ b/src/components/basicUIElements/view/userListItem/userListItem.js @@ -17,6 +17,7 @@ const UserListItem = ({ handleOnPress, handleOnLongPress, isClickable, + text, }) => ( handleOnLongPress && handleOnLongPress()} @@ -27,7 +28,7 @@ const UserListItem = ({ {itemIndex && {itemIndex}} - {username} + {text || username} {description && {description}} {isHasRightItem && ( diff --git a/src/components/postDropdown/container/postDropdownContainer.js b/src/components/postDropdown/container/postDropdownContainer.js index 8b1840561..ca51378a2 100644 --- a/src/components/postDropdown/container/postDropdownContainer.js +++ b/src/components/postDropdown/container/postDropdownContainer.js @@ -7,8 +7,7 @@ import { injectIntl } from 'react-intl'; // Services and Actions import { reblog } from '../../../providers/steem/dsteem'; - -// Middleware +import { addBookmark } from '../../../providers/esteem/esteem'; // Constants import OPTIONS from '../../../constants/options/post'; @@ -59,6 +58,10 @@ class PostDropdownContainer extends PureComponent { }, 500); break; + case '4': + this._addToBookmarks(); + break; + default: break; } @@ -73,6 +76,17 @@ class PostDropdownContainer extends PureComponent { }); }; + _addToBookmarks = () => { + const { currentAccount, content, intl } = this.props; + addBookmark(currentAccount.name, content.author, content.permlink) + .then(() => { + Alert.alert(intl.formatMessage({ id: 'bookmarks.added' })); + }) + .catch(() => { + Alert.alert(intl.formatMessage({ id: 'alert.fail' })); + }); + } + _reblog = () => { const { currentAccount, content, isLoggedIn, pinCode, intl, diff --git a/src/components/postElements/body/view/postBodyView.js b/src/components/postElements/body/view/postBodyView.js index 8375f61f0..d6293e357 100644 --- a/src/components/postElements/body/view/postBodyView.js +++ b/src/components/postElements/body/view/postBodyView.js @@ -112,7 +112,7 @@ class PostBody extends PureComponent { } if (_className === 'text-justify') { - node.attribs.style = 'text-align: justify; text-justify: inter-word; letter-spacing: 1.2px;'; + node.attribs.style = 'text-align: justify; text-justify: inter-word; letter-spacing: 0px;'; } if (_className === 'phishy') { diff --git a/src/config/locales/en-US.json b/src/config/locales/en-US.json index 17a5526b1..539f7abb6 100644 --- a/src/config/locales/en-US.json +++ b/src/config/locales/en-US.json @@ -158,7 +158,9 @@ "load_error": "Could not load bookmarks", "empty_list": "Nothing here", "deleted": "Bookmark removed", - "search": "Search in bookmarks" + "search": "Search in bookmarks", + "added": "Added to bookmars", + "add": "Add to bookmarks" }, "favorites": { "title": "Favorites", @@ -184,7 +186,8 @@ "copy": "copy link", "reblog": "reblog", "reply": "reply", - "share": "share" + "share": "share", + "bookmarks": "added to bookmarks" }, "deep_link": { "no_existing_user": "No existing user", diff --git a/src/constants/options/post.js b/src/constants/options/post.js index 8831e207f..e7d956876 100644 --- a/src/constants/options/post.js +++ b/src/constants/options/post.js @@ -1 +1 @@ -export default ['copy', 'reblog', 'reply', 'share']; +export default ['copy', 'reblog', 'reply', 'share', 'bookmarks']; diff --git a/src/providers/esteem/esteem.js b/src/providers/esteem/esteem.js index d3b8f5a72..af730c9b8 100644 --- a/src/providers/esteem/esteem.js +++ b/src/providers/esteem/esteem.js @@ -89,7 +89,7 @@ export const getBookmarks = username => api.get(`/bookmarks/${username}`).then(r * @params id * @params current username */ -export const removeBookmark = (id, username) => api.delete(`/bookmarks/${username}/${id}`); +export const removeBookmark = (username, id) => api.delete(`/bookmarks/${username}/${id}`); /** * @params current username diff --git a/src/screens/bookmarks/container/bookmarksContainer.js b/src/screens/bookmarks/container/bookmarksContainer.js index c46736346..cd0dffcb1 100644 --- a/src/screens/bookmarks/container/bookmarksContainer.js +++ b/src/screens/bookmarks/container/bookmarksContainer.js @@ -4,7 +4,7 @@ import { Alert } from 'react-native'; import { injectIntl } from 'react-intl'; // Services and Actions -import { getFavorites, removeFavorite } from '../../../providers/esteem/esteem'; +import { getFavorites, removeFavorite, getBookmarks, removeBookmark } from '../../../providers/esteem/esteem'; // Constants import ROUTES from '../../../constants/routeNames'; @@ -39,6 +39,7 @@ class DraftsContainer extends Component { _fetchData = () => { this._getFavorites(); + this._getBookmarks(); }; _getFavorites = () => { @@ -55,6 +56,20 @@ class DraftsContainer extends Component { }); }; + _getBookmarks = () => { + const { currentAccount, intl } = this.props; + this.setState({ isLoading: true }); + + getBookmarks(currentAccount.name) + .then((data) => { + this.setState({ bookmarks: this._sortData(data), isLoading: false }); + }) + .catch(() => { + Alert.alert(intl.formatMessage({ id: 'bookmarks.load_error' })); + this.setState({ isLoading: false }); + }); + }; + _removeFavorite = (selectedUsername) => { const { currentAccount, intl } = this.props; @@ -70,6 +85,21 @@ class DraftsContainer extends Component { }); }; + _removeBoomark = (id) => { + const { currentAccount, intl } = this.props; + + removeBookmark(currentAccount.name, id) + .then(() => { + const { bookmarks } = this.state; + const newBookmarks = [...bookmarks].filter(bookmark => bookmark._id !== id); + + this.setState({ bookmarks: this._sortData(newBookmarks) }); + }) + .catch(() => { + Alert.alert(intl.formatMessage({ id: 'alert.fail' })); + }); + }; + _handleOnFavoritePress = (username) => { const { navigation } = this.props; @@ -82,6 +112,18 @@ class DraftsContainer extends Component { }); }; + _handleOnBookarkPress = (permlink, author) => { + const { navigation } = this.props; + + navigation.navigate({ + routeName: ROUTES.SCREENS.POST, + params: { + permlink, + author, + }, + }); + }; + _sortData = data => data.sort((a, b) => { const dateA = new Date(a.created).getTime(); const dateB = new Date(b.created).getTime(); @@ -100,7 +142,9 @@ class DraftsContainer extends Component { favorites={favorites} bookmarks={bookmarks} removeFavorite={this._removeFavorite} + removeBookmark={this._removeBoomark} handleOnFavoritePress={this._handleOnFavoritePress} + handleOnBookarkPress={this._handleOnBookarkPress} /> ); } diff --git a/src/screens/bookmarks/screen/bookmarksScreen.js b/src/screens/bookmarks/screen/bookmarksScreen.js index de4382070..1829c8d95 100644 --- a/src/screens/bookmarks/screen/bookmarksScreen.js +++ b/src/screens/bookmarks/screen/bookmarksScreen.js @@ -26,25 +26,30 @@ class BookmarksScreen extends Component { constructor(props) { super(props); this.state = { - selectedUsername: null, + selectedItemId: null, + activeTab: 0, }; } // Component Life Cycles // Component Functions - _renderItem = (item, index) => { - const { handleOnFavoritePress } = this.props; + _renderItem = (item, index, itemType) => { + const { handleOnFavoritePress, handleOnBookarkPress } = this.props; + const isFavorites = itemType === 'favorites'; + const text = isFavorites ? item.account : `${item.author}/${item.permlink}`; return ( this._handleLongPress(item.account)} - handleOnPress={() => handleOnFavoritePress(item.account)} + handleOnLongPress={() => this._handleLongPress(isFavorites ? item.account : item._id)} + handleOnPress={() => (isFavorites + ? handleOnFavoritePress(item.account) + : handleOnBookarkPress(item.permlink, item.author)) + } index={index} isClickable - username={item.account} - rightText="bok" - subRightText="bok" + text={text} + username={item.author} /> ); }; @@ -71,7 +76,7 @@ class BookmarksScreen extends Component { data={data} keyExtractor={item => item._id} removeClippedSubviews={false} - renderItem={({ item, index }) => this._renderItem(item, index)} + renderItem={({ item, index }) => this._renderItem(item, index, type)} /> ) )} @@ -79,17 +84,17 @@ class BookmarksScreen extends Component { ); }; - _handleLongPress = (selectedUsername) => { - this.setState({ selectedUsername }, () => { + _handleLongPress = (selectedItemId) => { + this.setState({ selectedItemId }, () => { this.ActionSheet.show(); }); }; render() { const { - favorites, bookmarks, intl, removeFavorite, + favorites, bookmarks, intl, removeFavorite, removeBookmark, } = this.props; - const { selectedUsername } = this.state; + const { selectedItemId, activeTab } = this.state; return ( @@ -100,6 +105,7 @@ class BookmarksScreen extends Component { /> this.setState({ activeTab: event.i })} style={globalStyles.tabView} renderTabBar={() => ( { - if (index === 0) removeFavorite(selectedUsername); + if (index === 0) { + activeTab === 0 ? removeBookmark(selectedItemId) : removeFavorite(selectedItemId); + } }} />