-
Notifications
You must be signed in to change notification settings - Fork 0
Refetch container working but replacing all items instead of appending. #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: eslint
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| import React, { Component } from 'react'; | ||
| import { View, Text, FlatList, ActivityIndicator } from 'react-native'; | ||
|
|
||
| import Relay, { | ||
| graphql, | ||
| QueryRenderer, | ||
| createRefetchContainer | ||
| } from 'react-relay'; | ||
|
|
||
| import environment from '../environment'; | ||
|
|
||
|
|
||
| class StarWars extends Component { | ||
| state = { | ||
| isFetching: false, | ||
| isFetchingEnd: false, | ||
| page: 1, | ||
| dataSource: [] | ||
| }; | ||
| renderItem = ({ item }) => { | ||
| const { name, gender } = item; | ||
| return ( | ||
| <View> | ||
| <Text>{name}</Text> | ||
| <Text>{gender}</Text> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| onRefresh = () => { | ||
| const { isFetching } = this.state; | ||
|
|
||
| if (isFetching) return; | ||
|
|
||
| this.setState({ isFetching: true }); | ||
|
|
||
| const refetchVariables = fragmentVariables => ({ | ||
| ...fragmentVariables, | ||
| }); | ||
|
|
||
| this.props.relay.refetch( | ||
| refetchVariables, | ||
| null, | ||
| () => { | ||
| this.setState({ | ||
| isFetching: false, | ||
| isFetchingEnd: false, | ||
| }); | ||
| }, | ||
| { | ||
| force: true, | ||
| }, | ||
| ); | ||
| }; | ||
|
|
||
| onEndReached = () => { | ||
| const { isFetchingEnd, page } = this.state; | ||
|
|
||
| if (isFetchingEnd === true) return; | ||
|
|
||
| if (page > 9) return; | ||
|
|
||
| this.setState({ | ||
| isFetchingEnd: true, | ||
| page: page + 1, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. page: total, |
||
| }); | ||
|
|
||
|
|
||
| const refetchVariables = fragmentVariables => ({ | ||
| ...fragmentVariables, | ||
| page: page + 1 | ||
| }); | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const renderVariables = {
page: total,
}; |
||
| this.props.relay.refetch( | ||
| refetchVariables, | ||
| null, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. then add here: renderVariables, |
||
| () => { | ||
| this.setState({ | ||
| isFetchingEnd: false, | ||
| isFetching: false, | ||
| }); | ||
| }, | ||
| { | ||
| force: false, | ||
| }, | ||
| ); | ||
| }; | ||
|
|
||
| renderFooter = () => { | ||
| if (this.state.isFetchingEnd) { | ||
| return null; | ||
| } | ||
| return ( | ||
| <View style={{ flex: 1 }}> | ||
| <ActivityIndicator /> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| render() { | ||
| const { peoplePage } = this.props.query; | ||
| console.log(peoplePage); | ||
| return ( | ||
| <FlatList | ||
| data={peoplePage} | ||
| renderItem={this.renderItem} | ||
| keyExtractor={item => item.name} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe it's better to use an |
||
| onRefresh={this.onRefresh} | ||
| refreshing={this.state.isFetching} | ||
| ListFooterComponent={this.renderFooter} | ||
| onEndReached={this.onEndReached} | ||
| onEndReachedThreshold={1} //on list | ||
| /> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const StarWarsRefetchContainer = createRefetchContainer( | ||
| StarWars, | ||
| { | ||
| query: graphql` | ||
| fragment StarWars_query on Query | ||
| @argumentDefinitions( | ||
| page: { type: "Int", defaultValue: 1 } | ||
| ) { | ||
| peoplePage(page: $page) { | ||
| name | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can ask for id here |
||
| height | ||
| mass | ||
| hairColor | ||
| birthYear | ||
| species | ||
| homeworld | ||
| films | ||
| gender | ||
| } | ||
| } | ||
| `, | ||
| }, | ||
| graphql` | ||
| query StarWarsRefetchQuery( $page: Int ) { | ||
| ...StarWars_query @arguments(page: $page) | ||
| } | ||
| `, | ||
| ); | ||
|
|
||
| const StarWarsQueryRenderer = () => ( | ||
| <QueryRenderer | ||
| environment={environment} | ||
| query={graphql` | ||
| query StarWarsQuery { | ||
| ...StarWars_query | ||
| } | ||
| ` | ||
| } | ||
| render={({ error, props }) => { | ||
| if (error) { | ||
| return ( | ||
| <View> | ||
| <Text>An unexpected error occurred</Text> | ||
| </View> | ||
| ); | ||
| } else if (props) { | ||
| return <StarWarsRefetchContainer query={props} />; | ||
| } | ||
| return ( | ||
| <View | ||
| style={{ | ||
| flex: 1, | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| }} | ||
| > | ||
| <ActivityIndicator animating={true} size="large" color="rgb(0, 148, 255)" /> | ||
| </View> | ||
| ); | ||
| }} | ||
| /> | ||
| ); | ||
|
|
||
| export default StarWarsQueryRenderer; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import { Environment, Network, RecordSource, Store } from 'relay-runtime'; | ||
|
|
||
| const fetchQuery = async (operation, variables, cacheConfig, uploadables) => { | ||
| const response = await fetch('https://graphql-sw-api-klmkpknwdl.now.sh/', { | ||
| const response = await fetch('https://graphql-sw-api-gxmdjgcfhi.now.sh/', { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can create an alias, so you don't need to update this every time you deploy to server \o/ |
||
| method: 'POST', | ||
| headers: { | ||
| Accept: 'application/json', | ||
|
|
@@ -13,7 +13,7 @@ const fetchQuery = async (operation, variables, cacheConfig, uploadables) => { | |
| }), | ||
| }); | ||
| return await response.json(); | ||
| } | ||
| }; | ||
|
|
||
| const network = Network.create(fetchQuery); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add here: