security-checklist/components/ChecklistItem/index.js

95 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-01-13 07:00:23 +00:00
// @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,
Checkbox,
CardContent,
ResourceContent,
2019-01-13 07:36:51 +00:00
Divider,
2019-01-13 07:00:23 +00:00
} from './style';
type Props = {
resource: ChecklistResource,
};
type State = {
isChecked: boolean,
isLoading: boolean,
2019-01-13 23:55:19 +00:00
isCollapsed: boolean,
2019-01-13 07:00:23 +00:00
};
class ChecklistItem extends React.Component<Props, State> {
2019-01-13 23:55:19 +00:00
state = { isChecked: false, isLoading: true, isCollapsed: true };
2019-01-13 07:00:23 +00:00
componentDidMount() {
const { resource } = this.props;
const isChecked = getCheckedStatusById(resource.id);
2019-01-13 23:55:19 +00:00
return this.setState({
isChecked,
isLoading: false,
isCollapsed: isChecked,
});
2019-01-13 07:00:23 +00:00
}
handleSetChecked = () => {
2019-01-14 00:05:01 +00:00
const { isChecked } = this.state;
2019-01-13 07:00:23 +00:00
const { resource } = this.props;
setCheckedStatusById(resource.id, !isChecked);
2019-01-14 00:02:22 +00:00
return this.setState({ isChecked: !isChecked, isCollapsed: !isChecked });
2019-01-13 07:00:23 +00:00
};
2019-01-14 00:02:22 +00:00
uncollapse = () =>
this.setState(state => ({ isCollapsed: !state.isCollapsed }));
2019-01-13 07:00:23 +00:00
render() {
2019-01-13 23:55:19 +00:00
const { isChecked, isLoading, isCollapsed } = this.state;
2019-01-13 07:00:23 +00:00
const { resource } = this.props;
if (isLoading) return <LoadingChecklistItem />;
return (
<Container>
<Card isChecked={isChecked}>
2019-01-14 00:16:03 +00:00
<CardContent isCollapsed={isCollapsed}>
2019-01-13 07:00:23 +00:00
<CheckboxContainer onClick={this.handleSetChecked}>
<Checkbox isChecked={isChecked} />
</CheckboxContainer>
2019-01-13 23:55:19 +00:00
<ResourceContent isChecked={isChecked} isCollapsed={isCollapsed}>
2019-01-14 00:02:22 +00:00
<Heading
resource={resource}
isCollapsed={isCollapsed}
handleCollapse={this.uncollapse}
/>
2019-01-13 23:50:07 +00:00
2019-01-13 23:55:19 +00:00
{!isCollapsed && resource.apps && (
2019-01-13 20:12:45 +00:00
<React.Fragment>
<Divider />
<Apps resource={resource} />
</React.Fragment>
)}
2019-01-13 23:55:19 +00:00
{!isCollapsed && resource.resources && (
2019-01-13 20:12:45 +00:00
<React.Fragment>
<Divider />
<Resources resource={resource} />
</React.Fragment>
)}
2019-01-13 07:00:23 +00:00
</ResourceContent>
</CardContent>
</Card>
</Container>
);
}
}
export default ChecklistItem;