mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-05 06:28:51 +00:00
Use recoil for state management
This commit is contained in:
1 parent
7a46eaf2be
commit
d8f756b309
10 files changed
+43
-29
No files matched your search
+2
-1
@@ -14,7 +14,8 @@
|
|||||||
"react-dom": "^16.13.1",
|
"react-dom": "^16.13.1",
|
||||||
"react-i18next": "^11.7.1",
|
"react-i18next": "^11.7.1",
|
||||||
"react-router-dom": "^5.2.0",
|
"react-router-dom": "^5.2.0",
|
||||||
"react-scripts": "3.4.3"
|
"react-scripts": "3.4.3",
|
||||||
|
"recoil": "^0.0.10"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "react-scripts start",
|
"start": "react-scripts start",
|
||||||
|
|||||||
@@ -1,14 +1,18 @@
|
|||||||
import React, { createContext, useEffect } from "react";
|
import React, { useEffect } from "react";
|
||||||
import { useConfig } from "../../../config/hooks/useConfig";
|
import { useConfig } from "../../../config/hooks/useConfig";
|
||||||
import { blue, grey } from "@material-ui/core/colors";
|
import { blue, grey } from "@material-ui/core/colors";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import ContactsIcon from "@material-ui/icons/Contacts";
|
import ContactsIcon from "@material-ui/icons/Contacts";
|
||||||
import SettingsIcon from '@material-ui/icons/Settings'
|
import SettingsIcon from '@material-ui/icons/Settings'
|
||||||
|
import { atom } from "recoil";
|
||||||
|
|
||||||
const AppsContext = createContext(null);
|
const appsState = atom({
|
||||||
|
key: 'apps',
|
||||||
|
default: null
|
||||||
|
});
|
||||||
|
|
||||||
export const useApps = () => {
|
export const useApps = () => {
|
||||||
const [apps, setApps] = useConfig(AppsContext);
|
const [apps, setApps] = useConfig(appsState);
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setApps("preinstalled", [
|
setApps("preinstalled", [
|
||||||
|
|||||||
@@ -22,14 +22,16 @@ export const SettingsApp = () => {
|
|||||||
const classes = useStyles();
|
const classes = useStyles();
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [config] = useConfig();
|
const [config] = useConfig();
|
||||||
const [settings, setSettings] = useSettings();
|
const [_settings, setSettings] = useSettings();
|
||||||
|
|
||||||
const wallpaperOptions = config.wallpapers.map((w) => ({
|
const wallpaperOptions = config.wallpapers.map((w) => ({
|
||||||
label: w,
|
label: w,
|
||||||
onClick: () => setSettings("wallpaper", w),
|
onClick: () => setSettings("wallpaper", w),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const [openMenu, closeMenu, ContextMenu, isMenuOpen] = useContextMenu(wallpaperOptions);
|
const [openMenu, closeMenu, ContextMenu, isMenuOpen] = useContextMenu(
|
||||||
|
wallpaperOptions
|
||||||
|
);
|
||||||
return (
|
return (
|
||||||
<AppWrapper className={classes.root}>
|
<AppWrapper className={classes.root}>
|
||||||
<AppTitle className={classes.title}>{t("APPS_SETTINGS")}</AppTitle>
|
<AppTitle className={classes.title}>{t("APPS_SETTINGS")}</AppTitle>
|
||||||
@@ -40,7 +42,7 @@ export const SettingsApp = () => {
|
|||||||
</ListItem>
|
</ListItem>
|
||||||
</List>
|
</List>
|
||||||
</AppContent>
|
</AppContent>
|
||||||
<ContextMenu handleClickAway={closeMenu} />
|
<ContextMenu />
|
||||||
</AppWrapper>
|
</AppWrapper>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -1,9 +1,12 @@
|
|||||||
import { createContext } from 'react';
|
|
||||||
import Default from '../default.json';
|
import Default from '../default.json';
|
||||||
import { useConfig } from '../../../config/hooks/useConfig';
|
import { useConfig } from '../../../config/hooks/useConfig';
|
||||||
|
import { atom } from 'recoil';
|
||||||
|
|
||||||
export const SettingsContext = createContext(Default);
|
const settingsState = atom({
|
||||||
|
key: 'settings',
|
||||||
|
default: Default
|
||||||
|
})
|
||||||
|
|
||||||
export const useSettings = () => {
|
export const useSettings = () => {
|
||||||
return useConfig(SettingsContext);
|
return useConfig(settingsState);
|
||||||
};
|
};
|
||||||
@@ -1,20 +1,22 @@
|
|||||||
import { createContext, useContext, useEffect, useState } from "react";
|
|
||||||
import _set from "lodash/set";
|
|
||||||
import Default from "../default.json";
|
import Default from "../default.json";
|
||||||
|
import { atom, useRecoilState } from "recoil";
|
||||||
|
|
||||||
export const ConfigContext = createContext(Default);
|
const configState = atom({
|
||||||
|
key: 'config',
|
||||||
|
default: Default
|
||||||
|
})
|
||||||
|
|
||||||
export const useConfig = (Context = ConfigContext) => {
|
console.log(Default)
|
||||||
const configCtx = useContext(Context);
|
|
||||||
const [config, _setConfig] = useState({ ...configCtx });
|
|
||||||
|
|
||||||
useEffect(() => {
|
export const useConfig = (state = configState) => {
|
||||||
_setConfig({ ...configCtx });
|
const [config, _setConfig] = useRecoilState(state)
|
||||||
}, [configCtx]);
|
|
||||||
|
|
||||||
const setConfig = (key, value) => {
|
const setConfig = (key, value) => {
|
||||||
const state = { ...config };
|
_setConfig(oldConfig => ({
|
||||||
_setConfig(_set(state, key, value));
|
...oldConfig,
|
||||||
|
[key]: value
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
return [config, setConfig];
|
return [config, setConfig];
|
||||||
};
|
};
|
||||||
@@ -4,11 +4,14 @@ import "./main.css";
|
|||||||
import Phone from "./Phone";
|
import Phone from "./Phone";
|
||||||
import * as serviceWorker from "./serviceWorker";
|
import * as serviceWorker from "./serviceWorker";
|
||||||
import { BrowserRouter } from "react-router-dom";
|
import { BrowserRouter } from "react-router-dom";
|
||||||
|
import { RecoilRoot } from "recoil";
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
|
<RecoilRoot>
|
||||||
<Phone />
|
<Phone />
|
||||||
|
</RecoilRoot>
|
||||||
</BrowserRouter>
|
</BrowserRouter>
|
||||||
</React.StrictMode>,
|
</React.StrictMode>,
|
||||||
document.getElementById("root")
|
document.getElementById("root")
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import {
|
|||||||
BottomNavigationAction,
|
BottomNavigationAction,
|
||||||
} from "@material-ui/core";
|
} from "@material-ui/core";
|
||||||
import { grey } from "@material-ui/core/colors";
|
import { grey } from "@material-ui/core/colors";
|
||||||
import { fade } from "@material-ui/core/styles/colorManipulator";
|
|
||||||
import KeyboardArrowLeftIcon from "@material-ui/icons/KeyboardArrowLeft";
|
import KeyboardArrowLeftIcon from "@material-ui/icons/KeyboardArrowLeft";
|
||||||
import AppsIcon from "@material-ui/icons/Apps";
|
import AppsIcon from "@material-ui/icons/Apps";
|
||||||
import { useHistory } from "react-router-dom";
|
import { useHistory } from "react-router-dom";
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { ClickAwayListener } from "@material-ui/core";
|
|
||||||
|
|
||||||
export const AppWrapper = ({ children, style, handleClickAway, ...props }) => {
|
export const AppWrapper = ({ children, style, handleClickAway, ...props }) => {
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import {
|
import {
|
||||||
Drawer,
|
|
||||||
ListItem,
|
ListItem,
|
||||||
List,
|
List,
|
||||||
ListItemIcon,
|
ListItemIcon,
|
||||||
ListItemText,
|
ListItemText,
|
||||||
Slide,
|
Slide,
|
||||||
makeStyles,
|
makeStyles,
|
||||||
ClickAwayListener,
|
|
||||||
} from "@material-ui/core";
|
} from "@material-ui/core";
|
||||||
|
|
||||||
const useStyles = makeStyles((theme) => ({
|
const useStyles = makeStyles((theme) => ({
|
||||||
@@ -15,14 +13,13 @@ const useStyles = makeStyles((theme) => ({
|
|||||||
width: "100%",
|
width: "100%",
|
||||||
backgroundColor: theme.palette.background.default,
|
backgroundColor: theme.palette.background.default,
|
||||||
color: "black",
|
color: "black",
|
||||||
zIndex: 2
|
zIndex: 2,
|
||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
export const ContextMenu = ({ open, onClose, handleClickAway, options }) => {
|
export const ContextMenu = ({ open, onClose, options }) => {
|
||||||
const classes = useStyles();
|
const classes = useStyles();
|
||||||
return (
|
return (
|
||||||
<ClickAwayListener onClickAway={handleClickAway}>
|
|
||||||
<Slide direction="up" in={open} mountOnEnter unmountOnExit>
|
<Slide direction="up" in={open} mountOnEnter unmountOnExit>
|
||||||
<div className={classes.root}>
|
<div className={classes.root}>
|
||||||
<List component="nav" aria-label="context menu">
|
<List component="nav" aria-label="context menu">
|
||||||
@@ -42,6 +39,5 @@ export const ContextMenu = ({ open, onClose, handleClickAway, options }) => {
|
|||||||
</List>
|
</List>
|
||||||
</div>
|
</div>
|
||||||
</Slide>
|
</Slide>
|
||||||
</ClickAwayListener>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -9087,6 +9087,11 @@ realpath-native@^1.1.0:
|
|||||||
dependencies:
|
dependencies:
|
||||||
util.promisify "^1.0.0"
|
util.promisify "^1.0.0"
|
||||||
|
|
||||||
|
recoil@^0.0.10:
|
||||||
|
version "0.0.10"
|
||||||
|
resolved "https://registry.yarnpkg.com/recoil/-/recoil-0.0.10.tgz#679ab22306f559f8a63c46fd5ff5241539f9248f"
|
||||||
|
integrity sha512-+9gRqehw3yKETmoZbhSnWu4GO10HDb5xYf1CjLF1oXGK2uT6GX5Lu9mfTXwjxV/jXxEKx8MIRUUbgPxvbJ8SEw==
|
||||||
|
|
||||||
[email protected]:
|
[email protected]:
|
||||||
version "2.2.2"
|
version "2.2.2"
|
||||||
resolved "https://registry.yarnpkg.com/recursive-readdir/-/recursive-readdir-2.2.2.tgz#9946fb3274e1628de6e36b2f6714953b4845094f"
|
resolved "https://registry.yarnpkg.com/recursive-readdir/-/recursive-readdir-2.2.2.tgz#9946fb3274e1628de6e36b2f6714953b4845094f"
|
||||||
|
|||||||
Reference in new issue
Block a user