Skip to content

Commit b79be12

Browse files
committed
added unstar gist
1 parent 78b9001 commit b79be12

File tree

5 files changed

+58
-32
lines changed

5 files changed

+58
-32
lines changed

src/api/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,14 @@ export const v3 = {
7878
getDataFromPutRequest: async(url, accessToken) => {
7979
const response = await v3.call(url, v3.parameters(accessToken, METHOD.PUT, ACCEPT.JSON));
8080

81+
return response.status;
82+
},
83+
performDeleteRequest: async(url, accessToken) => {
84+
const response = await v3.call(url, v3.parameters(accessToken, METHOD.DELETE));
85+
8186
console.log('from api', response);
8287

83-
return response.data;
88+
return response.status;
8489
},
8590
};
8691

@@ -122,3 +127,5 @@ export const requestGistComments = async(accessToken, gistId) => await v3.getJso
122127
export const requestStarGist = async(accessToken, gistId) => v3.getDataFromPutRequest(`/gists/${gistId}/star`, accessToken);
123128

124129
export const checkStarredGistFavoriteValue = async(accessToken, gistId) => v3.getJson(`/gists/${gistId}/star`, accessToken);
130+
131+
export const requestUnstarGist = async(accessToken, gistId) => v3.performDeleteRequest(`/gists/${gistId}/star`, accessToken);

src/gists/gists.actiontype.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export const starredGistsFetch = createSagaActionSet('FETCH_USER_STARRED_GISTS')
66
export const publicGistsFetch = createSagaActionSet('FETCH_PUBLIC_GISTS');
77
export const starGist = createSagaActionSet('STAR_GIST');
88
export const fetchInitialFavoriteValue = createSagaActionSet('INITIAL_FAVORITE_VALUE');
9+
export const UnstarGist = createSagaActionSet('UNSTAR_GIST');

src/gists/gists.reducer.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,9 @@ const setGistComments = (state, { payload }) => {
5757
};
5858
};
5959

60-
export const setFavoriteValue = (state, { payload }) => {
60+
const setFavoriteValue = (state, { payload }) => {
6161
const { value } = payload;
6262

63-
6463
return {
6564
...state,
6665
isStarred: value,
@@ -104,5 +103,5 @@ export default {
104103
),
105104
initialFavoriteValue: createReducer({
106105
[fetchInitialFavoriteValue.successType]: setFavoriteValue,
107-
}),
106+
}, { isStarred: false }),
108107
};

