1
0
Fork 0

Merge pull request #2317 from eriese/eriese-rollback

Roll Back some changes to `useGist`
This commit is contained in:
Joost De Cock 2022-06-29 13:39:23 +02:00 committed by GitHub
commit f59aea1ef4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 28 deletions

View file

@ -9,6 +9,7 @@ const CoreSettingSaBool = props => {
const toggle = () => {
props.setGist({
...props.gist,
saBool: !value,
sa: value ? 0 : props.gist.saMm
})

View file

@ -15,6 +15,7 @@ const CoreSettingMm = props => {
setValue(newVal)
if (props.gist.sa) props.setGist({
...props.gist,
saMm: newVal,
sa: newVal,
})

View file

@ -1,7 +1,7 @@
import MenuIcon from 'shared/components/icons/menu.js'
import { linkClasses, Chevron } from 'shared/components/navigation/primary.js'
import { useTranslation } from 'next-i18next'
import defaultSettings from '../default-settings'
import {defaultGist} from 'shared/hooks/useGist'
const View = props => {
const { t } = useTranslation(['app'])
@ -61,7 +61,7 @@ const View = props => {
{
name: 'clear',
title: t('clearThing', { thing: 'YAML' }),
onClick: () => props.clearGist()
onClick: () => props.setGist(defaultGist(props.design, props.gist.locale))
},
]

View file

@ -47,7 +47,7 @@ const hasRequiredMeasurementsMethod = (design, gist) => {
const WorkbenchWrapper = ({ app, design, preload=false, from=false, layout=false }) => {
// State for gist
const {gist, setGist, unsetGist, clearGist, updateGist, gistReady} = useGist(design, app);
const {gist, setGist, unsetGist, updateGist, gistReady} = useGist(design, app);
const [messages, setMessages] = useState([])
const [popup, setPopup] = useState(false)
@ -67,11 +67,11 @@ const WorkbenchWrapper = ({ app, design, preload=false, from=false, layout=false
const doPreload = async () => {
if (preload && from && preloaders[from]) {
const g = await preloaders[from](preload, design)
setGist({...g.settings})
setGist({...gist, ...g.settings})
}
}
doPreload();
}, [preload, from])
}, [preload, from, gist])
// Helper methods to manage the gist state
const updateWBGist = useMemo(() => (path, value, closeNav=false) => {
@ -114,7 +114,6 @@ const WorkbenchWrapper = ({ app, design, preload=false, from=false, layout=false
updateGist: updateWBGist,
unsetGist,
setGist,
clearGist,
draft,
feedback,
gistReady,

View file

@ -4,7 +4,7 @@ import unset from 'lodash.unset'
import defaultSettings from 'shared/components/workbench/default-settings.js'
// Generates a default design gist to start from
const defaultGist = (design, locale='en') => {
export const defaultGist = (design, locale='en') => {
const gist = {
design: design.config.name,
version: design.config.version,
@ -19,18 +19,11 @@ const defaultGist = (design, locale='en') => {
// generate the gist state and its handlers
export function useGist(design, app) {
// get the localstorage state and setter
const [gist, _setGist, gistReady] = useLocalStorage(`${design.config.name}_gist`, defaultGist(design, app.locale));
/** apply new gist values onto existing gist */
const setGist = (newGist) => {
// setters that are passed a function always use the latest state value, so these don't need to be memoized
_setGist((gistState) => ({...gistState, ...newGist}))
}
const [gist, setGist, gistReady] = useLocalStorage(`${design.config.name}_gist`, defaultGist(design, app.locale));
/** update a single gist value */
const updateGist = (path, value) => {
_setGist((gistState) => {
setGist((gistState) => {
const newGist = {...gistState};
set(newGist, path, value);
return newGist;
@ -39,22 +32,12 @@ export function useGist(design, app) {
/** unset a single gist value */
const unsetGist = (path) => {
_setGist((gistState) => {
setGist((gistState) => {
const newGist = {... gistState};
unset(newGist, path);
return newGist;
})
}
/** replace the entire gist with the given gist */
const replaceGist = (newGist) => {
_setGist(newGist);
}
/** reset to the default gist */
const clearGist = () => {
replaceGist(defaultGist(design, gist.locale))
}
return {gist, setGist, unsetGist, replaceGist, clearGist, gistReady, updateGist};
return {gist, setGist, unsetGist, gistReady, updateGist};
}