|
1 | 1 | import React from 'react';
|
| 2 | +import {AsyncStorage} from 'react-native'; |
| 3 | +import {Toast} from 'native-base'; |
| 4 | + |
| 5 | + |
| 6 | +import config from '../../config'; |
| 7 | +import Page from '../page'; |
2 | 8 | import Swiper from './swiper';
|
3 | 9 |
|
4 |
| -import { |
5 |
| - Container, |
6 |
| - Header, |
7 |
| - Title, |
8 |
| - Button, |
9 |
| - Icon, |
10 |
| - Left, |
11 |
| - Right, |
12 |
| - Body, |
13 |
| - Text |
14 |
| -} from 'native-base'; |
15 |
| - |
16 |
| -class WState extends React.Component { |
17 |
| - constructor(props) { |
18 |
| - super(props); |
19 |
| - |
20 |
| - this.state = { |
21 |
| - posts: [] |
22 |
| - }; |
| 10 | +class PageSwiper extends React.Component { |
| 11 | + state = { |
| 12 | + loading: true, |
| 13 | + posts: [] |
| 14 | + }; |
| 15 | + session = null; |
| 16 | + |
| 17 | + saveSession = () => { |
| 18 | + AsyncStorage.setItem('session', JSON.stringify(this.session)); |
| 19 | + } |
| 20 | + |
| 21 | + resetSession = (day) => { |
| 22 | + this.session = { |
| 23 | + day: day, |
| 24 | + swiped: [] // not implemented with shift because the order & the number of posts could change |
| 25 | + } |
| 26 | + |
| 27 | + this.saveSession() |
| 28 | + } |
| 29 | + |
| 30 | + async componentDidMount() { |
| 31 | + try { |
| 32 | + const fetched = await fetch(config.LATEST_ENDPOINT); |
| 33 | + const json = await fetched.json(); |
| 34 | + const session = await AsyncStorage.getItem('session'); |
| 35 | + |
| 36 | + if (!json || !json.posts || json.posts.length === 0) { |
| 37 | + throw new Error('No posts'); |
| 38 | + } |
| 39 | + |
| 40 | + if (session === null) { |
| 41 | + this.resetSession(json.posts[0].day); |
| 42 | + } else { |
| 43 | + this.session = JSON.parse(session); |
| 44 | + |
| 45 | + if (this.session.day !== json.posts[0].day) { // new day => new session |
| 46 | + this.resetSession(json.posts[0].day); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + this.setState({ |
| 51 | + posts: json.posts.filter(p => this.session.swiped.indexOf(p.id) === -1), |
| 52 | + loading: false |
| 53 | + }); |
| 54 | + } catch(error) { |
| 55 | + Toast.show({ |
| 56 | + text: 'Verify your internet connection and retry', |
| 57 | + position: 'bottom', |
| 58 | + buttonText: 'OKAY' |
| 59 | + }) |
| 60 | + } |
23 | 61 | }
|
24 | 62 |
|
25 |
| - onSwipe = () => { |
26 |
| - console.log('swipe'); |
| 63 | + onSwipe = (o) => { |
| 64 | + this.session.swiped.push(o.id); |
| 65 | + this.saveSession(); |
| 66 | + |
| 67 | + if (this.state.posts.indexOf(o) === this.state.posts.length - 1) { |
| 68 | + this.setState({posts: []}); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + onSwipeLeft = (o) => { |
| 73 | + this.onSwipe(o); |
| 74 | + } |
| 75 | + |
| 76 | + onSwipeRight = (o) => { |
| 77 | + this.onSwipe(o); |
27 | 78 | }
|
28 | 79 |
|
29 | 80 | render() {
|
30 | 81 | return (
|
31 |
| - <Container style={{backgroundColor: '#fbfafa'}}> |
32 |
| - <Header> |
33 |
| - <Left> |
34 |
| - <Button |
35 |
| - transparent |
36 |
| - onPress={() => this.props.navigation.navigate('DrawerOpen')} |
37 |
| - > |
38 |
| - <Icon name="menu" /> |
39 |
| - </Button> |
40 |
| - </Left> |
41 |
| - <Body> |
42 |
| - <Title>Deck Swiper</Title> |
43 |
| - </Body> |
44 |
| - <Right /> |
45 |
| - </Header> |
46 |
| - |
| 82 | + <Page title="Hunty - Swiper" navigation={this.props.navigation} loading={this.state.loading}> |
47 | 83 | <Swiper
|
48 | 84 | items={this.state.posts}
|
49 |
| - onSwipeLeft={this.onSwipe} |
50 |
| - onSwipeRight={this.onSwipe} |
| 85 | + onSwipeLeft={this.onSwipeLeft} |
| 86 | + onSwipeRight={this.onSwipeRight} |
51 | 87 | />
|
52 |
| - </Container> |
| 88 | + </Page> |
53 | 89 | );
|
54 | 90 |
|
55 | 91 | }
|
56 | 92 | }
|
57 | 93 |
|
58 |
| -export default WState; |
| 94 | +export default PageSwiper; |
0 commit comments