problem with rev and lastLogin info changing triggering state updates

This commit is contained in:
lingdocs 2021-09-03 14:29:30 +04:00
parent aec5e53e13
commit e537eaedf5
2 changed files with 19 additions and 5 deletions

View File

@ -73,7 +73,7 @@ import "./App.css";
import classNames from "classnames";
import { getTextOptions } from "./lib/get-text-options";
import { getTextFromShareTarget } from "./lib/share-target";
import { objIsEqual } from "./lib/misc-helpers";
import { objIsEqual, userObjIsEqual } from "./lib/misc-helpers";
// to allow Moustrap key combos even when input fields are in focus
Mousetrap.prototype.stopCallback = function () {
@ -145,7 +145,6 @@ class App extends Component<RouteComponentProps, State> {
ReactGA.pageview(window.location.pathname + window.location.search);
}
dictionary.initialize().then((r) => {
console.log(this.props.location.pathname);
this.checkUserCronJob.start();
this.networkCronJob.start();
this.setState({
@ -282,7 +281,10 @@ class App extends Component<RouteComponentProps, State> {
if (userOnServer === "offline") return;
if (userOnServer) sendSubmissions();
if (!userOnServer) {
this.setState({ user: undefined });
if (this.state.user) {
console.log("setting state user because user is newly undefined");
this.setState({ user: undefined });
}
saveUser(undefined);
return;
}
@ -291,7 +293,8 @@ class App extends Component<RouteComponentProps, State> {
...userOnServer,
userTextOptionsRecord,
};
if (!objIsEqual(prevUser, user)) {
if (!userObjIsEqual(prevUser, user)) {
console.log("setting state user because something is different about the user")
this.setState({ user });
saveUser(user);
}
@ -409,7 +412,7 @@ class App extends Component<RouteComponentProps, State> {
}
}
private checkUserCronJob = new CronJob("1/3 * * * * *", () => {
private checkUserCronJob = new CronJob("1/20 * * * * *", () => {
this.handleLoadUser();
})

View File

@ -1,4 +1,15 @@
import * as AT from "../types/account-types";
export function objIsEqual(obj1: any, obj2: any): boolean {
if (!obj1 || !obj2) return false;
return JSON.stringify(obj1) === JSON.stringify(obj2);
}
export function userObjIsEqual(u1: AT.LingdocsUser | undefined, u2: AT.LingdocsUser | undefined): boolean {
if (!u1 || !u2) return false;
function removeFrills(u: AT.LingdocsUser) {
const { lastActive, _rev, ...rest } = u;
return rest;
}
return objIsEqual(removeFrills(u1), removeFrills(u2));
}