Skip to content

Commit

Permalink
Add mobx store and make it available to all relevant React components
Browse files Browse the repository at this point in the history
  • Loading branch information
katiepantell committed Feb 18, 2020
1 parent 05f358a commit 53d5110
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 106 deletions.
141 changes: 51 additions & 90 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.4.0",
"@testing-library/user-event": "^7.2.1",
"mobx": "^5.15.4",
"mobx-react": "^6.1.8",
"react": "^16.12.0",
"react-bootstrap": "^1.0.0-beta.16",
"react-dom": "^16.12.0",
Expand Down
17 changes: 11 additions & 6 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import {Provider} from 'mobx-react';
import React, { Component } from 'react';

import About from './components/About';
import Home from './components/Home';
import FormContainer from './components/FormContainer';
import RentalDataStore from './rentalDataStore';

import { Switch, Route } from 'react-router-dom'; // sets up routing

class App extends Component {
constructor() {
super();
this.state = {}
this.store = new RentalDataStore();
}

render() {
return (
<div className="wrapper">
<Switch>
<Route exact path='/' component={Home} />
<Route exact path='/about' component={About} />
<Route exact path='/form' component={FormContainer} />
</Switch>
<Provider store={this.store}>
<Switch>
<Route exact path='/' component={Home} />
<Route exact path='/about' component={About} />
<Route exact path='/form' component={FormContainer} />
</Switch>
</Provider>
</div>
);
}
Expand Down
7 changes: 5 additions & 2 deletions src/components/FormContainer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* eslint-disable default-case */
import {inject, observer} from 'mobx-react';
import React, { Component } from 'react';
import PropertyDetails from './PropertyDetails';
import UnitDetails from './UnitDetails'
import Thanks from './Thanks';

export class FormContainer extends Component {

const FormContainer = inject('store')(observer(class FormContainer extends Component {

state = {
step: 1,
firstName: '',
Expand Down Expand Up @@ -131,6 +134,6 @@ export class FormContainer extends Component {
return <Thanks />
}
}
}
}));

export default FormContainer;
6 changes: 4 additions & 2 deletions src/components/Home.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {inject, observer} from 'mobx-react';
import React, { Component } from 'react';
import Header from '../components/Header';
import Footer from '../components/Footer';
import NewFormButton from './NewFormButton';
import AboutButton from './AboutButton';

export class Home extends Component {

const Home = inject('store')(observer(class Home extends Component {
render() {
return (
<div class="wrapper">
Expand All @@ -29,6 +31,6 @@ export class Home extends Component {
</div>
);
}
}
}));

export default Home;
8 changes: 4 additions & 4 deletions src/components/PropertyDetails.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { Component } from 'react'
import {inject, observer} from 'mobx-react';
import React, { Component } from 'react';
import { Form, Button } from 'react-bootstrap';

import Header from './Header';
import Footer from './Footer';


export class PropertyDetails extends Component {

const PropertyDetails = inject('store')(observer(class PropertyDetails extends Component {

saveAndContinue = (e) => {
e.preventDefault()
Expand Down Expand Up @@ -75,6 +75,6 @@ export class PropertyDetails extends Component {
</React.Fragment>
)
}
}
}));

export default PropertyDetails
6 changes: 4 additions & 2 deletions src/components/UnitDetails.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {inject, observer} from 'mobx-react';
import React, { Component } from 'react'
import { Form, Button } from 'react-bootstrap';
import Header from './Header';
import Footer from './Footer';


export class UnitDetails extends Component {
const UnitDetails = inject('store')(observer(class UnitDetails extends Component {

saveAndContinue = (e) => {
e.preventDefault()
this.props.nextStep()
Expand All @@ -30,6 +32,6 @@ export class UnitDetails extends Component {
</React.Fragment>
)
}
}
}));

export default UnitDetails
142 changes: 142 additions & 0 deletions src/rentalDataStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import {action, observable, decorate} from 'mobx';
import React from 'react';

/*
How to interface with this store:
import {inject, observer} from 'mobx-react';
const MyClassName = inject('store')(observer(class MyClassName extends Component {
...
this.props.store.callMethodOnStore();
...
const variableFromStore = this.props.store.variableFromStore;
...
}));
export default MyClassName;
*/

export default class RentalDataStore {
// Owner data
ownerFirstName = null;
ownerLastName = null;
ownershipType = null;
yearOfAcquisition = null;

// Rental property data
streetAddress = null;
city = null;
state = null;
zipCode = null;
hasMultipleUnits = false;

// Unit data
unitData = [];

storeOwnerAndPropertyData(data) {
this.ownerFirstName = data.firstName;
this.ownerLastName = data.lastName;
this.ownershipType = data.ownershipType;
this.yearOfAcquisition = data.yearOfAcquisition;
this.streetAddress = data.streetAddress;
this.city = data.city;
this.state = data.state;
this.zipCode = data.zipCode;
}

setHasMulitipleUnits(event) {
this.hasMultipleUnits = event.hasMultipleUnits;
}

addRentalUnit(data) {
this.unitData.push(new RentalUnitData(data));
}

sendDataToNetlify() {
const form = (
<form name="rental-data" method="POST" data-netlify="true">
<input type="text" name="first-name" value={this.ownerFirstName} />
<input type="text" name="last-name" value={this.ownerLastName} />
<input type="text" name="ownership-type" value={this.ownershipType} />
<input type="number" name="year-of-acquisition" value={this.yearOfAcquisition} />
<input type="text" name="street-address" value={this.streetAddress} />
<input type="text" name="city" value={this.city} />
<input type="text" name="state" value={this.state} />
<input type="number" name="zip-code" value={this.zipCode} />
{this.unitData.map((unit, index) => (
<div>
<input type="number" name={`unit-${index+1}-num-beds`} value={unit.numBeds} />
<input type="number" name={`unit-${index+1}-num-baths`} value={unit.numBaths} />
<input type="text" name={`unit-${index+1}-occupancy-status`} value={unit.occupancyStatus} />
<input type="month" name={`unit-${index+1}-occupancy-start-month`} value={unit.occupancyStartMonth} />
<input type="number" name={`unit-${index+1}-occupancy-start-year`} value={unit.occupancyStartYear} />
<input type="number" name={`unit-${index+1}-num-months-rented`} value={unit.numMonthsRentedLastYear} />
<input type="number" name={`unit-${index+1}-num-vacancy-days`} value={unit.numVacancyDaysLastYear} />
<input type="number" name={`unit-${index+1}-jan-rent-amount`} value={unit.rentAmountJanuary} />
<input type="number" name={`unit-${index+1}-jan-utility-amount`} value={unit.utilityAmountJanuary} />
<input type="month" name={`unit-${index+1}-effective-month`} value={unit.effectiveMonth} />
<input type="date" name={`unit-${index+1}-effective-date`} value={unit.effectiveDate} />
<input type="number" name={`unit-${index+1}-increase-amount`} value={unit.increaseAmount} />
<input type="number" name={`unit-${index+1}-num-evicted`} value={unit.numTenantsEvicted} />
<input type="text" name={`unit-${index+1}-eviction-reasons`} value={unit.evictionReasons} />
</div>
))}
</form>
);
form.submit();
}
}

decorate(RentalDataStore, {
ownerFirstName: observable,
ownerLastName: observable,
ownershipType: observable,
yearOfAcquisition: observable,
streetAddress: observable,
city: observable,
state: observable,
zipCode: observable,
hasMultipleUnits: observable,
unitData: observable,
storeOwnerAndPropertyData: action,
setHasMulitipleUnits: action,
addRentalUnit: action,
sendDataToNetlify: action,
});

class RentalUnitData {
// Occupancy data
numBeds = null;
numBaths = null;
occupancyStatus = null;
occupancyStartMonth = null;
occupancyStartYear = null;
numMonthsRentedLastYear = null;
numVacancyDaysLastYear = null;
rentAmountJanuary = null;
utilityAmountJanuary = null;

// Rent Increase & Eviction Data
effectiveMonth = null;
effectiveDate = null;
increaseAmount = null; // Positive value indicates increase; negative value indicates decrease
numTenantsEvicted = null;
evictionReasons = null;

constructor(data) {
this.numBeds = data.numBeds;
this.numBaths = data.numBaths;
this.occupancyStatus = data.occupancyStatus;
this.occupancyStartMonth = data.occupancyStartMonth;
this.occupancyStartYear = data.occupancyStartYear;
this.numMonthsRentedLastYear = data.numMonthsRentedLastYear;
this.numVacancyDaysLastYear = data.numVacancyDaysLastYear;
this.rentAmountJanuary = data.rentAmountJanuary;
this.utilityAmountJanuary = data.utilityAmountJanuary;
this.effectiveMonth = data.effectiveMonth;
this.effectiveDate = data.effectiveDate;
this.increaseAmount = data.increaseAmount;
this.numTenantsEvicted = data.numTenantsEvicted;
this.evictionReasons = data.evictionReasons;
}
}

0 comments on commit 53d5110

Please sign in to comment.