|
| 1 | +// @flow |
| 2 | +import * as React from 'react' |
| 3 | +import {ScrollView, View, Text, StyleSheet} from 'react-native' |
| 4 | +import {CellTextField} from '../../components/cells/textfield' |
| 5 | +import {ButtonCell} from '../../components/cells/button' |
| 6 | +import {TableView, Section} from 'react-native-tableview-simple' |
| 7 | +import {submitReport} from './submit' |
| 8 | +import type {WordType} from '../types' |
| 9 | +import * as c from '../../components/colors' |
| 10 | +import type {TopLevelViewPropsType} from '../../types' |
| 11 | + |
| 12 | +type Props = TopLevelViewPropsType & { |
| 13 | + navigation: {state: {params: {item: WordType}}}, |
| 14 | +} |
| 15 | + |
| 16 | +type State = { |
| 17 | + term: string, |
| 18 | + definition: string, |
| 19 | +} |
| 20 | + |
| 21 | +export class DictionaryEditorView extends React.PureComponent<Props, State> { |
| 22 | + static navigationOptions = () => { |
| 23 | + return { |
| 24 | + title: 'Suggest an Edit', |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + static getDerivedStateFromProps(nextProps: Props) { |
| 29 | + let entry = nextProps.navigation.state.params.word |
| 30 | + return { |
| 31 | + term: entry.word, |
| 32 | + definition: entry.definition, |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + state = { |
| 37 | + term: this.props.navigation.state.params.word.word, |
| 38 | + definition: this.props.navigation.state.params.word.definition, |
| 39 | + } |
| 40 | + |
| 41 | + submit = () => { |
| 42 | + submitReport(this.props.navigation.state.params.word, { |
| 43 | + word: this.state.term, |
| 44 | + definition: this.state.definition, |
| 45 | + }) |
| 46 | + } |
| 47 | + |
| 48 | + onChangeTitle = (newTitle: string) => { |
| 49 | + this.setState(() => ({term: newTitle})) |
| 50 | + } |
| 51 | + |
| 52 | + onChangeDefinition = (newDefinition: string) => { |
| 53 | + this.setState(() => ({definition: newDefinition})) |
| 54 | + } |
| 55 | + |
| 56 | + render() { |
| 57 | + let term = this.state.term ? this.state.term.trim() : '' |
| 58 | + let definition = this.state.definition ? this.state.definition.trim() : '' |
| 59 | + |
| 60 | + return ( |
| 61 | + <ScrollView |
| 62 | + keyboardDismissMode="on-drag" |
| 63 | + keyboardShouldPersistTaps="always" |
| 64 | + > |
| 65 | + <View style={styles.helpWrapper}> |
| 66 | + <Text style={styles.helpTitle}>Thanks for spotting a problem!</Text> |
| 67 | + <Text style={styles.helpDescription}> |
| 68 | + If you could tell us what the word and definition should be, |
| 69 | + we’d greatly appreciate it. |
| 70 | + </Text> |
| 71 | + </View> |
| 72 | + |
| 73 | + <TableView> |
| 74 | + <Section header="WORD"> |
| 75 | + <TitleCell onChange={this.onChangeTitle} text={term} /> |
| 76 | + </Section> |
| 77 | + |
| 78 | + <Section header="DEFINITION"> |
| 79 | + <DefinitionCell |
| 80 | + onChange={this.onChangeDefinition} |
| 81 | + text={definition} |
| 82 | + /> |
| 83 | + </Section> |
| 84 | + |
| 85 | + <Section footer="Thanks for reporting!"> |
| 86 | + <ButtonCell onPress={this.submit} title="Submit Report" /> |
| 87 | + </Section> |
| 88 | + </TableView> |
| 89 | + </ScrollView> |
| 90 | + ) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +type TextFieldProps = {text: string, onChange: string => any} |
| 95 | + |
| 96 | +const TitleCell = ({text, onChange = () => {}}: TextFieldProps) => ( |
| 97 | + <CellTextField |
| 98 | + autoCapitalize="words" |
| 99 | + hideLabel={true} |
| 100 | + onChangeText={onChange} |
| 101 | + onSubmitEditing={onChange} |
| 102 | + placeholder="Title" |
| 103 | + returnKeyType="done" |
| 104 | + value={text} |
| 105 | + /> |
| 106 | +) |
| 107 | + |
| 108 | +const DefinitionCell = ({text, onChange = () => {}}: TextFieldProps) => ( |
| 109 | + <CellTextField |
| 110 | + autoCapitalize="sentences" |
| 111 | + hideLabel={true} |
| 112 | + multiline={true} |
| 113 | + onChangeText={onChange} |
| 114 | + onSubmitEditing={onChange} |
| 115 | + placeholder="Definition" |
| 116 | + returnKeyType="default" |
| 117 | + value={text} |
| 118 | + /> |
| 119 | +) |
| 120 | + |
| 121 | +const styles = StyleSheet.create({ |
| 122 | + helpWrapper: { |
| 123 | + backgroundColor: c.white, |
| 124 | + borderWidth: StyleSheet.hairlineWidth, |
| 125 | + borderTopColor: c.iosHeaderTopBorder, |
| 126 | + borderBottomColor: c.iosHeaderBottomBorder, |
| 127 | + marginBottom: 10, |
| 128 | + }, |
| 129 | + helpTitle: { |
| 130 | + fontSize: 16, |
| 131 | + fontWeight: 'bold', |
| 132 | + paddingTop: 15, |
| 133 | + paddingHorizontal: 15, |
| 134 | + }, |
| 135 | + helpDescription: { |
| 136 | + fontSize: 14, |
| 137 | + paddingTop: 5, |
| 138 | + paddingBottom: 15, |
| 139 | + paddingHorizontal: 15, |
| 140 | + }, |
| 141 | +}) |
0 commit comments