Skip to content

Commit

Permalink
fix(settings): don't update remote with initial data
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaschiang committed Mar 10, 2021
1 parent f11b2e3 commit 0c4439a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
4 changes: 2 additions & 2 deletions components/settings/general.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, FormEvent } from 'react';
import { FormEvent, useCallback } from 'react';
import { TextField } from '@rmwc/textfield';
import useTranslation from 'next-translate/useTranslation';

Expand All @@ -7,8 +7,8 @@ import PhotoInput from 'components/photo-input';
import { Org } from 'lib/model/org';
import useSocialProps from 'lib/hooks/social-props';

import { useSettings } from './context';
import styles from './settings.module.scss';
import { useSettings } from './context';

export default function General(): JSX.Element {
const { t } = useTranslation();
Expand Down
17 changes: 8 additions & 9 deletions components/settings/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactNode, useCallback, useEffect } from 'react';
import { ReactNode, useCallback, useMemo } from 'react';
import { Snackbar, SnackbarAction } from '@rmwc/snackbar';
import Link from 'next/link';
import axios from 'axios';
Expand All @@ -20,8 +20,6 @@ export interface SettingsProps {
children: ReactNode;
}

const emptyOrg = new Org();

export default function Settings({
orgId,
active,
Expand All @@ -42,6 +40,10 @@ export default function Settings({
return Org.fromJSON(data);
}, []);

const initialData = useMemo(
() => orgs.find((o) => o.id === orgId) || new Org(),
[orgId, orgs]
);
const {
data: org,
setData: setOrg,
Expand All @@ -50,12 +52,9 @@ export default function Settings({
error,
retry,
timeout,
} = useContinuous(emptyOrg, updateRemote, updateLocal);
} = useContinuous(initialData, updateRemote, updateLocal);

useEffect(() => {
const idx = orgs.findIndex((o: Org) => o.id === orgId);
if (idx >= 0) setOrg(orgs[idx]);
}, [orgs, orgId, setOrg]);
const settingsContextValue = useMemo(() => ({ org, setOrg }), [org, setOrg]);

return (
<>
Expand Down Expand Up @@ -99,7 +98,7 @@ export default function Settings({
</div>
</div>
<div className={styles.right}>
<SettingsContext.Provider value={{ org, setOrg }}>
<SettingsContext.Provider value={settingsContextValue}>
{children}
</SettingsContext.Provider>
</div>
Expand Down
4 changes: 2 additions & 2 deletions components/settings/signup.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useCallback, FormEvent } from 'react';
import { FormEvent, useCallback } from 'react';
import { TextField } from '@rmwc/textfield';
import useTranslation from 'next-translate/useTranslation';

import { Aspect } from 'lib/model/aspect';
import { Org } from 'lib/model/org';

import { useSettings } from './context';
import styles from './settings.module.scss';
import { useSettings } from './context';

export default function Signup(): JSX.Element {
const { t, lang: locale } = useTranslation();
Expand Down
4 changes: 2 additions & 2 deletions lib/hooks/social-props.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback, FormEvent } from 'react';
import { TextFieldProps, TextFieldHTMLProps } from '@rmwc/textfield';
import { FormEvent, useCallback } from 'react';
import { TextFieldHTMLProps, TextFieldProps } from '@rmwc/textfield';
import useTranslation from 'next-translate/useTranslation';

import { Account, SocialInterface, SocialTypeAlias } from 'lib/model/account';
Expand Down

0 comments on commit 0c4439a

Please sign in to comment.