Guest User

Untitled

a guest
Jan 23rd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. // @flow
  2. import React from "react";
  3. import styled from "styled-components";
  4. import Select from "@material-ui/core/Select";
  5. import MenuItem from "@material-ui/core/MenuItem";
  6. import InputLabel from "@material-ui/core/InputLabel";
  7. import FormControl from "@material-ui/core/FormControl";
  8. import CircularProgress from "@material-ui/core/CircularProgress";
  9. import { withStyles } from "@material-ui/core/styles";
  10.  
  11. import UNINITIALISED from "modules/uninitialised";
  12. import DemandTable from "./table";
  13.  
  14. import type { DemandMetaDataReportItem } from "shared/demand_data";
  15.  
  16. import typeof FetchDemandDataGroupsAction from "modules/demand_data/actions/demand_data_groups_fetch";
  17. import typeof ClearDemandDataGroupsAction from "modules/demand_data/actions/demand_data_groups_clear";
  18.  
  19. const styles = theme => ({
  20. formControl: {
  21. margin: theme.spacing.unit,
  22. minWidth: 120,
  23. },
  24. });
  25.  
  26. const Title = styled.h2`
  27. text-align: center;
  28. `;
  29.  
  30. const CenteredDiv = styled.div`
  31. justify-content: center;
  32. display: flex;
  33. `;
  34.  
  35. const LoadingWrapper = styled.div`
  36. display: flex;
  37. align-items: center;
  38. justify-content: center;
  39. height: 100px;
  40. `;
  41.  
  42. type Props = {
  43. types: Object,
  44. groups: DemandMetaDataReportItem[],
  45. fetchDemandDataGroups: FetchDemandDataGroupsAction,
  46. clearDemandDataGroups: ClearDemandDataGroupsAction,
  47. classes: Object,
  48. };
  49.  
  50. type State = {
  51. type: string,
  52. subType: string,
  53. loading: boolean,
  54. };
  55.  
  56. class DemandData extends React.Component<Props, State> {
  57. constructor(props: Props) {
  58. super(props);
  59.  
  60. if (props.types === UNINITIALISED) {
  61. this.state = {
  62. type: "",
  63. subType: "",
  64. loading: false,
  65. };
  66. } else {
  67. const type = Object.keys(props.types)[0];
  68. const subType = props.types[type][0];
  69.  
  70. this.state = {
  71. type,
  72. subType,
  73. loading: true,
  74. };
  75.  
  76. this.props.fetchDemandDataGroups([subType]);
  77. }
  78. }
  79.  
  80. handleTypeChange = event => {
  81. if (event.target.value !== this.state.type) {
  82. const type = event.target.value;
  83. const subType = this.props.types[type][0];
  84. this.setState({ type, subType, loading: true });
  85. this.props.fetchDemandDataGroups([subType]);
  86. }
  87. };
  88.  
  89. handleSubTypeChange = event => {
  90. if (event.target.value !== this.state.subType) {
  91. this.setState({
  92. subType: event.target.value,
  93. loading: true,
  94. });
  95. this.props.fetchDemandDataGroups([event.target.value]);
  96. }
  97. };
  98.  
  99. componentWillReceiveProps(newProps: Props) {
  100. if (!this.state.type) {
  101. const type = Object.keys(newProps.types)[0];
  102. const subType = newProps.types[type][0];
  103. this.setState({ type, subType, loading: true });
  104. this.props.fetchDemandDataGroups([subType]);
  105. }
  106.  
  107. if (newProps.groups !== UNINITIALISED) {
  108. this.setState({ loading: false });
  109. }
  110. }
  111.  
  112. componentWillUnmount() {
  113. this.props.clearDemandDataGroups();
  114. }
  115.  
  116. render() {
  117. const { classes } = this.props;
  118. const { loading } = this.state;
  119.  
  120. return (
  121. <div>
  122. <Title>Demand Data</Title>
  123. <CenteredDiv>
  124. <FormControl className={classes.formControl}>
  125. <InputLabel htmlFor="type">Type</InputLabel>
  126. <Select
  127. inputProps={{ id: "type" }}
  128. value={this.state.type}
  129. onChange={this.handleTypeChange}
  130. >
  131. {Object.keys(this.props.types).map((type, i) => (
  132. <MenuItem key={i} value={type}>
  133. {type[0].toUpperCase() + type.slice(1)}
  134. </MenuItem>
  135. ))}
  136. </Select>
  137. </FormControl>
  138.  
  139. {this.state.type && (
  140. <FormControl className={classes.formControl}>
  141. <InputLabel htmlFor="sub-type">Sub type</InputLabel>
  142. <Select
  143. inputProps={{ id: "sub-type" }}
  144. value={this.state.subType}
  145. onChange={this.handleSubTypeChange}
  146. >
  147. {this.state.type &&
  148. this.props.types[this.state.type].map((subType, i) => (
  149. <MenuItem key={i} value={subType}>
  150. {subType[0].toUpperCase() + subType.slice(1)}
  151. </MenuItem>
  152. ))}
  153. </Select>
  154. </FormControl>
  155. )}
  156. </CenteredDiv>
  157. {loading && (
  158. <LoadingWrapper>
  159. <CircularProgress />
  160. </LoadingWrapper>
  161. )}
  162. {!loading && <DemandTable data={this.props.groups} />}
  163. </div>
  164. );
  165. }
  166. }
  167.  
  168. export default withStyles(styles)(DemandData);
Add Comment
Please, Sign In to add comment