Skip to content

Commit 525f98e

Browse files
committed
modified add and delete methods
1 parent 5de0809 commit 525f98e

File tree

6 files changed

+105
-32
lines changed

6 files changed

+105
-32
lines changed

src/api/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ export const v3 = {
8383
performDeleteRequest: async(url, accessToken) => {
8484
const response = await v3.call(url, v3.parameters(accessToken, METHOD.DELETE));
8585

86-
console.log('from api', response);
87-
8886
return response.status;
8987
},
9088
};
@@ -109,7 +107,7 @@ export const fetchAccessToken = async({ code, state }) => {
109107
return response.data;
110108
};
111109

112-
export const addComments = async(payload, id, accessToken) =>
110+
export const requestAddComment = async(accessToken, id, payload) =>
113111
v3.getDataFromPostRequest(`/gists/${id}/comments`, accessToken, { body: payload });
114112

115113
export const getAuthUser = async accessToken => await v3.getJson('/user', accessToken);
@@ -129,3 +127,5 @@ export const requestStarGist = async(accessToken, gistId) => v3.getDataFromPutRe
129127
export const checkStarredGistFavoriteValue = async(accessToken, gistId) => v3.getJson(`/gists/${gistId}/star`, accessToken);
130128

131129
export const requestUnstarGist = async(accessToken, gistId) => v3.performDeleteRequest(`/gists/${gistId}/star`, accessToken);
130+
131+
export const requestDeleteComment = async(accessToken, gistId, commentId) => v3.performDeleteRequest(`/gists/${gistId}/comments/${commentId}`, accessToken);

src/config/colors.js

+1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ export const colors = {
3232
githubDark: '#1f2327',
3333
alabaster: '#f7f7f7',
3434
topicLightBlue: '#f1f8ff',
35+
pictonBlue: '#33B5E5',
3536
};

src/gists/gistoptions.screen.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@ import React from 'react';
22
import { View, Text, TouchableOpacity } from 'react-native';
33
import { colors } from './../config';
44