src/gists/gists.saga.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
fetchGistComments,
88
starGist,
99
fetchInitialFavoriteValue,
10+
UnstarGist,
1011
} from './gists.actiontype';
1112
import {
1213
requestUserGists,
@@ -15,6 +16,7 @@ import {
1516
requestGistComments,
1617
requestStarGist,
1718
checkStarredGistFavoriteValue,
19+
requestUnstarGist,
1820
} from '../api';
1921

2022
const tokenSelector = state => state.auth.access_token;
@@ -90,10 +92,12 @@ function* fetchCommentsForGist(action) {
9092
function* starAGist(action) {
9193
try {
9294
const token = yield select(tokenSelector);
93-
const data = yield call(requestStarGist, token, action.payload);
95+
const status = yield call(requestStarGist, token, action.payload);
9496

95-
if (data.status === 204) {
97+
if (status === 204) {
9698
yield call(fetchStarredGists, { shouldRefresh: true });
99+
100+
return;
97101
}
98102
} catch (err) {
99103
console.log(err);
@@ -103,15 +107,27 @@ function* starAGist(action) {
103107
function* getInitialFavoriteValue(action) {
104108
try {
105109
const token = yield select(tokenSelector);
106-
const data = yield call(checkStarredGistFavoriteValue, token, action.payload);
107110

108-
console.log('data', data);
111+
yield call(checkStarredGistFavoriteValue, token, action.payload);
109112
yield put(fetchInitialFavoriteValue.success({ value: true }));
110-
111-
return data;
112113
} catch (err) {
113114
yield put(fetchInitialFavoriteValue.success({ value: false }));
114-
console.log('rrrrrrrrr', err);
115+
116+
return null;
117+
}
118+
}
119+
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);
115131
}
116132
}
117133

@@ -123,5 +139,6 @@ export default function* gistsSaga() {
123139
takeLatest(fetchGistComments.actionType, fetchCommentsForGist),
124140
takeLatest(starGist.actionType, starAGist),
125141
takeLatest(fetchInitialFavoriteValue.actionType, getInitialFavoriteValue),
142+
takeLatest(UnstarGist.actionType, unstarAGist),
126143
]);
127144
}

src/gists/screens/gistdetails.screen.js

+23-21
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import MaterialIcon from 'react-native-vector-icons/dist/MaterialCommunityIcons'
1010
// import { processFiles } from '~/src/shared/processFiles';
1111
import GistFileItem from './components/GistFileItem';
1212
import forOwn from 'lodash/forOwn';
13-
import { requestStarGist, checkStarredGist } from '../../api';
14-
import { starGist, fetchInitialFavoriteValue } from '../gists.actiontype';
13+
import { starGist, fetchInitialFavoriteValue, UnstarGist } from '../gists.actiontype';
1514

1615
const HeaderProps = [
1716
'avatal_url',
@@ -29,24 +28,17 @@ const ToolbarContentContainer = styled.View`
2928

3029
class GistDetails extends React.Component {
3130
state = {
32-
iconName: 'star-o',
31+
iconName: this.props.isStarred ? 'star' : 'star-o',
3332
}
3433

3534
componentWillMount() {
36-
const data = this.props.checkIfGistIsStarred(this.props.navigation.getParam('gistData').id);
37-
38-
console.log('data------------------------------', data);
35+
this.props.checkIfGistIsStarred(this.props.navigation.getParam('gistData').id);
3936
}
4037

4138
componentWillReceiveProps(nextProps) {
42-
console.log('(((((((((((((((((((((((((((((((', nextProps);
43-
if (nextProps.isStarred) {
44-
this.setState({
45-
iconName: 'star',
46-
});
47-
} else {
48-
this.setState({ iconName: 'star-o' });
49-
}
39+
this.setState({
40+
iconName: nextProps.isStarred ? 'star' : 'star-o',
41+
});
5042
}
5143

5244
handleFileItemPress = fileData => {
@@ -56,11 +48,22 @@ class GistDetails extends React.Component {
5648
}
5749

5850
handleActionButtonClick = () => {
59-
// requestStarGist(this.props.accessToken, this.props.navigation.getParam('gistData').id)
60-
// .then(() => this.setState({ iconName: 'star' }))
61-
// .catch(error => console.log(error));
62-
this.setState({ iconName: 'star' });
63-
this.props.starThisGist(this.props.navigation.getParam('gistData').id);
51+
const { id } = this.props.navigation.getParam('gistData');
52+
53+
if (this.state.iconName === 'star') {
54+
this.props.UnstarThisGist(id);
55+
this.setState({
56+
iconName: 'star-o',
57+
});
58+
} else {
59+
this.props.starThisGist(id);
60+
this.setState({
61+
iconName: 'star',
62+
});
63+
}
64+
65+
// this.setState({ iconName: 'star' });
66+
// this.props.starThisGist(this.props.navigation.getParam('gistData').id);
6467
}
6568

6669
processFiles = fileData => {
@@ -82,8 +85,6 @@ class GistDetails extends React.Component {
8285
);
8386

8487
renderToobarContent = () => {
85-
console.log('777777777777777777777777777777777', this.state.iconName);
86-
8788
return (
8889
<ToolbarContentContainer>
8990
<TouchableOpacity onPress={this.handleActionButtonClick}>
@@ -134,6 +135,7 @@ class GistDetails extends React.Component {
134135
const mapDispatchToProps = dispatch => ({
135136
starThisGist: id => dispatch(starGist.action(id)),
136137
checkIfGistIsStarred: id => dispatch(fetchInitialFavoriteValue.action(id)),
138+
UnstarThisGist: id => dispatch(UnstarGist.action(id)),
137139
});
138140

139141
const mapStateToProps = ({ initialFavoriteValue }) =>

0 commit comments

Comments
 (0)