<commit_before>/**
 * @flow
 *
 * An editor for individual building schedules.
 */

import React from 'react'
import xor from 'lodash/xor'
import {View, ScrollView, Platform, Text, StyleSheet} from 'react-native'
import moment from 'moment-timezone'
import {TableView, Section, Cell} from 'react-native-tableview-simple'
import type {SingleBuildingScheduleType, DayOfWeekEnumType} from '../types'
import {Row} from '../../components/layout'
import type {TopLevelViewPropsType} from '../../types'
import {parseHours, blankSchedule} from '../lib'
import * as c from '../../components/colors'
import {DatePicker} from '../../components/datepicker'
import {Touchable} from '../../components/touchable'
import {DeleteButtonCell} from '../../components/cells/delete-button'
import {CellToggle} from '../../components/cells/toggle'
import {ListSeparator} from '../../components/list'

export class BuildingHoursScheduleEditorView extends React.PureComponent {
  static navigationOptions = {
    title: 'Edit Schedule',
  }

  props: TopLevelViewPropsType & {
    navigation: {
      state: {
        params: {
          set: SingleBuildingScheduleType,
          onEditSet: (set: SingleBuildingScheduleType) => any,
          onDeleteSet: () => any,
        },
      },
    },
  }

  state: {
    set: ?SingleBuildingScheduleType,
  } = {
    set: this.props.navigation.state.params.initialSet,
  }

  delete = () => {
    this.props.navigation.state.params.onDeleteSet()
    this.props.navigation.goBack()
  }

  onChangeDays = (newDays: DayOfWeekEnumType[]) => {
    this.setState(
      state => ({...state, set: {...state.set, days: newDays}}),
      () => this.props.navigation.state.params.onEditSet(this.state.set),
    )
  }

  onChangeOpen = (newDate: moment) => {
    this.setState(
      state => ({...state, set: {...state.set, from: newDate.format('h:mma')}}),
      () => this.props.navigation.state.params.onEditSet(this.state.set),
    )
  }

  onChangeClose = (newDate: moment) => {
    this.setState(
      state => ({...state, set: {...state.set, to: newDate.format('h:mma')}}),
      () => this.props.navigation.state.params.onEditSet(this.state.set),
    )
  }

  render() {
    const set = this.state.set || blankSchedule()

    const {open, close} = parseHours(set, moment())

    return (
      <ScrollView>
        <TableView>
          <Section header="DAYS">
            <WeekToggles days={set.days} onChangeDays={this.onChangeDays} />
          </Section>

          <Section header="TIMES">
            <DatePickerCell
              title="Open"
              date={open}
              onChange={this.onChangeOpen}
            />
            <DatePickerCell
              title="Close"
              date={close}
              onChange={this.onChangeClose}
            />
          </Section>

          <Section>
            <DeleteButtonCell title="Delete Hours" onPress={this.delete} />
          </Section>
        </TableView>
      </ScrollView>
    )
  }
}

class WeekTogglesIOS extends React.PureComponent {
  props: {days: DayOfWeekEnumType[], onChangeDays: (DayOfWeekEnumType[]) => any}

  toggleDay = day => {
    this.props.onChangeDays(xor(this.props.days, [day]))
  }

  render() {
    const allDays = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su']

    return (
      <Row justifyContent="center" alignItems="stretch">
        {allDays.map(day =>
          <ToggleButton
            key={day}
            text={day}
            active={this.props.days.includes(day)}
            onPress={this.toggleDay}
          />,
        )}
      </Row>
    )
  }
}

class WeekTogglesAndroid extends React.PureComponent {
  props: {days: DayOfWeekEnumType[], onChangeDays: (DayOfWeekEnumType[]) => any}

  toggleDay = day => {
    this.props.onChangeDays(xor(this.props.days, [day]))
  }

  render() {
    const allDays = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su']

    return (
      <View>
        {allDays.map((day, i) =>
          <View key={day}>
            <CellToggle
              key={day}
              label={day}
              value={this.props.days.includes(day)}
              onChange={() => this.toggleDay(day)}
            />
            {i === allDays.length - 1
              ? null
              : <ListSeparator force={true} />}
          </View>,
        )}
      </View>
    )
  }
}

class ToggleButton extends React.PureComponent {
  props: {
    active: boolean,
    text: string,
    onPress: (newState: string) => any,
  }

  onPress = () => this.props.onPress(this.props.text)

