From 897fdda1381ab1b6bd91539267ae3c745d816f80 Mon Sep 17 00:00:00 2001 From: AykutSarac Date: Fri, 8 Apr 2022 16:56:47 +0300 Subject: [PATCH] create context --- src/hocs/config.tsx | 40 +++++++++++++++++ src/reducer/reducer.ts | 99 ++++++++++++++++++++++++++++++++++++++++++ src/typings/global.ts | 12 ++--- 3 files changed, 146 insertions(+), 5 deletions(-) create mode 100644 src/hocs/config.tsx create mode 100644 src/reducer/reducer.ts diff --git a/src/hocs/config.tsx b/src/hocs/config.tsx new file mode 100644 index 0000000..8199271 --- /dev/null +++ b/src/hocs/config.tsx @@ -0,0 +1,40 @@ +import React from "react"; +import { defaultConfig, defaultJson } from "src/constants/data"; +import { ReducerAction, useConfigReducer } from "src/reducer/reducer"; +import { StorageConfig } from "src/typings/global"; + +export interface AppConfig { + json: string; + settings: StorageConfig; +} + +export const initialStates: AppConfig = { + json: JSON.stringify(defaultJson), + settings: defaultConfig, +}; + +interface Config { + states: AppConfig; + dispatch: React.Dispatch; +} + +const defaultContext: Config = { + states: initialStates, + dispatch: () => {}, +}; + +const ConfigContext: React.Context = + React.createContext(defaultContext); + +const useConfig = () => React.useContext(ConfigContext); + +const WithConfig: React.FC = ({ children }) => { + const [states, dispatch] = React.useReducer(useConfigReducer, initialStates); + const value = { states, dispatch }; + + return ( + {children} + ); +}; + +export { WithConfig, useConfig, ConfigContext }; diff --git a/src/reducer/reducer.ts b/src/reducer/reducer.ts new file mode 100644 index 0000000..693e515 --- /dev/null +++ b/src/reducer/reducer.ts @@ -0,0 +1,99 @@ +import React from "react"; +import { getNextLayout } from "src/containers/LiveEditor/helpers"; +import { AppConfig, initialStates } from "../hocs/config"; + +export enum ConfigActionType { + SET_CONFIG, + TOGGLE_LAYOUT, + TOGGLE_EXPAND, + TOGGLE_AUTOFORMAT, + TOGGLE_DOCK, + ZOOM_IN, + ZOOM_OUT, + CENTER_VIEW, + SET_JSON, +} + +export type ReducerAction = { + type: ConfigActionType; + payload?: any; +}; + +export const useConfigReducer: React.Reducer = ( + state = initialStates, + action +) => { + switch (action.type) { + case ConfigActionType.SET_CONFIG: + return { ...state, settings: action.payload }; + + case ConfigActionType.CENTER_VIEW: + return { + ...state, + settings: { ...state.settings, transform: Math.random() }, + }; + + case ConfigActionType.ZOOM_IN: + return { + ...state, + settings: { + ...state.settings, + zoomScale: state.settings.zoomScale + 0.1, + }, + }; + + case ConfigActionType.ZOOM_OUT: + return { + ...state, + settings: { + ...state.settings, + zoomScale: state.settings.zoomScale - 0.1, + }, + }; + + case ConfigActionType.TOGGLE_AUTOFORMAT: + return { + ...state, + settings: { + ...state.settings, + autoformat: !state.settings.autoformat, + }, + }; + + case ConfigActionType.TOGGLE_DOCK: + return { + ...state, + settings: { + ...state.settings, + hideEditor: !state.settings.hideEditor, + }, + }; + + case ConfigActionType.TOGGLE_EXPAND: + return { + ...state, + settings: { + ...state.settings, + expand: !state.settings.expand, + }, + }; + + case ConfigActionType.TOGGLE_LAYOUT: + return { + ...state, + settings: { + ...state.settings, + layout: getNextLayout(state.settings.layout), + }, + }; + + case ConfigActionType.SET_JSON: + return { + ...state, + json: action.payload, + }; + + default: + return state; + } +}; diff --git a/src/typings/global.ts b/src/typings/global.ts index 08e6c82..f34e696 100644 --- a/src/typings/global.ts +++ b/src/typings/global.ts @@ -1,8 +1,10 @@ import { CanvasDirection } from "reaflow"; export interface StorageConfig { - layout: CanvasDirection; - expand: boolean; - controls: boolean; - autoformat: boolean; - } \ No newline at end of file + layout: CanvasDirection; + expand: boolean; + autoformat: boolean; + hideEditor: boolean; + zoomScale: number; + transform: number; +}