|
| 1 | +/** |
| 2 | + * @flow |
| 3 | + * |
| 4 | + * An editor for individual building schedules. |
| 5 | + */ |
| 6 | + |
| 7 | +import React from 'react' |
| 8 | +import xor from 'lodash/xor' |
| 9 | +import fromPairs from 'lodash/fromPairs' |
| 10 | +import {ScrollView, Text, StyleSheet} from 'react-native' |
| 11 | +import moment from 'moment-timezone' |
| 12 | +import {TableView, Section, Cell} from 'react-native-tableview-simple' |
| 13 | +import type {SingleBuildingScheduleType, DayOfWeekEnumType} from '../types' |
| 14 | +import {Row} from '../../components/layout' |
| 15 | +import type {TopLevelViewPropsType} from '../../types' |
| 16 | +import {parseHours, blankSchedule} from '../lib' |
| 17 | +import * as c from '../../components/colors' |
| 18 | +import DatePicker from 'react-native-datepicker' |
| 19 | +import {Touchable} from '../../components/touchable' |
| 20 | +import {DeleteButtonCell} from '../../components/cells/delete-button' |
| 21 | + |
| 22 | +export class BuildingHoursScheduleEditorView extends React.PureComponent { |
| 23 | + static navigationOptions = { |
| 24 | + title: 'Edit Schedule', |
| 25 | + } |
| 26 | + |
| 27 | + state: { |
| 28 | + set: ?SingleBuildingScheduleType, |
| 29 | + } = { |
| 30 | + set: this.props.navigation.state.params.initialSet, |
| 31 | + } |
| 32 | + |
| 33 | + props: TopLevelViewPropsType & { |
| 34 | + navigation: { |
| 35 | + state: { |
| 36 | + params: { |
| 37 | + set: SingleBuildingScheduleType, |
| 38 | + onEditSet: (set: SingleBuildingScheduleType) => any, |
| 39 | + onDeleteSet: () => any, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + delete = () => { |
| 46 | + this.props.navigation.state.params.onDeleteSet() |
| 47 | + this.props.navigation.goBack() |
| 48 | + } |
| 49 | + |
| 50 | + onChangeDays = (newDays: DayOfWeekEnumType[]) => { |
| 51 | + this.setState( |
| 52 | + state => ({...state, set: {...state.set, days: newDays}}), |
| 53 | + () => this.props.navigation.state.params.onEditSet(this.state.set), |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + onChangeOpen = (newDate: moment) => { |
| 58 | + this.setState( |
| 59 | + state => ({...state, set: {...state.set, from: newDate.format('h:mma')}}), |
| 60 | + () => this.props.navigation.state.params.onEditSet(this.state.set), |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + onChangeClose = (newDate: moment) => { |
| 65 | + this.setState( |
| 66 | + state => ({...state, set: {...state.set, to: newDate.format('h:mma')}}), |
| 67 | + () => this.props.navigation.state.params.onEditSet(this.state.set), |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + render() { |
| 72 | + const set = this.state.set || blankSchedule() |
| 73 | + |
| 74 | + const {open, close} = parseHours(set, moment()) |
| 75 | + |
| 76 | + return ( |
| 77 | + <ScrollView> |
| 78 | + <TableView> |
| 79 | + <Section header="DAYS"> |
| 80 | + <WeekToggles days={set.days} onChangeDays={this.onChangeDays} /> |
| 81 | + </Section> |
| 82 | + |
| 83 | + <Section header="TIMES"> |
| 84 | + <DatePickerCell |
| 85 | + title="Open" |
| 86 | + date={open} |
| 87 | + onChange={this.onChangeOpen} |
| 88 | + /> |
| 89 | + <DatePickerCell |
| 90 | + title="Close" |
| 91 | + date={close} |
| 92 | + onChange={this.onChangeClose} |
| 93 | + /> |
| 94 | + </Section> |
| 95 | + |
| 96 | + <Section> |
| 97 | + <DeleteButtonCell title="Delete Hours" onPress={this.delete} /> |
| 98 | + </Section> |
| 99 | + </TableView> |
| 100 | + </ScrollView> |
| 101 | + ) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +class WeekToggles extends React.PureComponent { |
| 106 | + props: {days: DayOfWeekEnumType[], onChangeDays: (DayOfWeekEnumType[]) => any} |
| 107 | + |
| 108 | + toggleDay = day => { |
| 109 | + this.props.onChangeDays(xor(this.props.days, [day])) |
| 110 | + } |
| 111 | + |
| 112 | + render() { |
| 113 | + const allDays = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'] |
| 114 | + |
| 115 | + return ( |
| 116 | + <Row justifyContent="center"> |
| 117 | + {allDays.map((day, i) => |
| 118 | + <ToggleButton |
| 119 | + key={day} |
| 120 | + text={day} |
| 121 | + active={this.props.days.includes(day)} |
| 122 | + onPress={this.toggleDay} |
| 123 | + style={i === allDays.length - 1 && styles.finalCell} |
| 124 | + />, |
| 125 | + )} |
| 126 | + </Row> |
| 127 | + ) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +class ToggleButton extends React.PureComponent { |
| 132 | + props: { |
| 133 | + active: boolean, |
| 134 | + text: string, |
| 135 | + onPress: (newState: string) => any, |
| 136 | + style?: any, |
| 137 | + } |
| 138 | + |
| 139 | + onPress = () => this.props.onPress(this.props.text) |
| 140 | + |
| 141 | + render() { |
| 142 | + const {text, style, active} = this.props |
| 143 | + return ( |
| 144 | + <Touchable |
| 145 | + highlight={false} |
| 146 | + containerStyle={[style, styles.dayWrapper, active && styles.activeDay]} |
| 147 | + onPress={this.onPress} |
| 148 | + > |
| 149 | + <Text style={[styles.dayText, active && styles.activeDayText]}> |
| 150 | + {text} |
| 151 | + </Text> |
| 152 | + </Touchable> |
| 153 | + ) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +class DatePickerCell extends React.PureComponent { |
| 158 | + props: { |
| 159 | + date: moment, |
| 160 | + title: string, |
| 161 | + onChange?: (date: moment) => any, |
| 162 | + _ref?: (ref: any) => any, |
| 163 | + } |
| 164 | + |
| 165 | + _picker: any |
| 166 | + openPicker = () => {} //this._picker.onPressDate() |
| 167 | + |
| 168 | + setRef = (ref: any) => { |
| 169 | + this._picker = ref |
| 170 | + this.props._ref && this.props._ref(ref) |
| 171 | + } |
| 172 | + |
| 173 | + onChange = (newDate: moment) => { |
| 174 | + console.log(newDate) |
| 175 | + this.props.onChange && this.props.onChange(newDate) |
| 176 | + } |
| 177 | + |
| 178 | + render() { |
| 179 | + const {date, title} = this.props |
| 180 | + const accessory = ( |
| 181 | + <Date ref={this.setRef} date={date.toDate()} onChange={this.onChange} /> |
| 182 | + ) |
| 183 | + |
| 184 | + return ( |
| 185 | + <Cell |
| 186 | + title={title} |
| 187 | + cellStyle="RightDetail" |
| 188 | + cellAccessoryView={accessory} |
| 189 | + onPress={this.openPicker} |
| 190 | + /> |
| 191 | + ) |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +const Date = ({date, onChange}) => { |
| 196 | + const format = 'h:mma' |
| 197 | + |
| 198 | + const callback = (newDateString: string) => { |
| 199 | + const oldMoment = moment(date) |
| 200 | + const newMoment = moment(newDateString, format) |
| 201 | + |
| 202 | + oldMoment.hours(newMoment.hours()) |
| 203 | + oldMoment.minutes(newMoment.minutes()) |
| 204 | + |
| 205 | + onChange(oldMoment) |
| 206 | + } |
| 207 | + |
| 208 | + return ( |
| 209 | + <DatePicker |
| 210 | + date={date} |
| 211 | + style={styles.datePicker} |
| 212 | + mode="time" |
| 213 | + format={format} |
| 214 | + is24Hour={false} |
| 215 | + confirmBtnText="Confirm" |
| 216 | + cancelBtnText="Cancel" |
| 217 | + showIcon={false} |
| 218 | + onDateChange={callback} |
| 219 | + customStyles={{ |
| 220 | + dateInput: styles.datePickerInput, |
| 221 | + dateText: styles.datePickerText, |
| 222 | + }} |
| 223 | + /> |
| 224 | + ) |
| 225 | +} |
| 226 | + |
| 227 | +const styles = StyleSheet.create({ |
| 228 | + dayWrapper: { |
| 229 | + flex: 1, |
| 230 | + alignItems: 'center', |
| 231 | + paddingVertical: 10, |
| 232 | + paddingHorizontal: 2, |
| 233 | + backgroundColor: c.white, |
| 234 | + borderRightWidth: StyleSheet.hairlineWidth, |
| 235 | + borderRightColor: c.iosSeparator, |
| 236 | + }, |
| 237 | + finalCell: { |
| 238 | + borderRightWidth: 0, |
| 239 | + }, |
| 240 | + activeDay: { |
| 241 | + backgroundColor: c.brickRed, |
| 242 | + }, |
| 243 | + dayText: { |
| 244 | + fontSize: 16, |
| 245 | + }, |
| 246 | + activeDayText: { |
| 247 | + color: c.white, |
| 248 | + }, |
| 249 | + datePicker: { |
| 250 | + width: null, |
| 251 | + }, |
| 252 | + datePickerInput: { |
| 253 | + flex: 0, |
| 254 | + borderWidth: 0, |
| 255 | + }, |
| 256 | + datePickerText: { |
| 257 | + color: c.iosDisabledText, |
| 258 | + fontSize: 16, |
| 259 | + }, |
| 260 | +}) |
0 commit comments