// @flow import React from 'react'; import type { ChecklistResource } from '../../types'; import { getCheckedStatusById, setCheckedStatusById } from './utils'; import Card from '../Card'; import LoadingChecklistItem from '../LoadingChecklistItem'; import { Heading } from './Heading'; import { Apps } from './Apps'; import { Resources } from './Resources'; import { Container, CheckboxContainer, CardContent, ResourceContent, Divider, Description, Content, } from './style'; type Props = { resource: ChecklistResource, }; type State = { isChecked: boolean, isLoading: boolean, isCollapsed: boolean, contentHeight: number, }; class ChecklistItem extends React.Component { state = { isChecked: false, isLoading: true, isCollapsed: true, contentHeight: 2000, }; contentContainer: { current: null | HTMLDivElement } constructor(props: Props) { super(props); this.contentContainer = React.createRef(); } componentDidMount() { const { resource } = this.props; const isChecked = getCheckedStatusById(resource.id); return this.setState({ isChecked, isLoading: false, isCollapsed: isChecked, }); } componentDidUpdate(prevProps: Props, prevState: State) { if (prevState.isLoading && !this.state.isLoading && this.contentContainer.current) { return this.setState({ contentHeight: this.contentContainer.current.scrollHeight, }) } } handleSetChecked = () => { const { isChecked } = this.state; const { resource } = this.props; setCheckedStatusById(resource.id, !isChecked); return this.setState({ isChecked: !isChecked, isCollapsed: !isChecked }); }; uncollapse = () => { this.setState(state => ({ isCollapsed: !state.isCollapsed })); this.contentContainer.current && this.contentContainer.current.focus(); }; handleAppsExpand = (appsContainerHeight: number) => { return this.contentContainer.current && this.setState({ contentHeight: this.contentContainer.current.scrollHeight + appsContainerHeight, }) } render() { const { isChecked, isLoading, isCollapsed, contentHeight } = this.state; const { resource } = this.props; if (isLoading) return ; return ( {resource.apps && ( )} {resource.resources && ( )} ); } } export default ChecklistItem;