Skip to content

Commit

Permalink
Add the editor type selection in localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
vikram-raj committed Nov 27, 2024
1 parent 98e447c commit dcf83bb
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 143 deletions.
5 changes: 4 additions & 1 deletion src/components/pipeline-builder/SyncedEditorField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { EditorType } from './types';
import { safeJSToYAML, safeYAMLToJS } from './yaml';

import './SyncedEditorField.scss';
import { LOCAL_STORAGE_KEY_EDITOR_TYPE } from './const';

type FormErrorCallback<ReturnValue = {}> = () => ReturnValue;
type WithOrWithoutPromise<Type> = Promise<Type> | Type;
Expand Down Expand Up @@ -43,7 +44,8 @@ const SyncedEditorField: React.FC<SyncedEditorFieldProps> = ({
}) => {
const { t } = useTranslation('plugin__pipelines-console-plugin');
const [editorType, setEditorType] = React.useState<EditorType>(
EditorType.Form,
(localStorage.getItem(LOCAL_STORAGE_KEY_EDITOR_TYPE) as EditorType) ||
EditorType.Form,
);
const [field] = useField(name);

Expand All @@ -61,6 +63,7 @@ const SyncedEditorField: React.FC<SyncedEditorFieldProps> = ({

const changeEditorType = (newType: EditorType) => {
setEditorType(newType);
localStorage.setItem(LOCAL_STORAGE_KEY_EDITOR_TYPE, newType);
setFieldValue(name, newType);
};

Expand Down
3 changes: 3 additions & 0 deletions src/components/pipeline-builder/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,6 @@ export const initialPipelineFormData: PipelineBuilderFormValues = {
finallyListTasks: [],
loadingTasks: [],
};

export const LOCAL_STORAGE_KEY_EDITOR_TYPE =
'pipeline-console-plugin-editorType';

This file was deleted.

5 changes: 4 additions & 1 deletion src/components/pipeline-builder/synced-editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useTranslation } from 'react-i18next';
import { EditorType, EditorToggle } from './editor-toggle';
import { K8sResourceKind } from '@openshift-console/dynamic-plugin-sdk';
import { asyncYAMLToJS, safeJSToYAML } from '../yaml';
import { LOCAL_STORAGE_KEY_EDITOR_TYPE } from '../const';

const YAML_KEY_ORDER = ['apiVerion', 'kind', 'metadata', 'spec', 'status'];
export const YAML_TO_JS_OPTIONS = {
Expand Down Expand Up @@ -47,7 +48,8 @@ export const SyncedEditor: React.FC<SyncedEditorProps> = ({
const [switchError, setSwitchError] = React.useState<string | undefined>();
const [yamlWarning, setYAMLWarning] = React.useState<boolean>(false);
const [editorType, setEditorType] = React.useState<EditorType>(
EditorType.Form,
(localStorage.getItem(LOCAL_STORAGE_KEY_EDITOR_TYPE) as EditorType) ||
EditorType.Form,
);

const handleFormDataChange = (newFormData: K8sResourceKind = {}) => {
Expand All @@ -71,6 +73,7 @@ export const SyncedEditor: React.FC<SyncedEditorProps> = ({

const changeEditorType = (newType: EditorType): void => {
setEditorType(newType);
localStorage.setItem(LOCAL_STORAGE_KEY_EDITOR_TYPE, newType);
onChangeEditorType(newType);
};

Expand Down

0 comments on commit dcf83bb

Please sign in to comment.