diff --git a/src/engine/lib/services/LocalStorageService.ts b/src/engine/lib/services/LocalStorageService.ts index 167521c5..0d324df4 100644 --- a/src/engine/lib/services/LocalStorageService.ts +++ b/src/engine/lib/services/LocalStorageService.ts @@ -1,5 +1,6 @@ export class MRILocalStorageService { RECENT_FILES_KEY: string = 'recentFiles'; + VIEW_MODE_KEY: string = 'viewMode'; constructor() { this.saveRecentFiles = this.saveRecentFiles.bind(this); @@ -22,6 +23,14 @@ export class MRILocalStorageService { this.localStorage.setItem(key, JSON.stringify(data)); } + saveViewMode(viewMode: string): void { + this.setData(this.VIEW_MODE_KEY, viewMode); + } + + getViewMode(): string { + return this.getData(this.VIEW_MODE_KEY) || ''; + } + saveRecentFiles(fileName: string): void { const recentFiles = this.getData(this.RECENT_FILES_KEY); const limitedRecentFiles = recentFiles.slice(0, 2); diff --git a/src/ui/LeftToolbar/ModeSwitcherToolbar.jsx b/src/ui/LeftToolbar/ModeSwitcherToolbar.jsx index e1cb92bb..6b200a31 100644 --- a/src/ui/LeftToolbar/ModeSwitcherToolbar.jsx +++ b/src/ui/LeftToolbar/ModeSwitcherToolbar.jsx @@ -2,7 +2,7 @@ * Copyright 2022 EPAM Systems, Inc. (https://www.epam.com/) * SPDX-License-Identifier: Apache-2.0 */ -import React from 'react'; +import React, { useEffect } from 'react'; import { Tooltip } from '../Tooltip/Tooltip'; import { UIButton } from '../Button/Button'; import { Container } from '../Layout/Container'; @@ -10,6 +10,7 @@ import { useDispatch, useSelector } from 'react-redux'; import StoreActionType from '../../store/ActionTypes'; import ViewMode from '../../store/ViewMode'; import { useNeedShow3d } from '../../utils/useNeedShow3d'; +import { mriLocalStorageService } from '../../engine/lib/services'; export function ModeSwitcherToolbar() { const dispatch = useDispatch(); @@ -19,8 +20,16 @@ export function ModeSwitcherToolbar() { const setMode = (indexMode) => { dispatch({ type: StoreActionType.SET_MODE_VIEW, viewMode: indexMode }); + mriLocalStorageService.saveViewMode(indexMode); }; + useEffect(() => { + const savedMode = mriLocalStorageService.getViewMode(); + if (savedMode) { + setMode(savedMode); + } + }, []); + const set2dMode = () => { setMode(ViewMode.VIEW_2D); }; diff --git a/src/ui/LeftToolbar/ModeSwitcherToolbar.test.jsx b/src/ui/LeftToolbar/ModeSwitcherToolbar.test.jsx index 260ceec1..465d8c02 100644 --- a/src/ui/LeftToolbar/ModeSwitcherToolbar.test.jsx +++ b/src/ui/LeftToolbar/ModeSwitcherToolbar.test.jsx @@ -4,8 +4,16 @@ import { fireEvent, screen } from '@testing-library/react'; import { ModeSwitcherToolbar } from './ModeSwitcherToolbar'; import ViewMode from '../../store/ViewMode'; import { useNeedShow3d } from '../../utils/useNeedShow3d'; +import { mriLocalStorageService } from '../../engine/lib/services'; jest.mock('../../utils/useNeedShow3d'); +jest.mock('../../engine/lib/services/LocalStorageService', () => { + return { + getViewMode: jest.fn(), + saveViewMode: jest.fn(), + }; +}); + const mockedUseNeedShow3d = useNeedShow3d; describe('ModeSwitcherToolbarTest', () => { @@ -27,10 +35,12 @@ describe('ModeSwitcherToolbarTest', () => { it('test button 3D', () => { mockedUseNeedShow3d.mockReturnValue(true); + mriLocalStorageService.getViewMode.mockReturnValue(ViewMode.VIEW_2D); const { store } = renderWithState(, { viewMode: ViewMode.VIEW_2D }); expect(store.getState().viewMode).toBe(ViewMode.VIEW_2D); fireEvent.click(screen.getByTestId('Button3D')); expect(store.getState().viewMode).toBe(ViewMode.VIEW_3D); + expect(mriLocalStorageService.saveViewMode).toHaveBeenCalledWith(ViewMode.VIEW_3D); }); it('should be render button3D', () => {