Add customizer reset button and have two modes of customizer vs not

depending of if the customizer is open.
This commit is contained in:
Kurt Hutten
2021-09-26 08:20:18 +10:00
parent ce3d04f12c
commit fc88a6e87a
3 changed files with 31 additions and 14 deletions

View File

@@ -22,11 +22,12 @@ const Customizer = () => {
const handleRender = useRender() const handleRender = useRender()
const toggleOpen = () => { const toggleOpen = () => {
const newOpenState = !isOpen const newOpenState = !isOpen
thunkDispatch({type: 'setCustomizerOpenState', payload: newOpenState}) thunkDispatch({ type: 'setCustomizerOpenState', payload: newOpenState })
if(!newOpenState) { setTimeout(() => handleRender())
// render on close }
setTimeout(() => handleRender()) const handleReset = () => {
} thunkDispatch({ type: 'resetCustomizer' })
setTimeout(() => handleRender(true))
} }
const updateCustomizerParam = (paramName: string, paramValue: any) => { const updateCustomizerParam = (paramName: string, paramValue: any) => {
@@ -74,11 +75,17 @@ const Customizer = () => {
} inline-block w-4 h-4 transform bg-white rounded-full`} } inline-block w-4 h-4 transform bg-white rounded-full`}
/> />
</Switch> </Switch>
<button
className="px-4 py-1 rounded bg-ch-gray-300 text-ch-gray-600 mr-2"
onClick={handleReset}
>
Reset
</button>
<button <button
className={`px-4 py-1 rounded bg-ch-gray-300 text-ch-gray-800 ${ className={`px-4 py-1 rounded bg-ch-gray-300 text-ch-gray-800 ${
shouldLiveUpdate && 'bg-opacity-30 cursor-default' shouldLiveUpdate && 'bg-opacity-30 cursor-default'
}`} }`}
onClick={handleRender} onClick={() => handleRender()}
disabled={shouldLiveUpdate} disabled={shouldLiveUpdate}
> >
Update Update
@@ -87,7 +94,9 @@ const Customizer = () => {
</> </>
)} )}
</div> </div>
<div className={`${isOpen ? 'h-full pb-32' : 'h-0'} overflow-y-auto px-12`}> <div
className={`${isOpen ? 'h-full pb-32' : 'h-0'} overflow-y-auto px-12`}
>
<div> <div>
{customizerParams.map((param, index) => { {customizerParams.map((param, index) => {
const otherProps = { const otherProps = {

View File

@@ -3,7 +3,7 @@ import { useIdeContext } from 'src/helpers/hooks/useIdeContext'
export const useRender = () => { export const useRender = () => {
const { state, thunkDispatch } = useIdeContext() const { state, thunkDispatch } = useIdeContext()
return () => { return (disableParams = false) => {
thunkDispatch((dispatch, getState) => { thunkDispatch((dispatch, getState) => {
const state = getState() const state = getState()
dispatch({ type: 'setLoading' }) dispatch({ type: 'setLoading' })
@@ -13,7 +13,7 @@ export const useRender = () => {
code: state.code, code: state.code,
viewerSize: state.viewerSize, viewerSize: state.viewerSize,
camera: state.camera, camera: state.camera,
parameters: state.currentParameters, parameters: disableParams ? {} : state.currentParameters,
}) })
}) })
localStorage.setItem(makeCodeStoreKey(state.ideType), state.code) localStorage.setItem(makeCodeStoreKey(state.ideType), state.code)

View File

@@ -108,16 +108,24 @@ const reducer = (state: State, { type, payload }): State => {
} }
case 'updateCode': case 'updateCode':
return { ...state, code: payload } return { ...state, code: payload }
case 'resetCustomizer':
const resetParameters = {}
state.customizerParams.forEach(({ name, initial }) => {
resetParameters[name] = initial
})
return {
...state,
currentParameters: resetParameters,
}
case 'healthyRender': case 'healthyRender':
const currentParameters = {} const currentParameters = {}
const customizerParams: CadhubParams[] = payload.customizerParams || [] const customizerParams: CadhubParams[] = payload.customizerParams || []
customizerParams.forEach((param) => { customizerParams.forEach((param) => {
currentParameters[param.name] = currentParameters[param.name] =
typeof state?.currentParameters?.[param.name] === 'undefined' || !state.isCustomizerOpen typeof state?.currentParameters?.[param.name] === 'undefined'
? param.initial ? param.initial
: state?.currentParameters?.[param.name] : state?.currentParameters?.[param.name]
}) })
return { return {
...state, ...state,
@@ -150,7 +158,7 @@ const reducer = (state: State, { type, payload }): State => {
case 'setCustomizerOpenState': case 'setCustomizerOpenState':
return { return {
...state, ...state,
isCustomizerOpen: payload isCustomizerOpen: payload,
} }
case 'setLayout': case 'setLayout':
return { return {