  render() {
    const {text, active} = this.props
    return (
      <Touchable
        highlight={false}
        containerStyle={[styles.dayWrapper, active && styles.activeDay]}
        onPress={this.onPress}<commit_msg>run prettier<commit_after>/**
 * @flow
 *
 * An editor for individual building schedules.
 */

import React from 'react'
import xor from 'lodash/xor'
import {View, ScrollView, Platform, Text, StyleSheet} from 'react-native'
import moment from 'moment-timezone'
import {TableView, Section, Cell} from 'react-native-tableview-simple'
import type {SingleBuildingScheduleType, DayOfWeekEnumType} from '../types'
import {Row} from '../../components/layout'
import type {TopLevelViewPropsType} from '../../types'
import {parseHours, blankSchedule} from '../lib'
import * as c from '../../components/colors'
import {DatePicker} from '../../components/datepicker'
import {Touchable} from '../../components/touchable'
import {DeleteButtonCell} from '../../components/cells/delete-button'
import {CellToggle} from '../../components/cells/toggle'
import {ListSeparator} from '../../components/list'

export class BuildingHoursScheduleEditorView extends React.PureComponent {
  static navigationOptions = {
    title: 'Edit Schedule',
  }

  props: TopLevelViewPropsType & {
    navigation: {
      state: {
        params: {
          set: SingleBuildingScheduleType,
          onEditSet: (set: SingleBuildingScheduleType) => any,
          onDeleteSet: () => any,
        },
      },
    },
  }

  state: {
    set: ?SingleBuildingScheduleType,
  } = {
    set: this.props.navigation.state.params.initialSet,
  }

  delete = () => {
    this.props.navigation.state.params.onDeleteSet()
    this.props.navigation.goBack()
  }

  onChangeDays = (newDays: DayOfWeekEnumType[]) => {
    this.setState(
      state => ({...state, set: {...state.set, days: newDays}}),
      () => this.props.navigation.state.params.onEditSet(this.state.set),
    )
  }

  onChangeOpen = (newDate: moment) => {
    this.setState(
      state => ({...state, set: {...state.set, from: newDate.format('h:mma')}}),
      () => this.props.navigation.state.params.onEditSet(this.state.set),
    )
  }

  onChangeClose = (newDate: moment) => {
    this.setState(
      state => ({...state, set: {...state.set, to: newDate.format('h:mma')}}),
      () => this.props.navigation.state.params.onEditSet(this.state.set),
    )
  }

  render() {
    const set = this.state.set || blankSchedule()

    const {open, close} = parseHours(set, moment())

    return (
      <ScrollView>
        <TableView>
          <Section header="DAYS">
            <WeekToggles days={set.days} onChangeDays={this.onChangeDays} />
          </Section>

          <Section header="TIMES">
            <DatePickerCell
              title="Open"
              date={open}
              onChange={this.onChangeOpen}
            />
            <DatePickerCell
              title="Close"
              date={close}
              onChange={this.onChangeClose}
            />
          </Section>

          <Section>
            <DeleteButtonCell title="Delete Hours" onPress={this.delete} />
          </Section>
        </TableView>
      </ScrollView>
    )
  }
}

class WeekTogglesIOS extends React.PureComponent {
  props: {days: DayOfWeekEnumType[], onChangeDays: (DayOfWeekEnumType[]) => any}

  toggleDay = day => {
    this.props.onChangeDays(xor(this.props.days, [day]))
  }

  render() {
    const allDays = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su']

    return (
      <Row justifyContent="center" alignItems="stretch">
        {allDays.map(day =>
          <ToggleButton
            key={day}
            text={day}
            active={this.props.days.includes(day)}
            onPress={this.toggleDay}
          />,
        )}
      </Row>
    )
  }
}

class WeekTogglesAndroid extends React.PureComponent {
  props: {days: DayOfWeekEnumType[], onChangeDays: (DayOfWeekEnumType[]) => any}

  toggleDay = day => {
    this.props.onChangeDays(xor(this.props.days, [day]))
  }

  render() {
    const allDays = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su']

    return (
      <View>
        {allDays.map((day, i) =>
          <View key={day}>
            <CellToggle
              key={day}
              label={day}
              value={this.props.days.includes(day)}
              onChange={() => this.toggleDay(day)}
            />
            {i === allDays.length - 1 ? null : <ListSeparator force={true} />}
          </View>,
        )}
      </View>
    )
  }
}

class ToggleButton extends React.PureComponent {
  props: {
    active: boolean,
    text: string,
    onPress: (newState: string) => any,
  }

  onPress = () => this.props.onPress(this.props.text)

  render() {
    const {text, active} = this.props
    return (
      <Touchable
        highlight={false}
        containerStyle={[styles.dayWrapper, active && styles.activeDay]}
        onPress={this.onPress}
{'fraction_non_alphanumeric': 0.15901171998732974, 'fraction_numerical': 0.00021117094287825995, 'mean_word_length': 2.3446327683615817, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6192339', 'n_tokens_mistral': 2938, 'n_tokens_neox': 2882, 'n_words': 574}