import { Component, createRef, JSX, RefObject } from "preact"; import { ToggleableBox } from "./toggleableBox"; import { FoodExcludeTypes } from "./types"; let defaultBroadExclusions = { isVegan: false, isVegetarian: false, } let defaultFoodToggles: FoodExcludeTypes = { containsMeat: true, containsFish: true, containsEggs: true, containsMilk: true, containsAlcohol: true, } export class FoodTypeToggles extends Component<{ onChange?: (toggles: FoodExcludeTypes) => void; }, { toggles: FoodExcludeTypes; broadExclusions: { isVegan: boolean; isVegetarian: boolean; } }> { refs = { "isVegan": createRef(), "isVegetarian": createRef(), "containsMeat": createRef(), "containsFish": createRef(), "containsEggs": createRef(), "containsMilk": createRef(), "containsAlcohol": createRef(), } constructor() { super() this.state = { toggles: { ...defaultFoodToggles }, broadExclusions: defaultBroadExclusions, } } componentDidMount(): void { this.setToDefaults(); this.sendChangedEvent(); } setCheckedStateOfInputBoxes(names: string[], value: boolean) { for (let name of names) { if (Object.keys(this.refs).includes(name)) { let ref: RefObject = this.refs[name]; if (ref.current) { if (Object.keys(this.state.toggles).includes(name)) { this.state.toggles[name] = value; } ref.current.forceChange(value) } else { console.debug(`Can't get current ref for: ${name}`) } } else { console.debug(`Can't find ref for ${name}`) } } } veganModeEnable() { this.vegetarianModeEnable() this.setCheckedStateOfInputBoxes(["containsMilk", "containsEggs"], false); } vegetarianModeEnable() { this.setCheckedStateOfInputBoxes(["containsMeat", "containsFish"], false); } veganModeDisable() { this.setCheckedStateOfInputBoxes(["isVegan"], false); } vegetarianModeDisable() { this.setCheckedStateOfInputBoxes(["isVegetarian"], false); } setToDefaults() { for (let toggle of Object.keys(defaultBroadExclusions)) { console.debug(`Setting broad exclusion ${toggle} to ${defaultBroadExclusions[toggle]}`) this.setCheckedStateOfInputBoxes([toggle], defaultBroadExclusions[toggle]) } for (let toggle of Object.keys(defaultFoodToggles)) { console.debug(`Setting food default of ${toggle} to ${defaultFoodToggles[toggle]}`) console.debug(defaultFoodToggles) this.setCheckedStateOfInputBoxes([toggle], defaultFoodToggles[toggle]) } } sendChangedEvent() { console.debug(`Changed state of food type toggles`, this.state.toggles) if(this.props.onChange) this.props.onChange(this.state.toggles); } render(): JSX.Element { return (

Toggles

Broad Toggles

{ if (isVegan) this.veganModeEnable(); this.sendChangedEvent(); }} /> { if (isVegetarian) this.vegetarianModeEnable(); this.sendChangedEvent(); }} />

Specific Toggles

{ this.state.toggles.containsMeat = containsMeat; if (containsMeat) { this.veganModeDisable(); this.vegetarianModeDisable(); } this.sendChangedEvent(); }} /> { this.state.toggles.containsFish = containsFish; if (containsFish) { this.veganModeDisable(); this.vegetarianModeDisable(); } this.sendChangedEvent(); }} /> { this.state.toggles.containsEggs = containsEggs; if (containsEggs) this.veganModeDisable(); this.sendChangedEvent(); }} /> { this.state.toggles.containsMilk = containsMilk; if (containsMilk) this.veganModeDisable(); this.sendChangedEvent(); }} /> { this.state.toggles.containsAlcohol = containsAlcohol; this.sendChangedEvent(); }} />
); } }