From 2e3f57677ef123ef64741a513ef6aef1332a88d9 Mon Sep 17 00:00:00 2001 From: amr Date: Tue, 20 Dec 2022 07:05:31 +0200 Subject: [PATCH] Created a GridLayout enum and updated all locations to use it. (#208) So, that we can more easily refactor the code later --- src/components/MangaCard.tsx | 14 +++++------ src/components/MangaGrid.tsx | 3 ++- .../context/LibraryOptionsContext.tsx | 10 ++++++-- .../library/LibraryOptionsPanel.tsx | 8 +++---- src/components/source/GridLayouts.tsx | 23 +++++++++++-------- src/typings.d.ts | 10 ++++++-- 6 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/components/MangaCard.tsx b/src/components/MangaCard.tsx index 63296a64..a640bc75 100644 --- a/src/components/MangaCard.tsx +++ b/src/components/MangaCard.tsx @@ -14,7 +14,7 @@ import { Avatar, CardContent, Grid } from '@mui/material'; import useLocalStorage from 'util/useLocalStorage'; import SpinnerImage from 'components/util/SpinnerImage'; import { Box, styled } from '@mui/system'; -import { useLibraryOptionsContext } from 'components/context/LibraryOptionsContext'; +import { GridLayout, useLibraryOptionsContext } from 'components/context/LibraryOptionsContext'; import { BACK } from 'util/useBackTo'; const BottomGradient = styled('div')({ @@ -66,7 +66,7 @@ const truncateText = (str: string, maxLength: number) => { interface IProps { manga: IMangaCard - gridLayout: number | undefined + gridLayout?: GridLayout dimensions: number inLibraryIndicator?: boolean } @@ -88,11 +88,11 @@ const MangaCard = React.forwardRef((props: IProps, ref) const mangaLinkTo = { pathname: `/manga/${id}/`, state: { backLink: BACK } }; - if (gridLayout !== 2) { + if (gridLayout !== GridLayout.List) { const cols = Math.ceil(dimensions / ItemWidth); return ( - + ((props: IProps, ref) placeItems: 'center', }} /> - {(gridLayout === 1) ? (<>) : ( + {(gridLayout === GridLayout.Comfortable) ? (<>) : ( <> )} - {(gridLayout === 1) ? ( + {(gridLayout === GridLayout.Comfortable) ? ( <> ) : ( ((props: IProps, ref) )} - {(gridLayout === 1) ? ( + {(gridLayout === GridLayout.Comfortable) ? ( void - gridLayout?: number | undefined + gridLayout?: GridLayout horisontal?: boolean | undefined noFaces?: boolean | undefined inLibraryIndicator?: boolean diff --git a/src/components/context/LibraryOptionsContext.tsx b/src/components/context/LibraryOptionsContext.tsx index 869cc5f5..e8c4da5d 100644 --- a/src/components/context/LibraryOptionsContext.tsx +++ b/src/components/context/LibraryOptionsContext.tsx @@ -12,11 +12,17 @@ type ContextType = { setOptions: React.Dispatch>; }; +export enum GridLayout { + Compact = 0, + Comfortable = 1, + List = 2, +} + export const DefaultLibraryOptions: LibraryOptions = { showDownloadBadge: false, showUnreadBadge: false, - gridLayout: 0, - SourcegridLayout: 0, + gridLayout: GridLayout.Compact, + SourcegridLayout: GridLayout.Compact, downloaded: undefined, sortDesc: undefined, diff --git a/src/components/library/LibraryOptionsPanel.tsx b/src/components/library/LibraryOptionsPanel.tsx index 87663d93..9eb03003 100644 --- a/src/components/library/LibraryOptionsPanel.tsx +++ b/src/components/library/LibraryOptionsPanel.tsx @@ -10,7 +10,7 @@ import CheckboxInput from 'components/atoms/CheckboxInput'; import RadioInput from 'components/atoms/RadioInput'; import SortRadioInput from 'components/atoms/SortRadioInput'; import ThreeStateCheckboxInput from 'components/atoms/ThreeStateCheckboxInput'; -import { useLibraryOptionsContext } from 'components/context/LibraryOptionsContext'; +import { GridLayout, useLibraryOptionsContext } from 'components/context/LibraryOptionsContext'; import OptionsTabs from 'components/molecules/OptionsTabs'; import React from 'react'; @@ -78,9 +78,9 @@ const LibraryOptionsPanel: React.FC = ({ open, onClose }) => { onChange={(e) => handleFilterChange('gridLayout', Number(e.target.value))} value={gridLayout} > - - - + + + Badges diff --git a/src/components/source/GridLayouts.tsx b/src/components/source/GridLayouts.tsx index 1debbd84..2500f49f 100644 --- a/src/components/source/GridLayouts.tsx +++ b/src/components/source/GridLayouts.tsx @@ -10,10 +10,11 @@ import { } from '@mui/material'; import React from 'react'; import ViewModuleIcon from '@mui/icons-material/ViewModule'; -import { useLibraryOptionsContext } from 'components/context/LibraryOptionsContext'; +import { GridLayout, useLibraryOptionsContext } from 'components/context/LibraryOptionsContext'; +// TODO: clean up this to use a FormControl, and remove dependency on name o radio button export default function SourceGridLayout() { - const { options, setOptions } = useLibraryOptionsContext(); + const { options: { SourcegridLayout }, setOptions } = useLibraryOptionsContext(); const [anchorEl, setAnchorEl] = React.useState(null); const open = Boolean(anchorEl); @@ -55,11 +56,14 @@ export default function SourceGridLayout() { )} @@ -70,8 +74,8 @@ export default function SourceGridLayout() { label="Comfortable grid" control={( )} @@ -82,15 +86,14 @@ export default function SourceGridLayout() { label="List" control={( )} /> - ); } diff --git a/src/typings.d.ts b/src/typings.d.ts index 04e96ea2..2fcc9fe0 100644 --- a/src/typings.d.ts +++ b/src/typings.d.ts @@ -280,12 +280,18 @@ type ChapterOptionsReducerAction = type LibrarySortMode = 'sortToRead' | 'sortAlph' | 'sortID'; +enum GridLayout { + Compact = 0, + Comfortable = 1, + List = 2, +} + interface LibraryOptions { // display options showDownloadBadge: boolean showUnreadBadge: boolean - gridLayout: number - SourcegridLayout:number + gridLayout: GridLayout + SourcegridLayout: GridLayout // filter options downloaded: NullAndUndefined