import * as React from "react"; import { connect } from "react-redux"; import { Row } from "../../ui/index"; import { mapStateToProps } from "./map_state_to_props"; import { FarmEventProps, CalendarOccurrence, FarmEventState } from "../interfaces"; import moment from "moment"; import { Content } from "../../constants"; import { Panel, DesignerNavTabs } from "../panel_header"; import { Link } from "../../link"; import { DesignerPanel, DesignerPanelContent, DesignerPanelTop } from "../plants/designer_panel"; import { EmptyStateWrapper, EmptyStateGraphic } from "../../ui/empty_state_wrapper"; import { some, uniq, map, sortBy } from "lodash"; import { t } from "../../i18next_wrapper"; const filterSearch = (term: string) => (item: CalendarOccurrence) => item.heading.toLowerCase().includes(term) || (item.subheading && item.subheading.toLowerCase().includes(term)); export class PureFarmEvents extends React.Component { state: FarmEventState = { searchTerm: "" }; get searchTerm() { return this.state.searchTerm.toLowerCase(); } resetCalendar = () => { this.setState({ searchTerm: "" }); const panel = document.querySelector(".farm-events"); if (panel) { panel.scrollTo(0, 0); } } innerRows = (items: CalendarOccurrence[]) => { return sortBy(items, x => x.sortKey) .filter(filterSearch(this.searchTerm)) .map((occur, index) => { const url = `/app/designer/events/` + (occur.id || "UNSAVED_EVENT").toString(); const heading = occur.subheading ? occur.subheading : occur.heading; const subHeading = occur.subheading ?

{occur.heading}

:

; return

{occur.timeStr}
{heading} {subHeading}
; }); } renderCalendarRowsInYear(year: number) { return this.props.calendarRows.filter((day) => { return day.year == year; }) .filter(item => !this.searchTerm || some(item.items.map(filterSearch(this.searchTerm)))) .map(item => { return
{item.month}
{item.day}
{this.innerRows(item.items)}
; }); } renderCalendarRows() { const years = uniq(map(this.props.calendarRows, "year")); return years.map(year => { return
20{year}
{this.renderCalendarRowsInYear(year)}
; }); } /** FarmEvents will generate some very unexpected results if the user has * not set a timezone for the bot (defaults to 0 UTC offset, which could be * far from user's local time). */ tzwarning = () => { return

Timezone Required

{t(Content.SET_TIMEZONE_HEADER)}

{t(Content.SET_TIMEZONE_BODY)}

; }; normalContent = () => { return
this.setState({ searchTerm: e.currentTarget.value })} placeholder={t("Search events...")} />
0} title={t("No events scheduled.")} text={t(Content.NOTHING_SCHEDULED)} colorScheme="events" graphic={EmptyStateGraphic.farm_events}> {this.renderCalendarRows()}
; }; render() { return {this.props.timezoneIsSet ? this.normalContent() : this.tzwarning()} ; } } /** This is intentional. It is not a hack or a work around. * It avoids mocking `connect` in unit tests. * See testing pattern noted here: https://github.com/airbnb/enzyme/issues/98 */ export const FarmEvents = connect(mapStateToProps)(PureFarmEvents);