Skip to content

Commit

Permalink
HomePageContext
Browse files Browse the repository at this point in the history
  • Loading branch information
nuuuwan committed Aug 1, 2024
1 parent f15f5db commit bc1bd9e
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .eslintcache

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/nonview/base/DICTIONARY.english.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ Will support bringing back 19the amendment
Will support bringing back the 19th Amendment
Will work with the IMF
Won a Electoral District Seat in the 2020 General Election
de Silva
de Silva
2 changes: 1 addition & 1 deletion src/nonview/constants/VERSION.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Auto-generated by javascript_version.py
export const DATETIME_STR = "2024-08-01 16:05:00";
export const DATETIME_STR = "2024-08-01 16:13:35";
export const VERSION_DATETIME = new Date(DATETIME_STR);

const VERSION = { DATETIME_STR, VERSION_DATETIME };
Expand Down
53 changes: 53 additions & 0 deletions src/nonview/core/HomePageContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { URLContext, I18N } from "../../nonview/base";
import GroundTruth from "../../nonview/core/GroundTruth";

export default class HomePageContext {
constructor(lang, page, version, criterionWeights) {
console.debug({ lang, page, version, criterionWeights });
this.lang = lang;
this.page = page;
this.version = version;
this.criterionWeights = criterionWeights;
}

toURL() {
I18N.setLang(this.lang);
URLContext.setContext({
lang: this.lang,
page: this.page,
version: this.version,
criterionWeightsJSON: JSON.stringify(this.criterionWeights),
});
}

// loaders

static DEFAULT = {
LANG: I18N.BASE_LANG,
PAGE: "CriteriaPage",
VERSION: GroundTruth.DEFAULT_VERSION,
CRITERION_WEIGHTS: GroundTruth.getInitCriterionWeights(
GroundTruth.DEFAULT_VERSION
),
};

static fromURL() {
const context = URLContext.getContext();
return new HomePageContext(
context.lang || HomePageContext.DEFAULT.LANG,
context.page || HomePageContext.DEFAULT.PAGE,
context.version || HomePageContext.DEFAULT.VERSION,
context.criterionWeightsJSON
? JSON.parse(context.criterionWeightsJSON)
: HomePageContext.DEFAULT.CRITERION_WEIGHTS
);
}

// state
static updateState(homePage, updater) {
let { context } = homePage.state;
context = updater(context);
context.toURL();
homePage.setState({ context });
}
}
11 changes: 10 additions & 1 deletion src/nonview/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@
import Candidate from "./Candidate";
import Criterion from "./Criterion";
import GroundTruth from "./GroundTruth";
import HomePageContext from "./HomePageContext";
import Party from "./Party";
import Version from "./Version";
import Weight from "./Weight";

export { Candidate, Criterion, GroundTruth, Party, Version, Weight };
export {
Candidate,
Criterion,
GroundTruth,
HomePageContext,
Party,
Version,
Weight,
};
114 changes: 36 additions & 78 deletions src/view/pages/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ import React, { createRef } from "react";
import Box from "@mui/material/Box";

import AudioX from "../../nonview/base/AudioX";
import I18N from "../../nonview/base/I18N";
import URLContext from "../../nonview/base/URLContext";

import CustomAppBar from "../../view/molecules/CustomAppBar";
import HomePageBottomNavigation from "../../view/molecules/HomePageBottomNavigation";

import GroundTruth from "../../nonview/core/GroundTruth";
import PAGE_CONFIG_LIST, {
DEFAULT_PAGE_CONFIG,
} from "../../view/pages/PAGE_CONFIG_LIST";
import PAGE_CONFIG_LIST from "../../view/pages/PAGE_CONFIG_LIST";
import HomePageContext from "../../nonview/core/HomePageContext";

