drafts sorting issue, hooks

This commit is contained in:
feruz 2020-01-07 05:57:28 +02:00
parent c6b19dfb22
commit 0f0af30d52
3 changed files with 137 additions and 157 deletions

View File

@ -26,7 +26,9 @@ export const getCurrencyTokenRate = (currency, token) =>
/** /**
* @params username * @params username
*/ */
export const getDrafts = data => export const getDrafts = username => api.get(`/drafts/${username}`).then(resp => resp.data);
/*export const getDrafts = data =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
api api
.get(`/drafts/${data}`) .get(`/drafts/${data}`)
@ -38,7 +40,7 @@ export const getDrafts = data =>
reject(error); reject(error);
}); });
}); });
*/
/** /**
* @params username * @params username
* @params draftID * @params draftID

View File

@ -1,4 +1,4 @@
import React, { Component } from 'react'; import React, { useEffect, useState } from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { Alert } from 'react-native'; import { Alert } from 'react-native';
import { injectIntl } from 'react-intl'; import { injectIntl } from 'react-intl';
@ -23,91 +23,70 @@ import { default as ROUTES } from '../../../constants/routeNames';
// Component // Component
import DraftsScreen from '../screen/draftsScreen'; import DraftsScreen from '../screen/draftsScreen';
/* const DraftsContainer = ({ currentAccount, intl, navigation, dispatch }) => {
* Props Name Description Value const [drafts, setDrafts] = useState([]);
*@props --> props name here description here Value Type Here const [schedules, setSchedules] = useState([]);
* const [isLoading, setIsLoading] = useState(false);
*/
class DraftsContainer extends Component { useEffect(() => {
constructor(props) { _getDrafts();
super(props); _getSchedules();
this.state = { }, []);
drafts: [],
schedules: [],
isLoading: false,
};
}
// Component Life Cycle Functions
componentDidMount() {
this._getDrafts();
this._getSchedules();
}
// Component Functions // Component Functions
_getSchedules = () => { const _getSchedules = () => {
const { currentAccount, intl } = this.props; setIsLoading(true);
this.setState({ isLoading: true });
getSchedules(currentAccount.name) getSchedules(currentAccount.name)
.then(data => { .then(data => {
this.setState({ schedules: this._sortData(data, true), isLoading: false }); setSchedules(_sortDataS(data));
setIsLoading(false);
}) })
.catch(() => { .catch(() => {
Alert.alert(intl.formatMessage({ id: 'drafts.load_error' })); Alert.alert(intl.formatMessage({ id: 'drafts.load_error' }));
this.setState({ isLoading: false }); setIsLoading(false);
}); });
}; };
_getDrafts = () => { const _getDrafts = () => {
const { currentAccount, intl } = this.props; setIsLoading(true);
this.setState({ isLoading: true });
getDrafts(currentAccount.name) getDrafts(currentAccount.name)
.then(data => { .then(data => {
this.setState({ drafts: this._sortData(data), isLoading: false }); setDrafts(_sortData(data));
setIsLoading(false);
}) })
.catch(() => { .catch(() => {
Alert.alert(intl.formatMessage({ id: 'drafts.load_error' })); Alert.alert(intl.formatMessage({ id: 'drafts.load_error' }));
this.setState({ isLoading: false }); setIsLoading(false);
}); });
}; };
_removeDraft = id => { const _removeDraft = id => {
const { currentAccount, intl } = this.props;
removeDraft(currentAccount.name, id) removeDraft(currentAccount.name, id)
.then(() => { .then(() => {
const { drafts } = this.state;
const newDrafts = [...drafts].filter(draft => draft._id !== id); const newDrafts = [...drafts].filter(draft => draft._id !== id);
setDrafts(_sortData(newDrafts));
this.setState({ drafts: this._sortData(newDrafts) });
}) })
.catch(() => { .catch(() => {
Alert.alert(intl.formatMessage({ id: 'alert.fail' })); Alert.alert(intl.formatMessage({ id: 'alert.fail' }));
}); });
}; };
_removeSchedule = id => { const _removeSchedule = id => {
const { currentAccount, intl } = this.props;
removeSchedule(currentAccount.name, id) removeSchedule(currentAccount.name, id)
.then(res => { .then(res => {
const { schedules } = this.state;
const newSchedules = [...schedules].filter(schedule => schedule._id !== id); const newSchedules = [...schedules].filter(schedule => schedule._id !== id);
this.setState({ schedules: this._sortData(newSchedules, true) }); setSchedules(_sortDataS(newSchedules));
}) })
.catch(() => { .catch(() => {
Alert.alert(intl.formatMessage({ id: 'alert.fail' })); Alert.alert(intl.formatMessage({ id: 'alert.fail' }));
}); });
}; };
_moveScheduleToDraft = id => { const _moveScheduleToDraft = id => {
const { currentAccount, dispatch, intl } = this.props;
moveSchedule(id, currentAccount.name) moveSchedule(id, currentAccount.name)
.then(res => { .then(res => {
dispatch( dispatch(
@ -118,54 +97,55 @@ class DraftsContainer extends Component {
), ),
); );
this._getDrafts(); _getDrafts();
this._getSchedules(); _getSchedules();
}) })
.catch(() => { .catch(() => {
dispatch(toastNotification(intl.formatMessage({ id: 'alert.fail' }))); dispatch(toastNotification(intl.formatMessage({ id: 'alert.fail' })));
}); });
}; };
_editDraft = id => { const _editDraft = id => {
const { navigation } = this.props;
const { drafts } = this.state;
const selectedDraft = drafts.find(draft => draft._id === id); const selectedDraft = drafts.find(draft => draft._id === id);
navigation.navigate({ navigation.navigate({
routeName: ROUTES.SCREENS.EDITOR, routeName: ROUTES.SCREENS.EDITOR,
params: { params: {
draft: selectedDraft, draft: selectedDraft,
fetchPost: this._getDrafts, fetchPost: _getDrafts,
}, },
}); });
}; };
_sortData = (data, isSchedule) => const _sortData = data =>
data.sort((a, b) => { data.sort((a, b) => {
const dateA = new Date(isSchedule ? a.schedule : a.created).getTime(); const dateA = new Date(a.created).getTime();
const dateB = new Date(isSchedule ? a.schedule : b.created).getTime(); const dateB = new Date(b.created).getTime();
return dateB > dateA ? 1 : -1; return dateB > dateA ? 1 : -1;
}); });
render() { const _sortDataS = data =>
const { isLoading, drafts, schedules } = this.state; data.sort((a, b) => {
const { currentAccount } = this.props; const dateA = new Date(a.schedule).getTime();
const dateB = new Date(b.schedule).getTime();
return dateB > dateA ? 1 : -1;
});
return ( return (
<DraftsScreen <DraftsScreen
isLoading={isLoading} isLoading={isLoading}
editDraft={this._editDraft} editDraft={_editDraft}
currentAccount={currentAccount} currentAccount={currentAccount}
drafts={drafts} drafts={drafts}
schedules={schedules} schedules={schedules}
removeDraft={this._removeDraft} removeDraft={_removeDraft}
moveScheduleToDraft={this._moveScheduleToDraft} moveScheduleToDraft={_moveScheduleToDraft}
removeSchedule={this._removeSchedule} removeSchedule={_removeSchedule}
/> />
); );
} };
}
const mapStateToProps = state => ({ const mapStateToProps = state => ({
currentAccount: state.account.currentAccount, currentAccount: state.account.currentAccount,

View File

@ -1,5 +1,5 @@
/* eslint-disable react/jsx-wrap-multilines */ /* eslint-disable react/jsx-wrap-multilines */
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';
@ -17,25 +17,34 @@ import { BasicHeader, TabBar, PostListItem, PostCardPlaceHolder } from '../../..
import globalStyles from '../../../globalStyles'; import globalStyles from '../../../globalStyles';
import styles from './draftStyles'; import styles from './draftStyles';
class DraftsScreen extends Component { const DraftsScreen = ({
/* Props currentAccount,
* ------------------------------------------------ removeDraft,
* @prop { type } name - Description.... editDraft,
*/ removeSchedule,
isLoading,
intl,
drafts,
schedules,
moveScheduleToDraft,
}) => {
const [selectedId, setSelectedId] = useState(null);
const ActionSheetRef = useRef(null);
const firstMount = useRef(true);
constructor(props) { useEffect(() => {
super(props); if (firstMount.current) {
this.state = { firstMount.current = false;
selectedId: null, return;
};
} }
if (ActionSheetRef.current) {
// Component Life Cycles ActionSheetRef.current.show();
}
}, [selectedId]);
// Component Functions // Component Functions
_renderItem = (item, type) => { const _renderItem = (item, type) => {
const { currentAccount, removeDraft, editDraft, removeSchedule } = this.props;
const tags = item.tags ? item.tags.split(/[ ,]+/) : []; const tags = item.tags ? item.tags.split(/[ ,]+/) : [];
const tag = tags[0] || ''; const tag = tags[0] || '';
const image = catchDraftImage(item.body); const image = catchDraftImage(item.body);
@ -52,11 +61,7 @@ class DraftsScreen extends Component {
image={image ? { uri: catchDraftImage(item.body) } : null} image={image ? { uri: catchDraftImage(item.body) } : null}
username={currentAccount.name} username={currentAccount.name}
reputation={currentAccount.reputation} reputation={currentAccount.reputation}
handleOnPressItem={() => handleOnPressItem={() => (isSchedules ? setSelectedId(item._id) : editDraft(item._id))}
isSchedules
? this.setState({ selectedId: item._id }, () => this.ActionSheet.show())
: editDraft(item._id)
}
handleOnRemoveItem={isSchedules ? removeSchedule : removeDraft} handleOnRemoveItem={isSchedules ? removeSchedule : removeDraft}
id={item._id} id={item._id}
key={item._id} key={item._id}
@ -64,9 +69,7 @@ class DraftsScreen extends Component {
); );
}; };
_renderEmptyContent = () => { const _renderEmptyContent = () => {
const { isLoading, intl } = this.props;
if (isLoading) { if (isLoading) {
return ( return (
<View> <View>
@ -85,22 +88,18 @@ class DraftsScreen extends Component {
); );
}; };
_getTabItem = (data, type) => ( const _getTabItem = (data, type) => (
<View style={globalStyles.lightContainer}> <View style={globalStyles.lightContainer}>
<FlatList <FlatList
data={data} data={data}
keyExtractor={item => item._id} keyExtractor={item => item._id}
removeClippedSubviews={false} removeClippedSubviews={false}
renderItem={({ item }) => this._renderItem(item, type)} renderItem={({ item }) => _renderItem(item, type)}
ListEmptyComponent={this._renderEmptyContent()} ListEmptyComponent={_renderEmptyContent()}
/> />
</View> </View>
); );
render() {
const { drafts, schedules, intl, moveScheduleToDraft } = this.props;
const { selectedId } = this.state;
return ( return (
<View style={globalStyles.container}> <View style={globalStyles.container}>
<BasicHeader <BasicHeader
@ -126,7 +125,7 @@ class DraftsScreen extends Component {
})} })}
style={styles.tabbarItem} style={styles.tabbarItem}
> >
{this._getTabItem(drafts, 'drafts')} {_getTabItem(drafts, 'drafts')}
</View> </View>
<View <View
tabLabel={intl.formatMessage({ tabLabel={intl.formatMessage({
@ -134,11 +133,11 @@ class DraftsScreen extends Component {
})} })}
style={styles.tabbarItem} style={styles.tabbarItem}
> >
{this._getTabItem(schedules, 'schedules')} {_getTabItem(schedules, 'schedules')}
</View> </View>
</ScrollableTabView> </ScrollableTabView>
<ActionSheet <ActionSheet
ref={o => (this.ActionSheet = o)} ref={ActionSheetRef}
title={intl.formatMessage({ title={intl.formatMessage({
id: 'alert.move_question', id: 'alert.move_question',
})} })}
@ -157,8 +156,7 @@ class DraftsScreen extends Component {
/> />
</View> </View>
); );
} };
}
export default injectIntl(DraftsScreen); export default injectIntl(DraftsScreen);
/* eslint-enable */ /* eslint-enable */