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