Skip to content

Commit 0dbf758

Browse files
committed
pullstate migration
1 parent 8c789b9 commit 0dbf758

File tree

8 files changed

+86
-163
lines changed

8 files changed

+86
-163
lines changed

frontend/pages/houses/[uuid].tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import _ from "lodash";
44
// prettier-ignore
55
import {justifyBetween, intersperse, Spacer, mt, absolute, alignCenter, bg, border, br, column, fullWidth, height, p, px, py, row, s, weightBold, width, noResize, justifyCenter, fg, fontSize, clickable, justifyStart, pl, pr, size, weightSemiBold, weightRegular, minWidth, mr, maxWidth, selfCenter, selfStretch, opacity, mb, light1, flexGrow, grow, pageHeight, dark4, dark3, dark5, light2, s6, full, f2, f3, caps, light3, light4, light5, flexWrap, dark2, s4, s3, hsl, f0, f1, selfEnd, alignEnd, alignStretch, keyedProp, inline, s5, alignStart, dark1, shadow, dark0, flexible, textAlign, s7, s8, s9, s10, selfStart, constrainWidth, center, light0, textOverflowClip, oneLine, stiff } from "src/styles";
66
import { Investment, FieldFormat, FinancingOption } from "src/models";
7-
import { useSelector, useDispatch } from "react-redux";
87
import { useRouter } from "next/router";
98
import {
109
isSSR,
@@ -21,6 +20,7 @@ import { AppAction } from "src/redux/reducer";
2120
import { card, editableStyles, plColor, primaryColor } from "src/app_styles";
2221
import { Selector } from "src/Selector";
2322
import { useWindowSize } from "rooks";
23+
import { AppStore } from "src/store";
2424

2525
const SliderGroup = ({ firstSlider, secondSlider, mobile }) => {
2626
if (mobile) {
@@ -44,8 +44,7 @@ const SliderGroup = ({ firstSlider, secondSlider, mobile }) => {
4444
export default function HousePage(props: {}): ReactElement {
4545
const router = useRouter();
4646
const uuid = router.query.uuid as string;
47-
const dispatch = useDispatch();
48-
const house = useSelector((state) =>
47+
const house = AppStore.useState((state) =>
4948
_.find(state.houses, (house: Investment) => {
5049
return house.uuid === uuid;
5150
})
@@ -61,9 +60,10 @@ export default function HousePage(props: {}): ReactElement {
6160
const projection = createInvestmentProjection(house);
6261

6362
const updateHouse = (house: Investment) => {
64-
dispatch({
65-
type: AppAction.UpdateInvestment,
66-
house: house,
63+
AppStore.update((s) => {
64+
s.houses = _.map(s.houses, (h: Investment) => {
65+
return h.uuid === house.uuid ? house : h;
66+
});
6767
});
6868
};
6969
const labelColor = light4;

frontend/pages/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import {
1313
formatUSDShorten,
1414
} from "src/utilities";
1515
import { useWindowSize } from "rooks";
16+
import { AppStore } from "src/store";
1617

1718
export default function Home() {
18-
const investments = useSelector((state: AppState) => state.houses);
19+
const investments = AppStore.useState((state) => state.houses);
1920
console.log("investments:", investments);
2021
const labelColor = light4;
2122
const { innerWidth } = useWindowSize();

frontend/src/AppContainer.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { f1, f2, intersperse, mt, absolute, alignCenter, bg, border, br, column,
44
import { useRouter } from "next/router";
55
import Link from "next/link";
66
import { useWindowSize } from "rooks";
7-
import { useSelector, useDispatch } from "react-redux";
87
import { useModal } from "react-modal-hook";
98
import { SignInForm } from "src/SignInForm";
109
import { AppState } from "src/redux/reducer";
1110
import _ from "lodash";
1211
import { HouseUploadService } from "src/HouseUploadService";
12+
import { AppStore } from "src/store";
1313

1414
const navItems = [
1515
{
@@ -26,11 +26,10 @@ export const AppContainer: FC<{}> = (props): ReactElement => {
2626
const router = useRouter();
2727
const { innerWidth } = useWindowSize();
2828
const mobile = innerWidth < 1000;
29-
const dispatch = useDispatch();
3029
const [openSignIn, closeSignIn] = useModal(() => {
3130
return <SignInForm onClose={closeSignIn} />;
3231
});
33-
const user = useSelector((state: AppState) => state.user);
32+
const user = AppStore.useState((s) => s.user);
3433
const loggedIn = !_.isNil(user);
3534
return (
3635
<div style={s(column, pageHeight)}>

frontend/src/HouseUploadService.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,26 @@ import React, {
88
import { inputStyles, primaryColor, secondaryColor } from "src/app_styles";
99
import { AppAction, AppState } from "src/redux/reducer";
1010
import superagent from "superagent";
11-
import { useDispatch, useSelector } from "react-redux";
1211
import { useRequestAuth } from "src/useAuthenticatedRequester";
1312
import _ from "lodash";
1413
import { useInterval, usePrevious } from "rooks";
14+
import { AppStore } from "src/store";
1515

1616
export const HouseUploadService = ({}) => {
1717
const requestAuth = useRequestAuth();
18-
const user = useSelector((state: AppState) => state.user);
19-
const changedHouseUuids = useSelector((state: AppState) => state.changed);
20-
const houses = useSelector((state: AppState) => state.houses);
21-
const dispatch = useDispatch();
22-
console.log("changedHouseUuids:", changedHouseUuids);
18+
const user = AppStore.useState((s) => s.user);
19+
const changedHouseUuids = AppStore.useState((s) => s.changed);
20+
const houses = AppStore.useState((s) => s.houses);
2321
const changedHouses = _.filter(houses, (house) => {
2422
return changedHouseUuids.has(house.uuid);
2523
});
26-
// const previousHouses = usePrevious(latestHouses);
27-
// const [changedHouses, setChangedHouses] = useRef(null);
2824
useInterval(
2925
() => {
3026
if (!_.isEmpty(changedHouses)) {
3127
_.forEach(changedHouses, (house) => {
32-
dispatch({
33-
type: AppAction.InvestmentUploaded,
34-
uuid: house.uuid,
28+
// TODO: delete after successful
29+
AppStore.update((s) => {
30+
s.changed.delete(house.uuid);
3531
});
3632
requestAuth(superagent.post("/api/investments").send(house)).end(
3733
(err, res) => {

frontend/src/SignInForm.tsx

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AppAction } from "src/redux/reducer";
55
// prettier-ignore
66
import {justifyEnd, justifyBetween, intersperse, Spacer, mt, absolute, alignCenter, bg, border, br, column, fullWidth, height, p, px, py, row, s, weightBold, width, noResize, justifyCenter, fg, fontSize, clickable, justifyStart, pl, pr, size, weightSemiBold, weightRegular, minWidth, mr, maxWidth, selfCenter, selfStretch, opacity, mb, light1, flexGrow, grow, pageHeight, dark4, dark3, dark5, light2, s6, full, f2, f3, caps, light3, light4, light5, flexWrap, dark2, s4, s3, hsl, f0, f1, selfEnd, alignEnd, alignStretch, keyedProp, inline, s5, alignStart, dark1, shadow, dark0, flexible, textAlign, s7, s8, s9, s10, selfStart, constrainWidth, center, light0, textOverflowClip, oneLine, stiff, modalContainer } from "src/styles";
77
import superagent from "superagent";
8-
import { useDispatch } from "react-redux";
8+
import { AppStore } from "src/store";
99

1010
export const SignInForm = ({ onClose }) => {
1111
const ref = useRef(null);
@@ -23,7 +23,6 @@ export const SignInForm = ({ onClose }) => {
2323
);
2424
const [email, setEmail] = useState();
2525
const [password, setPassword] = useState();
26-
const dispatch = useDispatch();
2726
return (
2827
<div
2928
style={s(modalContainer)}
@@ -65,12 +64,9 @@ export const SignInForm = ({ onClose }) => {
6564
.send({ email, password })
6665
.end((err, res) => {
6766
console.log("res:", res);
68-
dispatch({
69-
type: AppAction.Login,
70-
user: {
71-
email,
72-
},
73-
token: res.body.token,
67+
AppStore.update((s) => {
68+
s.jwt = res.body.token;
69+
s.user = { email };
7470
});
7571
});
7672
}}
@@ -85,12 +81,9 @@ export const SignInForm = ({ onClose }) => {
8581
.post("/api/login")
8682
.send({ email, password })
8783
.end((err, res) => {
88-
dispatch({
89-
type: AppAction.Login,
90-
user: {
91-
email,
92-
},
93-
token: res.body.token,
84+
AppStore.update((s) => {
85+
s.jwt = res.body.token;
86+
s.user = { email };
9487
});
9588
});
9689
}}

frontend/src/redux/reducer.ts

Lines changed: 0 additions & 127 deletions
This file was deleted.

frontend/src/store.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,65 @@
11
import { Store } from "pullstate";
2+
import { FinancingOption, Investment } from "src/models";
23

3-
export const AppStore = new Store({
4+
export const AppStore = new Store<{
5+
houses: Investment[];
6+
signInModalOpen: boolean;
7+
user: {
8+
email: string;
9+
};
10+
jwt: string;
11+
changed: Set<string>;
12+
deleted: Set<string>;
13+
}>({
414
changed: new Set(),
15+
deleted: new Set(),
16+
user: null,
17+
jwt: null,
18+
houses: [
19+
{
20+
title: "9572 Gonzalez Dr",
21+
purchasePrice: 80000,
22+
downPayment: 0.2,
23+
interestRate: 0.045,
24+
rent: 1150,
25+
annualTaxes: 2400,
26+
insurance: 200,
27+
expenseRatio: 0.15,
28+
financingOption: FinancingOption.Mortgage,
29+
propManagement: 0.1,
30+
hoa: 0,
31+
miscExpenses: 0,
32+
uuid: "7354a4fa-d5fd-4369-b54d-340a86b582ef",
33+
},
34+
{
35+
title: "9576 Gonzalez Dr",
36+
purchasePrice: 70000,
37+
downPayment: 0.2,
38+
interestRate: 0.045,
39+
rent: 975,
40+
annualTaxes: 2400,
41+
insurance: 200,
42+
expenseRatio: 0.15,
43+
financingOption: FinancingOption.Mortgage,
44+
propManagement: 0.1,
45+
hoa: 0,
46+
miscExpenses: 0,
47+
uuid: "e2c5ea65-d16a-4a27-ae6d-c6516e6d0591",
48+
},
49+
{
50+
title: "42 Wallaby Way",
51+
purchasePrice: 1000000,
52+
downPayment: 0.2,
53+
interestRate: 0.045,
54+
rent: 2150,
55+
annualTaxes: 4800,
56+
insurance: 400,
57+
expenseRatio: 0.15,
58+
financingOption: FinancingOption.Mortgage,
59+
propManagement: 0.1,
60+
hoa: 0,
61+
miscExpenses: 0,
62+
uuid: "144588a0-1047-4d05-b1ea-af5c1de54309",
63+
},
64+
],
565
});

server/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct Claims {
3333
}
3434

3535
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
36+
#[serde(rename_all = "camelCase")]
3637
struct Investment {
3738
purchase_price: u32,
3839
uuid: String,

0 commit comments

Comments
 (0)