Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jamesraymondbrown/rent-tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
JackDuluoz committed Feb 14, 2023
2 parents 39444e1 + 15d1179 commit e746288
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 16 deletions.
32 changes: 24 additions & 8 deletions client/src/components/RentList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getBathroomsFromPrices,
} from "./helpers/getDataFromPrices";
import LazyLoad from "react-lazyload";
import WelcomeCard from "./WelcomeCard";

const RentList = () => {
const { state } = useContext(MarkerFilterContext);
Expand Down Expand Up @@ -54,11 +55,10 @@ const RentList = () => {
}
}, [showCharts]);


return (
<div className="RentList">
<div className="slider-container">
<BackButton/>
<BackButton />

<MultiRangeSlider style={{ height: "20px" }} />

Expand Down Expand Up @@ -134,16 +134,32 @@ const RentList = () => {
</table>
</React.Fragment>
) : (
<img
src="https://media.npr.org/assets/img/2013/12/10/ap101018123881-ca0472fba716df4b485fff878b558284cdc89ab9.jpg"
alt="Rent List"
className="image"
/>
<>
<img
src="https://media.npr.org/assets/img/2013/12/10/ap101018123881-ca0472fba716df4b485fff878b558284cdc89ab9.jpg"
alt="Rent List"
className="image"
/>
<WelcomeCard />
{/* <table className="home-right-property-table-top">
<thead>
<tr>
<th>Properties</th>
<th>Prices</th>
</tr>
</thead>
<tbody>
<tr>
<td>{properties.length}</td>
<td>{prices.length}</td>
</tr>
</tbody>
</table> */}
</>
)}
<ChartsPanel />
</div>
);

};

export default RentList;
4 changes: 2 additions & 2 deletions client/src/components/RentList.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
border-bottom: none;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
font-size: .85em;
font-size: 0.85em;
}

.home-right-property-table-bottom {
Expand All @@ -55,7 +55,7 @@
border-top: none;
border-bottom-left-radius: 5px;
border-bottom-left-radius: 5px;
font-size: .85em;
font-size: 0.85em;
}

