From b2998a1f7b64f93ffd9ae90f85ca0581015d589a Mon Sep 17 00:00:00 2001 From: bezvershenko Date: Tue, 12 Mar 2019 21:54:03 +0400 Subject: Add 'pull-to-refresh' in ListView and ArticleView Add host's constant for backend ip-address --- frontend/app/screens/articles/article.js | 167 ++++++++++++++++------------- frontend/app/screens/articles/articles2.js | 143 +++++++++++++----------- 2 files changed, 176 insertions(+), 134 deletions(-) (limited to 'frontend/app/screens/articles') diff --git a/frontend/app/screens/articles/article.js b/frontend/app/screens/articles/article.js index 059ab44..aedeb1d 100644 --- a/frontend/app/screens/articles/article.js +++ b/frontend/app/screens/articles/article.js @@ -1,94 +1,117 @@ import React from 'react'; import axios from 'axios'; - - import { - ScrollView, - Image, - View, - TouchableOpacity, + ScrollView, + Image, + View, + RefreshControl, } from 'react-native'; + import { - RkCard, - RkText, - RkStyleSheet, + RkCard, + RkText, + RkStyleSheet, } from 'react-native-ui-kitten'; import { - Avatar, - SocialBar, + Avatar, + SocialBar, } from '../../components'; import NavigationType from '../../config/navigation/propTypes'; +import {HOST} from '../../constants' export class Article extends React.Component { - state = { - article: {}, - mounted: false, - }; + state = { + article: {}, + mounted: false, + refreshing: false, + }; - static propTypes = { - navigation: NavigationType.isRequired, - }; - static navigationOptions = { - title: 'Current problem'.toUpperCase(), - }; + static propTypes = { + navigation: NavigationType.isRequired, + }; + static navigationOptions = { + title: 'Current problem'.toUpperCase(), + }; + + + componentWillMount() { + this.makeRemoteRequest() + } + makeRemoteRequest = () => { + const articleId = this.props.navigation.getParam('id', 1); + console.log(articleId); - componentWillMount() { - const articleId = this.props.navigation.getParam('id', 1); - console.log(articleId); + axios.get(`${HOST}/api/articles/${articleId}`) + .then(res => { + this.setState({ + article: res.data, + mounted: true, + refreshing: false, + }); + console.log(this.state.article); + }); + }; - axios.get(`http://192.168.1.43:8000/api/articles/${articleId}`) - .then(res => { + handleRefresh = () => { this.setState({ - article: res.data, - mounted: true, - }); - console.log(this.state.article); - }); - } - - render() { - if (this.state.mounted) { - return ( - - - - - - {this.state.article.title} - - - - - - - {this.state.article.text} - - - - - - - - - ); + refreshing: true, + }, () => { + setTimeout(() => { + this.makeRemoteRequest() + }, 500) + + }) + }; + + + render() { + if (this.state.mounted) { + return ( + }> + + + + + {this.state.article.title} + + + + + + + {this.state.article.text} + + + + + + + + + ); + } + return null; } - return null; - } } const styles = RkStyleSheet.create(theme => ({ - root: { - backgroundColor: theme.colors.screen.base, - }, - title: { - marginBottom: 5, - }, + root: { + backgroundColor: theme.colors.screen.base, + }, + title: { + marginBottom: 5, + }, })); diff --git a/frontend/app/screens/articles/articles2.js b/frontend/app/screens/articles/articles2.js index 0d48812..03a78da 100644 --- a/frontend/app/screens/articles/articles2.js +++ b/frontend/app/screens/articles/articles2.js @@ -1,84 +1,103 @@ import React from 'react'; import { - FlatList, - Image, - View, - TouchableOpacity, + FlatList, + Image, + View, + TouchableOpacity, } from 'react-native'; import { - RkText, - RkCard, RkStyleSheet, + RkText, + RkCard, RkStyleSheet, } from 'react-native-ui-kitten'; import axios from 'axios'; -import { SocialBar } from '../../components'; +import {SocialBar} from '../../components'; import NavigationType from '../../config/navigation/propTypes'; +import {HOST} from '../../constants' export class Articles2 extends React.Component { - static propTypes = { - navigation: NavigationType.isRequired, - }; - static navigationOptions = { - title: 'Problems List'.toUpperCase(), - }; + static propTypes = { + navigation: NavigationType.isRequired, + }; + static navigationOptions = { + title: 'Problems List'.toUpperCase(), + }; - state = { - articles: [], - }; + state = { + articles: [], + refreshing: false, + }; - componentDidMount() { - axios.get('http://192.168.1.43:8000/api/articles') - .then(res => { + componentDidMount() { + this.makeRemoteRequest() + }; + + makeRemoteRequest = () => { + axios.get(`${HOST}/api/articles`) + .then(res => { + this.setState({ + articles: res.data, + refreshing: false, + }); + + }); + }; + + handleRefresh = () =>{ this.setState({ - articles: res.data, - }); + refreshing: true, + }, () => { + setTimeout(()=>{this.makeRemoteRequest()}, 500) - }); - } + }) + }; - extractItemKey = (item) => `${item.id}`; + extractItemKey = (item) => `${item.id}`; - onItemPressed = (item) => { - this.props.navigation.navigate('Article', { id: item.id }); - }; + onItemPressed = (item) => { + this.props.navigation.navigate('Article', {id: item.id}); + }; - renderItem = ({ item }) => ( - this.onItemPressed(item)}> - - - - {item.title} - - - - - - - ); + renderItem = ({item}) => ( + this.onItemPressed(item)}> + + + + {item.title} + + + + + + + ); - render = () => ( - - ); + render = () => ( + + ); } const styles = RkStyleSheet.create(theme => ({ - container: { - backgroundColor: theme.colors.screen.scroll, - paddingVertical: 8, - paddingHorizontal: 14, - }, - card: { - marginVertical: 8, - }, - time: { - marginTop: 5, - }, + container: { + backgroundColor: theme.colors.screen.scroll, + paddingVertical: 8, + paddingHorizontal: 14, + }, + card: { + marginVertical: 8, + }, + time: { + marginTop: 5, + }, })); -- cgit v1.2.3