Skip to content

Commit

Permalink
reset order - works!
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Aug 24, 2024
1 parent 51dd903 commit c697359
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 17 deletions.
106 changes: 97 additions & 9 deletions ui2/src/components/Viewer/ThumbnailsToggle/ThumbnailsToggle.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,133 @@
import {useContext} from "react"
import {useContext, useState} from "react"
import {useDispatch, useSelector} from "react-redux"
import {UnstyledButton, Flex} from "@mantine/core"
import {Popover, Stack, Group, Button, Tooltip} from "@mantine/core"
import {
IconLayoutSidebarRightExpand,
IconLayoutSidebarRightCollapse
} from "@tabler/icons-react"
import classes from "./ThumbnailsToggle.module.css"
import {
toggleThumbnailsPanel,
selectThumbnailsPanelOpen
selectThumbnailsPanelOpen,
selectPagesHaveChanged,
resetPageChanges
} from "@/slices/dualPanel/dualPanel"
import PanelContext from "@/contexts/PanelContext"
import {PanelMode} from "@/types"
import {RootState} from "@/app/types"

export default function ThumbnailsToggle() {
const [dontBotherMe, setDontBotherMe] = useState<boolean>(false)
const dispatch = useDispatch()
const mode: PanelMode = useContext(PanelContext)
const isOpen = useSelector((state: RootState) =>
selectThumbnailsPanelOpen(state, mode)
)
const pagesHaveChanged = useSelector((state: RootState) =>
selectPagesHaveChanged(state, mode)
)

const onClick = () => {
dispatch(toggleThumbnailsPanel(mode))
}

if (isOpen) {
const onSave = () => {
// dispatch((state: RootState) => savePageChanges(state, mode))
}

const onReset = () => {
dispatch(resetPageChanges(mode))
}

const onClosePopover = () => {
setDontBotherMe(true)
}

const toggleElement = (
<UnstyledButton onClick={() => onClick()}>
{isOpen ? (
<IconLayoutSidebarRightExpand />
) : (
<IconLayoutSidebarRightCollapse />
)}
</UnstyledButton>
)

if (pagesHaveChanged && !dontBotherMe) {
return (
<Flex align={"flex-start"} className={classes.thumbnailsToggle}>
<UnstyledButton onClick={() => onClick()}>
<IconLayoutSidebarRightExpand />
</UnstyledButton>
<Popover
opened={true}
onChange={() => {}}
position="right"
withArrow
shadow="md"
>
<Popover.Target>{toggleElement}</Popover.Target>
<Popover.Dropdown>
<PopoverBody
onSave={onSave}
onReset={onReset}
onClose={onClosePopover}
/>
</Popover.Dropdown>
</Popover>
</Flex>
)
}

return (
<Flex align={"flex-start"} className={classes.thumbnailsToggle}>
<UnstyledButton onClick={() => onClick()}>
<IconLayoutSidebarRightCollapse />
</UnstyledButton>
{toggleElement}
</Flex>
)
}

type Args = {
onSave: () => void
onReset: () => void
onClose: () => void
}

function PopoverBody({onSave, onReset, onClose}: Args) {
return (
<Stack>
Changes are not yet saved on the server
<Group>
<Tooltip
multiline
w={220}
withArrow
openDelay={1000}
label="Apply changes to the server. Once applied, document version
will increase by one"
>
<Button onClick={onSave}>Save</Button>
</Tooltip>
<Tooltip
openDelay={1000}
multiline
w={220}
withArrow
label="Discard changes. This will bring pages in their initial state (same as on server)"
>
<Button onClick={onReset} variant="default">
Reset
</Button>
</Tooltip>
<Tooltip
openDelay={1000}
multiline
w={220}
withArrow
label="Close this dialog. You can apply changes later from the context menu"
>
<Button onClick={onClose} variant="default">
Don't bother me
</Button>
</Tooltip>
</Group>
</Stack>
)
}
60 changes: 56 additions & 4 deletions ui2/src/slices/dualPanel/dualPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import {
setCurrentNodeHelper,
nodeAddedHelper,
nodeUpdatedHelper,
dropThumbnailPageHelper
dropThumbnailPageHelper,
resetPageChangesHelper,
getLatestVersionPages
} from "./helpers"

import type {
Expand Down Expand Up @@ -222,6 +224,10 @@ const dualPanelSlice = createSlice({
name: "dualPanel",
initialState,
reducers: {
resetPageChanges: (state, action: PayloadAction<PanelMode>) => {
const mode = action.payload
resetPageChangesHelper(state, mode)
},
selectionAddPage: (state, action: PayloadAction<SelectionPagePayload>) => {
const pageId = action.payload.selectionId
const mode = action.payload.mode
Expand Down Expand Up @@ -527,7 +533,8 @@ const dualPanelSlice = createSlice({
currentPage: action.meta.arg.page || 1,
thumbnailsPanelOpen: mainThumbnailsPanelInitialState(),
zoomFactor: 100,
selectedIds: []
selectedIds: [],
initialPages: getLatestVersionPages(action.payload.versions)
}
return
}
Expand All @@ -544,7 +551,8 @@ const dualPanelSlice = createSlice({
currentPage: action.meta.arg.page || 1,
thumbnailsPanelOpen: secondaryThumbnailsPanelInitialState(),
zoomFactor: 100,
selectedIds: []
selectedIds: [],
initialPages: getLatestVersionPages(action.payload.versions)
}
}
return
Expand Down Expand Up @@ -593,7 +601,8 @@ export const {
storeInboxNode,
nodeAdded,
setCurrentPage,
dropThumbnailPage
dropThumbnailPage,
resetPageChanges
} = dualPanelSlice.actions

export default dualPanelSlice.reducer
Expand Down Expand Up @@ -821,6 +830,17 @@ export const selectSelectedPageIds = (state: RootState, mode: PanelMode) => {
return state.dualPanel.secondaryPanel?.viewer?.selectedIds
}

export const selectInitialPages = (
state: RootState,
mode: PanelMode
): Array<PageType> | undefined => {
if (mode == "main") {
return state.dualPanel.mainPanel.viewer?.initialPages
}

return state.dualPanel.secondaryPanel?.viewer?.initialPages
}

export const selectPagesRaw = (
state: RootState,
mode: PanelMode
Expand Down Expand Up @@ -871,6 +891,38 @@ export const selectSelectedPages = createSelector(
}
)

export const selectPagesHaveChanged = createSelector(
[selectInitialPages, selectPagesRaw],
(
initialPages: Array<PageType> | undefined,
currentPages: Array<PageType> | undefined
): boolean => {
if (!initialPages) {
return false
}

if (!currentPages) {
return false
}

if (initialPages?.length != currentPages?.length) {
return true
}

for (let i = 0; i < (initialPages?.length || 0); i++) {
if (initialPages[i].id != currentPages[i].id) {
return true
}

if (initialPages[i].number != currentPages[i].number) {
return true
}
}

return false
}
)

export const selectSelectedNodes = createSelector(
[selectSelectedNodeIds, selectNodesRaw],
(
Expand Down
41 changes: 38 additions & 3 deletions ui2/src/slices/dualPanel/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import type {
NodeType,
PanelMode,
PageType,
DroppedThumbnailPosition
DroppedThumbnailPosition,
DocumentVersion
} from "@/types"
import {DualPanelState, NodeWithSpinner, Commander} from "./types"
import {INITIAL_PAGE_SIZE} from "@/cconstants"
Expand Down Expand Up @@ -179,7 +180,8 @@ export function setCurrentNodeHelper({
currentPage: 1,
thumbnailsPanelOpen: false,
zoomFactor: 100,
selectedIds: []
selectedIds: [],
initialPages: []
}
}
}
Expand Down Expand Up @@ -220,7 +222,8 @@ export function setCurrentNodeHelper({
currentPage: 1,
thumbnailsPanelOpen: false,
zoomFactor: 100,
selectedIds: []
selectedIds: [],
initialPages: []
},
searchResults: null
}
Expand Down Expand Up @@ -371,6 +374,28 @@ export function dropThumbnailPageHelper({
}
}

export function resetPageChangesHelper(state: DualPanelState, mode: PanelMode) {
let curVer

if (mode == "main") {
if (state.mainPanel.viewer) {
curVer = state.mainPanel.viewer?.currentVersion
if (curVer && curVer > 1) {
state.mainPanel.viewer.versions[curVer - 1].pages =
state.mainPanel.viewer.initialPages
}
}
} else {
if (state.secondaryPanel && state.secondaryPanel.viewer) {
curVer = state.secondaryPanel.viewer.currentVersion
if (curVer && curVer > 1) {
state.secondaryPanel.viewer.versions[curVer - 1].pages =
state.secondaryPanel.viewer.initialPages
}
}
}
}

function _removePanelNodes(
state: DualPanelState,
idsToRemove: string[],
Expand Down Expand Up @@ -409,3 +434,13 @@ function _removeNodes(
}
return state.nodes
}

export function getLatestVersionPages(vers: DocumentVersion[]): PageType[] {
const lastVer = vers.reduce((maxVer, v) => {
if (maxVer.number > v.number) {
return maxVer
}
return v
}, vers[0])
return lastVer.pages
}
4 changes: 3 additions & 1 deletion ui2/src/slices/dualPanel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import type {
CurrentNodeType,
PaginationType,
BreadcrumbItemType,
DocumentVersion
DocumentVersion,
PageType
} from "@/types"

export type NodeWithSpinner = {
Expand Down Expand Up @@ -61,6 +62,7 @@ export interface Viewer {
zoomFactor: number
// selected page IDs
selectedIds: Array<string>
initialPages: Array<PageType>
}

export interface SearchResults {
Expand Down

0 comments on commit c697359

Please sign in to comment.