Skip to content

Commit 1dd2c09

Browse files
committed
till adding comments
1 parent 2f3df64 commit 1dd2c09

10 files changed

+95
-24
lines changed

src/api/index.js

+18
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ export const fetchAccessToken = async({ code, state }) => {
9393
return response.data;
9494
};
9595

96+
export const addComments = async(payload, id, accessToken) => {
97+
console.log('here------------------------', payload, id, accessToken);
98+
const ADD_COMMENTS_URL = `https://api.github.com/gists/${id}/comments`;
99+
100+
const response = await axios.post(ADD_COMMENTS_URL, { body: payload }, {
101+
headers: {
102+
Accept: 'application/json',
103+
'Content-Type': 'application/json',
104+
Authorization: `token ${accessToken}`,
105+
},
106+
})
107+
.then(data => data);
108+
109+
console.log('oooooooooooooooooooo', response);
110+
111+
return response;
112+
};
113+
96114
export const getAuthUser = async accessToken => await v3.getJson('/user', accessToken);
97115

98116
export const requestUserGists = async(accessToken, userName, pageNo) => await v3.getJsonWithHeader(`/users/${userName}/gists?page=${pageNo}`, accessToken);

src/assets/default_user_image.png

667 Bytes
Loading

src/auth/auth.saga.js

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ function* loginAndFetchUser() {
1313
yield put(logIn.progress());
1414
const loginResponse = yield call(fetchAccessToken, payload);
1515

16-
console.log('pppppppppppp', loginResponse);
1716
yield put(logIn.success(loginResponse));
1817
yield put(fetchAuthUser.action());
1918
} catch (error) {

src/gists/gists.reducer.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ const clearCache = () => ({
4040

4141
const setGistComments = (state, { payload }) => {
4242
console.log('---------------------', payload.data);
43+
const { data, error } = payload;
44+
// const initialComments = payload.data.slice(key - 6, key);
4345

4446
return {
4547
...state,
4648
inProgress: false,
47-
error: payload.error,
48-
comments: payload.data,
49+
error,
50+
comments: data, // initialComments,
51+
// hasMoreComments: key !== data.length,
4952
};
5053
};
5154

@@ -81,7 +84,7 @@ export default {
8184
[fetchGistComments.errorType]: setError,
8285
},
8386
{
84-
comments: [], inProgress: false,
87+
comments: [], inProgress: false, // hasMoreComments: true,
8588
}
8689
),
8790
};

src/gists/gists.saga.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,16 @@ function* fetchPublicGists() {
5858
}
5959

6060
function* fetchCommentsForGist(action) {
61-
console.log('here');
6261
try {
62+
// const hasMoreComments = yield select(state => state.gistComments.hasMoreComments);
63+
64+
// if (hasMoreComments) {
6365
yield put(fetchGistComments.progress());
6466
const token = yield select(tokenSelector);
6567
const { data } = yield call(requestGistComments, token, action.payload);
6668

6769
yield put(fetchGistComments.success({ data }));
70+
// }
6871
} catch (err) {
6972
yield put(publicGistsFetch.error(err));
7073
}

src/gists/screens/components/GistDetailHeader.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Text, Image } from 'react-native';
55
import moment from 'moment';
66
import fileSize from 'filesize';
77
import { colors, normalizeFont } from '../../../config';
8+
import defaultUserImage from '~/src/assets/default_user_image.png';
89

910
const Container = styled.View`
1011
display: flex;
@@ -46,7 +47,7 @@ export default GistDetailsHeader = ({
4647
}) => {
4748
return (
4849
<Container>
49-
<Avatar source={{ uri: userImage }} />
50+
<Avatar source={userImage ? { uri: userImage } : defaultUserImage} />
5051
<DetailsContainer>
5152
<Title numberOfLines={2}>
5253
{userName}

src/gists/screens/gistComments.screen.js

+59-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React from 'react';
22
import { connect } from 'react-redux';
3-
import { FlatList } from 'react-native';
3+
import { FlatList, TextInput, Button, View, KeyboardAvoidingView } from 'react-native';
44
import styled from 'styled-components';
55
import CardView from 'react-native-cardview';
66
import TimeAgo from 'time-ago';
77
import { fetchGistComments } from '../gists.actiontype';
88
import ListEmptyComponent from './components/EmptyListComponent';
9+
import { addComments } from '~/src/api';
910

1011
const CardContainer = styled(CardView)`
1112
padding: 3%;
@@ -44,12 +45,33 @@ const CommentDate = styled.Text`
4445
font-size: 14;
4546
`;
4647

48+
// const CommentBox = styled.TextInput`
49+
// `;
50+
4751
class GistCommentsScreen extends React.Component {
52+
state = {
53+
comment: '',
54+
}
4855
componentDidMount() {
56+
console.log('id---------------------------', this.props.navigation.getParam('gistData').id);
4957
this.props.fetchComments(this.props.navigation.getParam('gistData').id);
5058
}
5159

52-
renderItem = ({ item }) => (
60+
// onEndReachedThreshold = () => {
61+
// this.setState({
62+
// i: this.state.i + 6,
63+
// }, () => {
64+
// this.props.fetchComments(this.props.navigation.getParam('gistData').id, this.state.i);
65+
// });
66+
// }
67+
68+
69+
onPressItem = () => {
70+
addComments(this.state.comment, this.props.navigation.getParam('gistData').id, this.props.accessToken);
71+
}
72+
73+
renderItem = ({ item }) => {
74+
return (
5375
<CardContainer
5476
cardElevation={2}
5577
cardMaxElevation={2}
@@ -64,25 +86,51 @@ class GistCommentsScreen extends React.Component {
6486
</UserProfile>
6587
<Comment>{item.body}</Comment>
6688
</CardContainer>
67-
)
89+
);
90+
}
6891

6992
render() {
93+
const { comments } = this.props;
94+
7095
return (
71-
<FlatList
72-
keyExtractor={item => item.id}
73-
data={this.props.comments}
74-
renderItem={this.renderItem}
75-
ListEmptyComponent={() => <ListEmptyComponent message="No comments found" />}
76-
/>
96+
<React.Fragment>
97+
<FlatList
98+
keyExtractor={item => item.id}
99+
data={this.props.comments}
100+
renderItem={this.renderItem}
101+
ListEmptyComponent={() => <ListEmptyComponent message="No comments found" />}
102+
// onEndReachedThreshold={0.2}
103+
// onEndReachedThreshold={this.onEndReachedThreshold}
104+
/>
105+
<View style={{
106+
flex: 1,
107+
flexDirection: 'row',
108+
alignItems: 'flex-end',
109+
marginHorizontal: 2,
110+
borderColor: 'red',
111+
borderRadius: 5,
112+
}}>
113+
<TextInput
114+
style={{
115+
width: '88%',
116+
}}
117+
placeholder="Add comment here"
118+
value={this.state.comment}
119+
onChangeText={comment => this.setState({ comment })}
120+
/>
121+
<Button title="Add" onPress={this.onPressItem} />
122+
</View>
123+
</React.Fragment>
77124
);
78125
}
79126
}
80-
127+
// style={{ position: 'absolute', bottom: 0, right: 0 }} />
81128
const mapDispatchToProps = dispatch => ({
82129
fetchComments: id => dispatch(fetchGistComments.action(id)),
83130
});
84-
const mapStateToProps = ({ gistComments }) => ({
131+
const mapStateToProps = ({ gistComments, auth }) => ({
85132
comments: gistComments.comments,
133+
accessToken: auth.access_token,
86134
});
87135

88136
export default connect(mapStateToProps, mapDispatchToProps)(GistCommentsScreen);

src/gists/screens/gistdetails.screen.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react';
22
import { View, FlatList, Text } from 'react-native';
3-
import pick from 'lodash/pick';
4-
import forOwn from 'lodash/forOwn';
3+
import isEmpty from 'lodash/isEmpty';
54
import styled from 'styled-components';
65
import Header from './components/GistDetailHeader';
76
import Toolbar from './components/Toolbar';
@@ -64,14 +63,14 @@ export default class GistDetails extends React.Component {
6463
render() {
6564
const { navigation } = this.props;
6665
const gistData = navigation.getParam('gistData', {});
67-
const { owner } = gistData;
66+
const { owner = {} } = gistData;
6867
const { totalFileSize } = processFiles(gistData.files);
6968

7069
return (
7170
<View>
7271
<Header
73-
userImage={owner.avatar_url}
74-
userName={owner.login}
72+
userImage={!isEmpty(owner) && owner.avatar_url}
73+
userName={isEmpty(owner) ? 'Anonymous' : owner.login}
7574
description={gistData.description}
7675
createdAt={gistData.created_At}
7776
gistSize={totalFileSize} />

src/routes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import StarredGistsScreen from './gists/screens/starredgists.screen';
99
import ClearCacheScreen from './cache/screens/cache.screen.js';
1010
import GistDetailsScreen from './gists/screens/gistdetails.screen';
1111
import GistFileContentScreen from './gists/screens/gistfilecontent.screen';
12-
import GistContentsScreen from './gists/screens/gistContents.screen';
12+
import GistContentListScreen from './gists/screens/gistContentList.screen';
1313
import GistCommentsScreen from './gists/screens/gistComments.screen';
1414

1515
const styles = StyleSheet.create({
@@ -55,7 +55,7 @@ const MainTabsScreen = TabNavigator({
5555

5656
const GistFileContentAndCommentsScreen = TabNavigator({
5757
GistContent: {
58-
screen: GistContentsScreen,
58+
screen: GistContentListScreen,
5959
navigationOptions: {
6060
tabBarLabel: 'Content',
6161
},

0 commit comments

Comments
 (0)