Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from "react";
  2. import { Switch, Route, BrowserRouter as Router } from "react-router-dom";
  3. import "./App.css";
  4. import { fetchOccupants, fetchApartments, createNewStay } from "../../api/api";
  5. import SideBar from "../SideBar/SideBar";
  6. import Apartment from "../Apartment/Apartment";
  7. import Occupant from "../Occupant/Occupant";
  8. import NewOccupantForm from "../NewOccupantForm/NewOccupantForm";
  9. import NewApartmentForm from "../NewApartmentForm/NewApartmentForm";
  10. import OccupantProfile from "../OccupantProfile/OccupantProfile";
  11. import ApartmentProfile from "../ApartmentProfile/ApartmentProfile";
  12.  
  13. class App extends Component {
  14.   constructor(props) {
  15.     super(props);
  16.     this.state = {
  17.       apartments: [],
  18.       occupants: [],
  19.       occupantToAssign: "",
  20.       occupantId: "",
  21.       apartmentId: "",
  22.       checkInDate: "",
  23.       checkOutDate: "",
  24.       success: false,
  25.       message: "",
  26.       dropdown: true,
  27.       renderToggle: false
  28.     };
  29.   }
  30.  
  31.   componentDidMount = async () => {
  32.     try {
  33.       const apartments = await fetchApartments();
  34.       this.setState({ apartments });
  35.       const occupants = await fetchOccupants();
  36.       this.setState({ occupants });
  37.     } catch (err) {
  38.       return err.message;
  39.     }
  40.   };
  41.  
  42.   componentDidUpdate = async (prevProps, prevState) => {
  43.     if (this.state.renderToggle !== prevState.renderToggle) {
  44.       try {
  45.         const apartments = await fetchApartments();
  46.         this.setState({ apartments });
  47.         const occupants = await fetchOccupants();
  48.         this.setState({ occupants });
  49.       } catch (err) {
  50.         return err.message;
  51.       }
  52.     }
  53.   };
  54.  
  55.   triggerRender = () => {
  56.     this.setState(prev => {
  57.       return {
  58.         renderToggle: !prev.renderToggle
  59.       };
  60.     });
  61.   };
  62.  
  63.   handleChange = event => {
  64.     this.setState({ [event.target.id]: event.target.value });
  65.   };
  66.  
  67.   handleClick = (apartmentId, occupantId, occupantName, flag) => {
  68.     this.setState({
  69.       apartmentId,
  70.       occupantId,
  71.       occupantToAssign: occupantName,
  72.       dropdown: flag,
  73.       checkInDate: "",
  74.       checkOutDate: ""
  75.     });
  76.   };
  77.  
  78.   addNewStay = async () => {
  79.     try {
  80.       const response = await createNewStay(
  81.         this.state.occupantId,
  82.         this.state.apartmentId,
  83.         this.state.checkInDate,
  84.         this.state.checkOutDate
  85.       );
  86.       this.setState({
  87.         apartmentId: "",
  88.         occupantId: "",
  89.         occupantToAssign: "",
  90.         dropdown: true,
  91.         success: true,
  92.         message: response,
  93.         checkInDate: "",
  94.         checkOutDate: ""
  95.       });
  96.     } catch (err) {
  97.       this.setState({
  98.         success: false,
  99.         message: "Unable to assign occupant to apartment"
  100.       });
  101.     }
  102.   };
  103.  
  104.   filterByText = (field, id) => {
  105.     if (this.state[id]) {
  106.       return this.state[field].filter(element =>
  107.         element.name.toLowerCase().includes(this.state[id].toLowerCase())
  108.       );
  109.     } else {
  110.       return [];
  111.     }
  112.   };
  113.  
  114.   render() {
  115.     return (
  116.       <section className="app">
  117.         <Router>
  118.           <SideBar />
  119.           <Switch>
  120.             <Route
  121.               exact
  122.               path="/"
  123.               render={props => (
  124.                 <Apartment apartments={this.state.apartments} {...props} />
  125.               )}
  126.             />
  127.             <Route
  128.               exact
  129.               path="/occupants"
  130.               render={props => (
  131.                 <Occupant occupants={this.state.occupants} {...props} />
  132.               )}
  133.             />
  134.             <Route
  135.               path="/apartments/:apartmentId"
  136.               render={props => (
  137.                 <ApartmentProfile
  138.                   apartments={this.state.apartments}
  139.                   {...props}
  140.                   handleChange={this.handleChange}
  141.                   handleClick={this.handleClick}
  142.                   filterByText={this.filterByText}
  143.                   dropdown={this.state.dropdown}
  144.                   addNewStay={this.addNewStay}
  145.                   occupantToAssign={this.state.occupantToAssign}
  146.                   success={this.state.success}
  147.                   message={this.state.message}
  148.                 />
  149.               )}
  150.             />
  151.             <Route
  152.               path="/occupants/:occupantId"
  153.               render={props => (
  154.                 <OccupantProfile occupants={this.state.occupants} {...props} />
  155.               )}
  156.             />
  157.             <Route
  158.               exact
  159.               path="/newApartment"
  160.               render={props => (
  161.                 <NewApartmentForm
  162.                   triggerRender={this.triggerRender}
  163.                   {...props}
  164.                 />
  165.               )}
  166.             />
  167.             <Route
  168.               exact
  169.               path="/newOccupant"
  170.               render={props => (
  171.                 <NewOccupantForm
  172.                   triggerRender={this.triggerRender}
  173.                   {...props}
  174.                 />
  175.               )}
  176.             />
  177.           </Switch>
  178.         </Router>
  179.       </section>
  180.     );
  181.   }
  182. }
  183.  
  184. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement