|
| 1 | +import Immutable from 'immutable'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import React, { Component } from 'react'; |
| 4 | +import { Text, View, Button, RefreshControl } from 'react-native'; |
| 5 | + |
| 6 | +import style from './styles'; |
| 7 | + |
| 8 | +const EMPTY_LIST = Immutable.List(); |
| 9 | +const MOCK_DELAY = 800; |
| 10 | + |
| 11 | +class GenericListExample extends Component { |
| 12 | + |
| 13 | + static propTypes = { |
| 14 | + ListComponent: PropTypes.oneOfType([PropTypes.func, PropTypes.element]).isRequired, |
| 15 | + listComponentProps: PropTypes.object.isRequired, |
| 16 | + |
| 17 | + initialDataA: PropTypes.object.isRequired, |
| 18 | + dataMutatorA: PropTypes.func.isRequired, |
| 19 | + extraPropsA: PropTypes.object, |
| 20 | + |
| 21 | + initialDataB: PropTypes.object.isRequired, |
| 22 | + dataMutatorB: PropTypes.func.isRequired, |
| 23 | + extraPropsB: PropTypes.object, |
| 24 | + }; |
| 25 | + |
| 26 | + componentWillMount() { |
| 27 | + const { initialDataA, initialDataB } = this.props; |
| 28 | + |
| 29 | + this.defaultStateA.data = initialDataA; |
| 30 | + this.defaultStateB.data = initialDataB; |
| 31 | + |
| 32 | + this.setState({ |
| 33 | + listA: { |
| 34 | + ...this.defaultStateA, |
| 35 | + }, |
| 36 | + listB: { |
| 37 | + ...this.defaultStateB, |
| 38 | + }, |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + defaultStateA = { |
| 43 | + data: undefined, // Will be manually set on mount. |
| 44 | + isLoading: false, |
| 45 | + errorMsg: undefined, |
| 46 | + }; |
| 47 | + |
| 48 | + defaultStateB = { |
| 49 | + data: undefined, // Will be manually set on mount. |
| 50 | + isLoading: false, |
| 51 | + errorMsg: undefined, |
| 52 | + }; |
| 53 | + |
| 54 | + changeDataA(delay = 0) { |
| 55 | + const { listA } = this.state; |
| 56 | + const { dataMutatorA } = this.props; |
| 57 | + |
| 58 | + if (delay) { |
| 59 | + this.setState({ |
| 60 | + listA: { |
| 61 | + ...listA, |
| 62 | + isLoading: true, |
| 63 | + }, |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + setTimeout(() => { |
| 68 | + this.setState({ |
| 69 | + listA: { |
| 70 | + ...listA, |
| 71 | + data: dataMutatorA(listA.data), |
| 72 | + isLoading: false, |
| 73 | + errorMsg: undefined, |
| 74 | + }, |
| 75 | + }); |
| 76 | + }, delay); |
| 77 | + } |
| 78 | + |
| 79 | + changeDataB(delay = 0) { |
| 80 | + const { listB } = this.state; |
| 81 | + const { dataMutatorB } = this.props; |
| 82 | + |
| 83 | + if (delay) { |
| 84 | + this.setState({ |
| 85 | + listB: { |
| 86 | + ...listB, |
| 87 | + isLoading: true, |
| 88 | + }, |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + setTimeout(() => { |
| 93 | + this.setState({ |
| 94 | + listB: { |
| 95 | + ...listB, |
| 96 | + data: dataMutatorB(listB.data), |
| 97 | + isLoading: false, |
| 98 | + errorMsg: undefined, |
| 99 | + }, |
| 100 | + }); |
| 101 | + }, delay); |
| 102 | + } |
| 103 | + |
| 104 | + toggleDefaultState() { |
| 105 | + this.setState({ |
| 106 | + listA: { |
| 107 | + ...this.defaultStateA, |
| 108 | + }, |
| 109 | + listB: { |
| 110 | + ...this.defaultStateB, |
| 111 | + }, |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + toggleLoadingState() { |
| 116 | + this.setState({ |
| 117 | + listA: { |
| 118 | + ...this.defaultStateA, |
| 119 | + data: EMPTY_LIST, |
| 120 | + isLoading: true, |
| 121 | + }, |
| 122 | + listB: { |
| 123 | + ...this.defaultStateB, |
| 124 | + data: EMPTY_LIST, |
| 125 | + isLoading: true, |
| 126 | + }, |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + toggleErrorState() { |
| 131 | + this.setState({ |
| 132 | + listA: { |
| 133 | + ...this.defaultStateA, |
| 134 | + data: EMPTY_LIST, |
| 135 | + errorMsg: 'Error! Fake data A has gone rogue!', |
| 136 | + }, |
| 137 | + listB: { |
| 138 | + ...this.defaultStateB, |
| 139 | + data: EMPTY_LIST, |
| 140 | + errorMsg: 'Error! Fake data B has gone rogue!', |
| 141 | + }, |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + render() { |
| 146 | + const { listA, listB } = this.state; |
| 147 | + const { |
| 148 | + ListComponent, extraPropsA, extraPropsB, listComponentProps, |
| 149 | + } = this.props; |
| 150 | + |
| 151 | + const emptyTextA = listA.isLoading ? 'Loading...' : listA.errorMsg; |
| 152 | + const emptyTextB = listB.isLoading ? 'Loading...' : listB.errorMsg; |
| 153 | + |
| 154 | + return ( |
| 155 | + <View style={style.container}> |
| 156 | + <Text style={style.title}> |
| 157 | + {ListComponent.displayName || ListComponent.name} |
| 158 | + </Text> |
| 159 | + <View style={style.controlPanelContainer}> |
| 160 | + <Text style={style.controlPanelLabel}> |
| 161 | + State: |
| 162 | + </Text> |
| 163 | + <Button |
| 164 | + onPress={() => this.toggleDefaultState()} |
| 165 | + title="Default" |
| 166 | + /> |
| 167 | + <View style={style.controlPanelSpacer} /> |
| 168 | + <Button |
| 169 | + onPress={() => this.toggleLoadingState()} |
| 170 | + title="Loading" |
| 171 | + /> |
| 172 | + <View style={style.controlPanelSpacer} /> |
| 173 | + <Button |
| 174 | + onPress={() => this.toggleErrorState()} |
| 175 | + title="Error" |
| 176 | + /> |
| 177 | + <View style={style.controlPanelSpacer} /> |
| 178 | + </View> |
| 179 | + <View style={style.listContainer}> |
| 180 | + <View style={style.list}> |
| 181 | + <View style={style.listButton}> |
| 182 | + <Button |
| 183 | + onPress={() => this.changeDataA()} |
| 184 | + title="Update data (or pull-refresh)" |
| 185 | + /> |
| 186 | + </View> |
| 187 | + <ListComponent |
| 188 | + refreshControl={ |
| 189 | + <RefreshControl |
| 190 | + refreshing={listA.isLoading} |
| 191 | + onRefresh={() => this.changeDataA(MOCK_DELAY)} |
| 192 | + /> |
| 193 | + } |
| 194 | + {...listComponentProps} |
| 195 | + {...extraPropsA} |
| 196 | + immutableData={listA.data} |
| 197 | + renderEmptyInList={emptyTextA} |
| 198 | + /> |
| 199 | + </View> |
| 200 | + <View style={style.list}> |
| 201 | + <View style={style.listButton}> |
| 202 | + <Button |
| 203 | + onPress={() => this.changeDataB()} |
| 204 | + title="Update data (or pull-refresh)" |
| 205 | + /> |
| 206 | + </View> |
| 207 | + <ListComponent |
| 208 | + refreshControl={ |
| 209 | + <RefreshControl |
| 210 | + refreshing={listB.isLoading} |
| 211 | + onRefresh={() => this.changeDataB(MOCK_DELAY)} |
| 212 | + /> |
| 213 | + } |
| 214 | + {...listComponentProps} |
| 215 | + {...extraPropsB} |
| 216 | + immutableData={listB.data} |
| 217 | + renderEmptyInList={emptyTextB} |
| 218 | + /> |
| 219 | + </View> |
| 220 | + </View> |
| 221 | + </View> |
| 222 | + ); |
| 223 | + } |
| 224 | + |
| 225 | +} |
| 226 | + |
| 227 | +export default GenericListExample; |
0 commit comments