const STYLE_INNER_PAGE_BOX = {
marginTop: 10,
Expand All @@ -22,56 +19,25 @@ const STYLE_INNER_PAGE_BOX = {
export default class HomePage extends Component {
constructor(props) {
super(props);
const context = this.getContext();
const context = HomePageContext.fromURL();
this.state = {
context,
};
this.isComponentMounted = false;
this.setContext(context);
}

getContext() {
let context = URLContext.getContext();
if (!context.page) {
context.page = "CriteriaPage";
}
if (!context.lang) {
context.lang = I18N.BASE_LANG;
}
if (!context.version) {
context.version = GroundTruth.DEFAULT_VERSION;
}

if (!context.criterionWeightsJSON) {
const criterionWeights = GroundTruth.getInitCriterionWeights(
context.version
);
context.criterionWeightsJSON = JSON.stringify(criterionWeights);
}
return context;
context.toURL();
}

componentDidMount() {
this.isComponentMounted = true;
}

setContext(newContext) {
const oldContext = this.getContext();
const context = { ...oldContext, ...newContext };

URLContext.setContext(context);
I18N.setLang(context.lang);

if (this.isComponentMounted) {
this.setState({ context });
}
}

onClickOpenPage(page) {
AudioX.playShort();
let context = URLContext.getContext();
context.page = page;
this.setContext(context);

HomePageContext.updateState(this, function (context) {
context.page = page;
});

window.scrollTo(0, 0);
}

Expand All @@ -80,66 +46,58 @@ export default class HomePage extends Component {

for (let config of PAGE_CONFIG_LIST) {
if (config.page === context.page) {
context.page = config.page;
URLContext.setContext(context);
return config;
}
}

context.page = DEFAULT_PAGE_CONFIG.page;
URLContext.setContext(context);
return DEFAULT_PAGE_CONFIG;
throw new Error("Invalid page: " + context.page);
}

onChangeCriterionWeight(iCriterion, criterionWeight) {
let context = this.getContext();
let criterionWeights = JSON.parse(context.criterionWeightsJSON);
criterionWeights[iCriterion] = criterionWeight;
context.criterionWeightsJSON = JSON.stringify(criterionWeights);
this.setContext(context);
HomePageContext.updateState(this, function (context) {
context.criterionWeights[iCriterion] = criterionWeight;
});
}

onChangeVersion(version) {
let context = this.getContext();
context.version = version;

context.criterionWeightsJSON = JSON.stringify(
GroundTruth.getInitCriterionWeights(context.version)
);
context.page = "CriteriaPage";
this.setContext(context);
HomePageContext.updateState(this, function (context) {
context.version = version;
context.criterionWeights = GroundTruth.getInitCriterionWeights(
context.version
);
});
}

onChangeLang(lang) {
let context = this.getContext();
context.lang = lang;

this.setContext(context);
HomePageContext.updateState(this, function (context) {
context.lang = lang;
});
}

onClickRandomCriteriaWeights() {
AudioX.playLong();
let context = this.getContext();
context.criterionWeightsJSON = JSON.stringify(
GroundTruth.getRandomCriterionWeights(context.version)
);
this.setContext(context);

HomePageContext.updateState(this, function (context) {
context.criterionWeights = GroundTruth.getRandomCriterionWeights(
context.version
);
});
}

onClickRefreshCriteriaWeights() {
AudioX.playLong();
let context = this.getContext();
context.criterionWeightsJSON = JSON.stringify(
GroundTruth.getInitCriterionWeights(context.version)
);
this.setContext(context);

HomePageContext.updateState(this, function (context) {
context.criterionWeights = GroundTruth.getInitCriterionWeights(
context.version
);
});
}

renderHeader() {
const { context } = this.state;

const innerPageConfig = this.getInnerPageConfig();
const criterionWeights = JSON.parse(context.criterionWeightsJSON);
const criterionWeights = context.criterionWeights;

return (
<CustomAppBar
Expand All @@ -158,7 +116,7 @@ export default class HomePage extends Component {
renderBody() {
const { context } = this.state;
const innerPageConfig = this.getInnerPageConfig();
const criterionWeights = JSON.parse(context.criterionWeightsJSON);
const criterionWeights = context.criterionWeights;
const refHomePage = createRef(null);

return (
Expand Down

0 comments on commit bc1bd9e

Please sign in to comment.