.home-right-property-table-top thead tr th {
Expand Down
31 changes: 31 additions & 0 deletions client/src/components/WelcomeCard.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { DataBaseContext } from "../providers/DataBaseProvider";
import { Card } from "primereact/card";
import { useContext } from "react";
import "./WelcomeCard.scss";

const WelcomeCard = () => {
const { users, properties, prices } = useContext(DataBaseContext);

return prices === null || properties === null ? (
<div>loading...</div>
) : (
<Card
title="Welcome to Rentproof!"
subTitle=""
className="welcome-card welcome-title"
>
<p className="m-0 welcome-card welcome-message">
Welcome to RentProof! We're currently tracking{" "}
<strong>{prices.length}</strong> prices across{" "}
<strong>{properties.length}</strong> different properties.
</p>
<p className="m-0 welcome-card welcome-message">
Do you have info to share that might help your fellow renters?{" "}
<a href="http://localhost:8000/register">Create an account</a> or{" "}
<a href="http://localhost:8000/login">login</a> to get started!
</p>
</Card>
);
};

export default WelcomeCard;
37 changes: 37 additions & 0 deletions client/src/components/WelcomeCard.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.welcome-card {
padding: 10px;
margin-top: 15px;
margin-left: 15px;
margin-right: 15px;

.p-card-title {
margin-left: 10px;
margin-bottom: 0px;
}

.p-card-subtitle {
margin-left: 10px;
margin-bottom: 0px;
}

.p-card-body {
padding: 5px;
}

.p-card-content {
padding-top: 5px;
padding-bottom: 5px;
}

.welcome-message {
padding: 5px;
margin-left: 5px;
margin-right: 5px;
margin-top: 3px;
margin-bottom: 0px;
}

a {
color: black;
}
}
2 changes: 1 addition & 1 deletion client/src/components/charts/RentGrowthRateVsMarket.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ const RentGrowthRateVsMarket = (props) => {
return <div>Not enough data</div>;
}

console.log("dataLogLog", data);
console.log("dataLogLog", data[data.length - 1].price_difference_percentage);

return (
<div>
Expand Down
9 changes: 4 additions & 5 deletions client/src/components/charts/SelectedPropertyVsAll.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,23 @@ const RentIncreaseChart = (props) => {
}

const CustomTooltip = ({ active, payload, label }) => {
if (active && data.compare_at_price && payload.length) {
if (active && data[0].compare_at_price && payload.length > 1) {
return (
<div className="custom-tooltip">
<p className="label tooltip-text">{`Year: ${label}`}</p>
<p className="tooltip-text blue">{`Actual price: $${payload[0].value}`}</p>
{}
<p className="tooltip-text red">{`Market-adjusted price: $${payload[1].value}`}</p>
{/* {console.log("load", payload)} */}
{console.log("load", payload)}
</div>
);
}
return null;
};

console.log("data", data);
// console.log("data", data[0].compare_at_price);

return data.length < 3 ? (
<div>Not enough data...</div>
<div>Not enough data</div>
) : (
<div>
<div className="chart-title">
Expand Down
176 changes: 176 additions & 0 deletions client/src/components/helpers/getPriceChangePercentage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import { useContext } from "react";
import { DataBaseContext } from "../../providers/DataBaseProvider";
import { MarkerFilterContext } from "../../providers/MarkerFilterProvider";
import { getPriceHistory } from "../helpers/getDataFromPrices";

const getPriceChangePercentage = (currentProperty) => {
const data = [];
const { prices, properties } = useContext(DataBaseContext);
const currentPropertyPrices = getPriceHistory(currentProperty.id, prices);
const averageIncreasePerYear = {
2015: [],
2016: [],
2017: [],
2018: [],
2019: [],
2020: [],
2021: [],
2022: [],
2023: [],
};

const addPricesToData = () => {
for (let price of currentPropertyPrices) {
if (price.admin_status === "approved") {
data.push({
date: parseInt(price.date.substring(0, 4)),
price: parseInt(price.price),
});
}
}
getRentIncreaseAverages(prices, properties, data);
};

const getRentIncreaseAverages = (prices, properties, data) => {
const priceObject = {};
const allIncreasesPerYear = {
2014: [],
2015: [],
2016: [],
2017: [],
2018: [],
2019: [],
2020: [],
2021: [],
2022: [],
2023: [],
};

// Add an array to the priceObject, for each property. The key is that property's ID
for (const property of properties) {
priceObject[property.id] = [];
}

// Push the price data for each property into the correct array
for (const price of prices) {
priceObject[price.property_id].push(price);
}

// Loop through each property to get an integer for querying the priceObject object
for (let property of properties) {
// Another loop for getting the index of the array inside of each priceObject index (priceObject is an object full of arrays)
for (let i = 1; i < priceObject[property.id].length; i++) {
if (priceObject[property.id].length > 3) {
allIncreasesPerYear[
priceObject[property.id][i].date.substring(0, 4)
].push(
Math.round(
((parseInt(priceObject[property.id][i].price) -
parseInt(priceObject[property.id][i - 1].price)) /
parseInt(priceObject[property.id][i - 1].price)) *
100 *
10
) / 10
);
}
}
}

// now we have an allIncreasesPerYear object, that contains an array with the percentage increase for each property, for each year
// Loop through that data to get the average increase per year, and push that to our data array
for (let i = 2015; i <= 2023; i++) {
let sum = 0;
for (const indexValue of allIncreasesPerYear[i]) {
sum += indexValue;
}
averageIncreasePerYear[i] =
Math.round((sum / allIncreasesPerYear[i].length) * 100) / 100;
}
showPricesBasedOnAverages();
};

// Multiplies the initial property price by the average rent increase percentage, to compare
// what it would be like if this property followed the market trend exactly
const showPricesBasedOnAverages = () => {
for (let i = 0; i < data.length; i++) {
if (data[i].date === 2014) {
data[i].compare_at_price = data[i].price;
}
if (data[i].date === 2015 && data[i - 1] !== undefined) {
data[i].compare_at_price = Math.round(
data[i - 1].compare_at_price +
data[i - 1].compare_at_price * (averageIncreasePerYear[2015] / 100)
);
}
if (data[i].date === 2016 && data[i - 1] !== undefined) {
data[i].compare_at_price = Math.round(
data[i - 1].compare_at_price +
data[i - 1].compare_at_price * (averageIncreasePerYear[2016] / 100)
);
}
if (data[i].date === 2017 && data[i - 1] !== undefined) {
data[i].compare_at_price = Math.round(
data[i - 1].compare_at_price +
data[i - 1].compare_at_price * (averageIncreasePerYear[2017] / 100)
);
}
if (data[i].date === 2018 && data[i - 1] !== undefined) {
data[i].compare_at_price = Math.round(
data[i - 1].compare_at_price +
data[i - 1].compare_at_price * (averageIncreasePerYear[2018] / 100)
);
}
if (data[i].date === 2019 && data[i - 1] !== undefined) {
data[i].compare_at_price = Math.round(
data[i - 1].compare_at_price +
data[i - 1].compare_at_price * (averageIncreasePerYear[2019] / 100)
);
}
if (data[i].date === 2020 && data[i - 1] !== undefined) {
data[i].compare_at_price = Math.round(
data[i - 1].compare_at_price +
data[i - 1].compare_at_price * (averageIncreasePerYear[2020] / 100)
);
}
if (data[i].date === 2021 && data[i - 1] !== undefined) {
data[i].compare_at_price = Math.round(
data[i - 1].compare_at_price +
data[i - 1].compare_at_price * (averageIncreasePerYear[2021] / 100)
);
}
if (data[i].date === 2022 && data[i - 1] !== undefined) {
data[i].compare_at_price = Math.round(
data[i - 1].compare_at_price +
data[i - 1].compare_at_price * (averageIncreasePerYear[2022] / 100)
);
}
if (data[i].date === 2023 && data[i - 1] !== undefined) {
data[i].compare_at_price = Math.round(
data[i - 1].compare_at_price +
data[i - 1].compare_at_price * (averageIncreasePerYear[2023] / 100)
);
}
}
percentIncreaseVsMarketAverage();
};

// convert the price difference into a percentage, comparing how much higher or lower the current
// property's price is vs market trends. Above 0 is above market value, below 0 is below market
// value. So below 0 means you're getting a better deal

const percentIncreaseVsMarketAverage = () => {
for (const d of data) {
d.price_difference_percentage =
Math.round((d.price / d.compare_at_price - 1) * 100 * 10) / 10;
}
};

// Call function to populate the data
addPricesToData();

// console.log("dataLogLog", data[data.length - 1].price_difference_percentage);

return <div>{data[data.length - 1].price_difference_percentage}</div>;
};

module.exports = getPriceChangePercentage();

0 comments on commit e746288

Please sign in to comment.