feat(localstorage): resolved issue with phone not re-rendering

This commit is contained in:
Christopher committed 2021-01-04 20:33:53 +01:00
1 parent 14223c9800
commit dddddc01f8
5 files changed
+19 -6

No files matched your search

@@ -6,6 +6,7 @@ import {
ListItemIcon, ListItemIcon,
} from '@material-ui/core'; } from '@material-ui/core';
import { SvgIconProps } from '@material-ui/core'; import { SvgIconProps } from '@material-ui/core';
interface IProps { interface IProps {
options?: any; options?: any;
label: string; label: string;
@@ -35,15 +35,9 @@ const SubHeaderComp = (props: { text: string }) => (
export const SettingsApp = () => { export const SettingsApp = () => {
const settingsApp = useApp('SETTINGS'); const settingsApp = useApp('SETTINGS');
const [config] = useConfig(); const [config] = useConfig();
const { setSettings, settings } = useSettings();
const [localVal, setVal, getStorageItem] = useLocalStorage(); const [localVal, setVal, getStorageItem] = useLocalStorage();
const simcard = useSimcard(); const simcard = useSimcard();
useEffect(() => {
console.log('new localstorage');
// this will be used to trigger a alert that gives feedback when the a setting is changed
}, [localVal]);
const wallpapers = config.wallpapers.map( const wallpapers = config.wallpapers.map(
MapStringOptions(getStorageItem('wallpaper'), (val: string) => MapStringOptions(getStorageItem('wallpaper'), (val: string) =>
setVal('wallpaper', val) setVal('wallpaper', val)
@@ -77,6 +71,10 @@ export const SettingsApp = () => {
// TODO: These new settings all work // TODO: These new settings all work
useEffect(() => {
// this will be used to trigger a alert that gives feedback when the a setting is changed
}, [setVal]);
const [openMenu, closeMenu, ContextMenu, isMenuOpen] = useContextMenu(); const [openMenu, closeMenu, ContextMenu, isMenuOpen] = useContextMenu();
return ( return (
<AppWrapper> <AppWrapper>
+7
View File
@@ -0,0 +1,7 @@
import { useRecoilState } from 'recoil';
import { phoneState } from './state';
export const useForceUpdate = () => {
const [update, setUpdate] = useRecoilState(phoneState.forceUpdateState);
return { update, setUpdate };
};
+4
View File
@@ -9,4 +9,8 @@ export const phoneState = {
key: 'phoneConfig', key: 'phoneConfig',
default: undefined, default: undefined,
}), }),
forceUpdateState: atom({
key: 'fuckingupdate',
default: 0,
}),
}; };
@@ -1,4 +1,5 @@
import { useState } from 'react'; import { useState } from 'react';
import { useForceUpdate } from '../../../os/phone/hooks/forceUpdate';
// TODO: Integrate debug log to fetch when that branch is merged // TODO: Integrate debug log to fetch when that branch is merged
@@ -10,6 +11,7 @@ import { useState } from 'react';
*/ */
function useLocalStorage<T>(key?: string, initialValue?: T) { function useLocalStorage<T>(key?: string, initialValue?: T) {
const { update, setUpdate } = useForceUpdate();
// Serves as a nice identifying prefix for the key // Serves as a nice identifying prefix for the key
const UNIQ_PREFIX = 'npwd_phone_'; const UNIQ_PREFIX = 'npwd_phone_';
const [localVal, setLocalVal] = useState<T>(() => { const [localVal, setLocalVal] = useState<T>(() => {
@@ -42,6 +44,7 @@ function useLocalStorage<T>(key?: string, initialValue?: T) {
const setVal = (localKey, value: T | ((val: T) => T)) => { const setVal = (localKey, value: T | ((val: T) => T)) => {
console.log(localKey, value); console.log(localKey, value);
try { try {
setUpdate(update + 1);
// Same API as useState meaning we can accept a function for value // Same API as useState meaning we can accept a function for value
const valForStore = value instanceof Function ? value(localVal) : value; const valForStore = value instanceof Function ? value(localVal) : value;
// Save that state so we repli useState // Save that state so we repli useState