Skip to content

Commit 1938f18

Browse files
committed
added common function for starring and unstarring
1 parent 1cd8f25 commit 1938f18

File tree

6 files changed

+16
-46
lines changed

6 files changed

+16
-46
lines changed

src/gists/gists.actiontype.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const fetchGistComments = createSagaActionSet('FETCH_GIST_COMMENTS');
44
export const userGistsFetch = createSagaActionSet('FETCH_USER_GISTS');
55
export const starredGistsFetch = createSagaActionSet('FETCH_USER_STARRED_GISTS');
66
export const publicGistsFetch = createSagaActionSet('FETCH_PUBLIC_GISTS');
7-
export const starGist = createSagaActionSet('STAR_GIST');
7+
export const toggleFavoriteGist = createSagaActionSet('TOGGLE_FAVORITE_GIST');
88
export const fetchInitialFavoriteValue = createSagaActionSet('INITIAL_FAVORITE_VALUE');
99
export const UnstarGist = createSagaActionSet('UNSTAR_GIST');
1010
export const deleteComment = createSagaActionSet('DELETE_COMMENT');

src/gists/gists.reducer.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,12 @@ const clearCache = () => ({
4646

4747
const setGistComments = (state, { payload }) => {
4848
const { data, error } = payload;
49-
// const initialComments = payload.data.slice(key - 6, key);
5049

5150
return {
5251
...state,
5352
inProgress: false,
5453
error,
55-
comments: data, // initialComments,
56-
// hasMoreComments: key !== data.length,
54+
comments: data,
5755
};
5856
};
5957

src/gists/gists.saga.js

+6-22
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import {
55
starredGistsFetch,
66
publicGistsFetch,
77
fetchGistComments,
8-
starGist,
8+
toggleFavoriteGist,
99
fetchInitialFavoriteValue,
10-
UnstarGist,
1110
deleteComment,
1211
addComment,
1312
} from './gists.actiontype';
@@ -91,12 +90,11 @@ function* fetchCommentsForGist(action) {
9190
}
9291
}
9392

94-
function* starAGist(action) {
93+
function* toggleGist(action) {
9594
try {
9695
const token = yield select(tokenSelector);
97-
98-
yield put(starGist.progress());
99-
const status = yield call(requestStarGist, token, action.payload);
96+
const requestUrl = (action.payload.type === 'star') ? requestStarGist : requestUnstarGist;
97+
const status = yield call(requestUrl, token, action.payload.id);
10098

10199
if (status === 204) {
102100
yield call(fetchStarredGists, { shouldRefresh: true });
@@ -110,27 +108,14 @@ function* getInitialFavoriteValue(action) {
110108
try {
111109
const token = yield select(tokenSelector);
112110

111+
yield put(fetchInitialFavoriteValue.progress());
113112
yield call(checkStarredGistFavoriteValue, token, action.payload);
114113
yield put(fetchInitialFavoriteValue.success({ value: true }));
115114
} catch (err) {
116115
yield put(fetchInitialFavoriteValue.success({ value: false }));
117116
}
118117
}
119118

120-
function* unstarAGist(action) {
121-
try {
122-
const token = yield select(tokenSelector);
123-
124-
const status = yield call(requestUnstarGist, token, action.payload);
125-
126-
if (status === 204) {
127-
yield call(fetchStarredGists, { shouldRefresh: true });
128-
}
129-
} catch (err) {
130-
console.log(err);
131-
}
132-
}
133-
134119
function* deleteAComment(action) {
135120
try {
136121
const token = yield select(tokenSelector);
@@ -172,9 +157,8 @@ export default function* gistsSaga() {
172157
takeLatest(starredGistsFetch.actionType, fetchStarredGists),
173158
takeLatest(publicGistsFetch.actionType, fetchPublicGists),
174159
takeLatest(fetchGistComments.actionType, fetchCommentsForGist),
175-
takeLatest(starGist.actionType, starAGist),
160+
takeLatest(toggleFavoriteGist.actionType, toggleGist),
176161
takeLatest(fetchInitialFavoriteValue.actionType, getInitialFavoriteValue),
177-
takeLatest(UnstarGist.actionType, unstarAGist),
178162
takeLatest(deleteComment.actionType, deleteAComment),
179163
takeLatest(addComment.actionType, addAComment),
180164
]);

src/gists/screens/gistComments.screen.js

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import CardView from 'react-native-cardview';
1515
import TimeAgo from 'time-ago';
1616
import { fetchGistComments, deleteComment, addComment } from '../gists.actiontype';
1717
import ListEmptyComponent from './components/EmptyListComponent';
18-
// import { addComments } from '../../api';
1918
import { colors } from '../../config';
2019
import GistOptions from '../gistoptions.screen';
2120

src/gists/screens/gistdetails.screen.js

+8-14
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Header from './components/GistDetailHeader';
99
import Toolbar from './components/Toolbar';
1010
import { processFiles } from '../../shared/processFiles';
1111
import GistFileItem from './components/GistFileItem';
12-
import { starGist, fetchInitialFavoriteValue, UnstarGist } from '../gists.actiontype';
12+
import { toggleFavoriteGist, fetchInitialFavoriteValue } from '../gists.actiontype';
1313

1414
const HeaderProps = [
1515
'avatal_url',
@@ -48,18 +48,13 @@ class GistDetails extends React.Component {
4848

4949
handleActionButtonClick = () => {
5050
const { id } = this.props.navigation.getParam('gistData');
51+
const action = (this.state.iconName === 'star') ?
52+
{ type: 'unstar', iconName: 'star-o' } : { type: 'star', iconName: 'star' };
5153

52-
if (this.state.iconName === 'star') {
53-
this.props.UnstarThisGist(id);
54-
this.setState({
55-
iconName: 'star-o',
56-
});
57-
} else {
58-
this.props.starThisGist(id);
59-
this.setState({
60-
iconName: 'star',
61-
});
62-
}
54+
this.props.toggleGist({ id, type: action.type });
55+
this.setState({
56+
iconName: action.iconName,
57+
});
6358
}
6459

6560
renderItem = ({ item }) => (
@@ -120,9 +115,8 @@ class GistDetails extends React.Component {
120115
}
121116

122117
const mapDispatchToProps = dispatch => ({
123-
starThisGist: id => dispatch(starGist.action(id)),
118+
toggleGist: data => dispatch(toggleFavoriteGist.action(data)),
124119
checkIfGistIsStarred: id => dispatch(fetchInitialFavoriteValue.action(id)),
125-
UnstarThisGist: id => dispatch(UnstarGist.action(id)),
126120
});
127121

128122
const mapStateToProps = ({ initialFavoriteValue }) =>

src/routes.js

-5
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,8 @@ const GistFileContentAndCommentsScreen = TabNavigator({
7373
style: styles.tabStyle,
7474
labelStyle: styles.labelStyle,
7575
},
76-
7776
});
7877

79-
// GistFileContentAndCommentsScreen.navigationOptions = {
80-
// header: GistDetailsScreen,
81-
// };
82-
8378
const MainScreen = StackNavigator({
8479
MainTabs: {
8580
screen: MainTabsScreen,

0 commit comments

Comments
 (0)