5-
const GistOptions = () => (
5+
const GistOptions = ({ onDelete, onCancel }) => (
66
<View style={{
7-
flex: 1, backgroundColor: colors.white, borderWidth: 5, borderColor: 'red',
7+
flex: 1,
8+
backgroundColor: 'rgba(0, 0, 0, 0.4)',
9+
justifyContent: 'center',
10+
alignItems: 'center',
811
}}>
9-
<TouchableOpacity style={{ padding: '2%' }}>
10-
<Text>Delete</Text>
11-
</TouchableOpacity>
12-
<TouchableOpacity style={{ padding: '2%' }}>
13-
<Text>Cancel</Text>
14-
</TouchableOpacity>
12+
<View style={{ backgroundColor: colors.white, width: '85%' }}>
13+
<TouchableOpacity
14+
style={{ padding: '4%', borderBottomWidth: 1, borderColor: colors.grey }}
15+
onPress={onDelete}>
16+
<Text style={{ color: colors.red, fontSize: 18, fontStyle: '600' }}>Delete</Text>
17+
</TouchableOpacity>
18+
<TouchableOpacity
19+
style={{ padding: '4%', fontStyle: '600' }}
20+
onPress={onCancel}>
21+
<Text style={{ color: colors.pictonBlue, fontSize: 18 }}>Cancel</Text>
22+
</TouchableOpacity>
23+
</View>
1524
</View>
16-
1725
);
1826

1927
export default GistOptions;

src/gists/gists.actiontype.js

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ export const publicGistsFetch = createSagaActionSet('FETCH_PUBLIC_GISTS');
77
export const starGist = createSagaActionSet('STAR_GIST');
88
export const fetchInitialFavoriteValue = createSagaActionSet('INITIAL_FAVORITE_VALUE');
99
export const UnstarGist = createSagaActionSet('UNSTAR_GIST');
10+
export const deleteComment = createSagaActionSet('DELETE_COMMENT');
11+
export const addComment = createSagaActionSet('ADD_COMMENT');

src/gists/gists.saga.js

+43-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
starGist,
99
fetchInitialFavoriteValue,
1010
UnstarGist,
11+
deleteComment,
12+
addComment,
1113
} from './gists.actiontype';
1214
import {
1315
requestUserGists,
@@ -17,10 +19,13 @@ import {
1719
requestStarGist,
1820
checkStarredGistFavoriteValue,
1921
requestUnstarGist,
22+
requestDeleteComment,
23+
requestAddComment,
2024
} from '../api';
2125

2226
const tokenSelector = state => state.auth.access_token;
2327
const userNameSelector = state => state.loggedInUser.userName;
28+
const commentsSelector = state => state.gistComments.comments;
2429

2530
function* fetchUserGists() {
2631
try {
@@ -75,16 +80,13 @@ function* fetchPublicGists() {
7580

7681
function* fetchCommentsForGist(action) {
7782
try {
78-
// const hasMoreComments = yield select(state => state.gistComments.hasMoreComments);
79-
80-
// if (hasMoreComments) {
8183
yield put(fetchGistComments.progress());
8284
const token = yield select(tokenSelector);
8385
const { data } = yield call(requestGistComments, token, action.payload);
8486

8587
yield put(fetchGistComments.success({ data }));
86-
// }
8788
} catch (err) {
89+
console.log(err);
8890
yield put(publicGistsFetch.error(err));
8991
}
9092
}
@@ -96,8 +98,6 @@ function* starAGist(action) {
9698

9799
if (status === 204) {
98100
yield call(fetchStarredGists, { shouldRefresh: true });
99-
100-
return;
101101
}
102102
} catch (err) {
103103
console.log(err);
@@ -112,8 +112,6 @@ function* getInitialFavoriteValue(action) {
112112
yield put(fetchInitialFavoriteValue.success({ value: true }));
113113
} catch (err) {
114114
yield put(fetchInitialFavoriteValue.success({ value: false }));
115-
116-
return null;
117115
}
118116
}
119117

@@ -131,6 +129,41 @@ function* unstarAGist(action) {
131129
}
132130
}
133131

132+
function* deleteAComment(action) {
133+
try {
134+
const token = yield select(tokenSelector);
135+
const { gistId, commentId } = action.payload;
136+
const status = yield call(requestDeleteComment, token, gistId, commentId);
137+
138+
if (status === 204) {
139+
const comments = yield select(commentsSelector);
140+
const data = comments.filter(comment => comment.id !== commentId);
141+
142+
yield put(fetchGistComments.success({ data }));
143+
}
144+
} catch (err) {
145+
console.log(err);
146+
}
147+
}
148+
149+
function* addAComment(action) {
150+
try {
151+
const token = yield select(tokenSelector);
152+
const { gistId, comment } = action.payload;
153+
154+
const data = yield call(requestAddComment, token, gistId, comment);
155+
156+
if (data) {
157+
const comments = yield select(commentsSelector);
158+
const newData = [...comments, data];
159+
160+
yield put(fetchGistComments.success({ data: newData }));
161+
}
162+
} catch (err) {
163+
console.log(err);
164+
}
165+
}
166+
134167
export default function* gistsSaga() {
135168
yield all([
136169
takeLatest(userGistsFetch.actionType, fetchUserGists),
@@ -140,5 +173,7 @@ export default function* gistsSaga() {
140173
takeLatest(starGist.actionType, starAGist),
141174
takeLatest(fetchInitialFavoriteValue.actionType, getInitialFavoriteValue),
142175
takeLatest(UnstarGist.actionType, unstarAGist),
176+
takeLatest(deleteComment.actionType, deleteAComment),
177+
takeLatest(addComment.actionType, addAComment),
143178
]);
144179
}

src/gists/screens/gistComments.screen.js

+39-12
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ import {
77
Keyboard,
88
Text,
99
ActivityIndicator,
10+
TouchableOpacity,
11+
Modal,
1012
} from 'react-native';
1113
import styled from 'styled-components';
1214
import CardView from 'react-native-cardview';
1315
import TimeAgo from 'time-ago';
14-
import { fetchGistComments } from '../gists.actiontype';
16+
import { fetchGistComments, deleteComment, addComment } from '../gists.actiontype';
1517
import ListEmptyComponent from './components/EmptyListComponent';
16-
import { addComments } from '../../api';
18+
// import { addComments } from '../../api';
1719
import { colors } from '../../config';
20+
import GistOptions from '../gistoptions.screen';
1821

1922
const CardContainer = styled(CardView)`
2023
padding: 3%;
@@ -66,7 +69,7 @@ const InputContainer = styled.View`
6669
const Button = styled.TouchableOpacity`
6770
padding: 3%;
6871
align-self: center;
69-
background-color: #33B5E5;
72+
background-color: ${colors.pictonBlue};
7073
`;
7174

7275
const ActivityIndicatorContainer = styled.View`
@@ -80,32 +83,48 @@ class GistCommentsScreen extends React.Component {
8083
super();
8184
this.state = {
8285
comment: '',
86+
isVisible: false,
87+
id: null,
8388
};
84-
this.timeout = null;
8589
}
8690
componentDidMount() {
8791
this.fetchComments();
8892
}
8993

9094
onPressItem = () => {
9195
Keyboard.dismiss();
96+
this.setState({ comment: '' });
9297
const { id } = this.props.navigation.getParam('gistData');
9398

94-
addComments(this.state.comment, id, this.props.accessToken)
95-
.then(() => {
96-
this.setState({ comment: '' });
97-
this.fetchComments();
98-
})
99-
.catch(console.log);
99+
this.props.addThisComment({ gistId: id, comment: this.state.comment });
100+
}
101+
102+
onCancel = () => {
103+
this.setState({ isVisible: false });
104+
}
105+
106+
openGistOptions = id => {
107+
this.setState({
108+
isVisible: true,
109+
id,
110+
});
100111
}
101112

102113
fetchComments = () => {
103114
this.props.fetchComments(this.props.navigation.getParam('gistData').id);
104115
}
105116

117+
deleteComment = () => {
118+
this.onCancel();
119+
this.props.deleteThisComment({
120+
gistId: this.props.navigation.getParam('gistData').id,
121+
commentId: this.state.id,
122+
});
123+
}
124+
106125
renderItem = ({ item }) => {
107126
return (
108-
<View style={{ flex: 1 }}>
127+
<TouchableOpacity style={{ flex: 1 }} onLongPress={() => this.openGistOptions(item.id)}>
109128
<CardContainer
110129
cardElevation={2}
111130
cardMaxElevation={2}
@@ -120,7 +139,7 @@ class GistCommentsScreen extends React.Component {
120139
</UserProfile>
121140
<Comment>{item.body}</Comment>
122141
</CardContainer>
123-
</View>
142+
</TouchableOpacity>
124143
);
125144
}
126145

@@ -160,12 +179,20 @@ class GistCommentsScreen extends React.Component {
160179
<Text style={{ color: colors.white, fontWeight: '600' }}>Submit</Text>
161180
</Button>
162181
</InputContainer>
182+
<Modal
183+
onRequestClose={() => {}}
184+
visible={this.state.isVisible}
185+
transparent>
186+
<GistOptions onDelete={this.deleteComment} onCancel={this.onCancel} />
187+
</Modal>
163188
</React.Fragment>
164189
);
165190
}
166191
}
167192
const mapDispatchToProps = dispatch => ({
168193
fetchComments: id => dispatch(fetchGistComments.action(id)),
194+
deleteThisComment: data => dispatch(deleteComment.action(data)),
195+
addThisComment: data => dispatch(addComment.action(data)),
169196
});
170197
const mapStateToProps = ({ gistComments, auth }) => ({
171198
comments: gistComments.comments,

0 commit comments

Comments
 (0)