Skip to content

Commit

Permalink
fix: enable page movement for non dynamic pages
Browse files Browse the repository at this point in the history
  • Loading branch information
schettn authored Sep 12, 2021
1 parent c912186 commit 26c83d3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
11 changes: 6 additions & 5 deletions packages/jaen-pages/src/containers/ui/tabs/PagesTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,17 @@ const PagesTab: React.FC<{}> = () => {
dispatch(actions.deletePage(id))
updateRouting(id)
}
const handlePageMove = (pageId: string, parentPageId: string | null) => {
const handlePageMove = (sourceId: string, destinationId: string | null) => {
dispatch(
actions.movePage({
pageId,
parentPageId
sourceId,
destinationId,
nodes: allSitePage.nodes
})
)

updateRouting(pageId)
handleNavigate(pageId)
updateRouting(sourceId)
handleNavigate(sourceId)
}

const handleItemImageClick = (pageId: string) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/jaen-pages/src/store/actions/siteActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
FieldUpdateDetails,
PageType,
ReturnResolveDynamicPaths,
SitePages,
SiteType
} from '@src/types'

Expand All @@ -29,8 +30,9 @@ export const addPage = createAction<AddPageActionPayload>('site/addPage')
export const deletePage = createAction<string>('site/deletePage')

export type MovePageActionPayload = {
pageId: string
parentPageId: string | null
sourceId: string
destinationId: string | null
nodes: SitePages['nodes']
}
export const movePage = createAction<MovePageActionPayload>('site/movePage')

Expand Down
47 changes: 19 additions & 28 deletions packages/jaen-pages/src/store/reducers/siteReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,66 +77,57 @@ const siteReducer = createReducer(initialState, {
state,
action: PayloadAction<actions.MovePageActionPayload>
) => {
const {pageId, parentPageId} = action.payload
const {sourceId, destinationId, nodes} = action.payload

const oldParent = state.allSitePage?.nodes?.[pageId]?.parent?.id as
| string
| null
const oldDestinationId = nodes[sourceId].parent?.id

if (oldParent === parentPageId) {
if (oldDestinationId === destinationId) {
return
}

state.allSitePage = {
...state.allSitePage,
nodes: {
...state.allSitePage?.nodes,
[pageId]: {
...state.allSitePage?.nodes?.[pageId],
parent: parentPageId
[sourceId]: {
...state.allSitePage?.nodes?.[sourceId],
parent: destinationId
? {
...state.allSitePage?.nodes?.[pageId]?.parent,
id: parentPageId
...state.allSitePage?.nodes?.[sourceId]?.parent,
id: destinationId
}
: null
}
}
}

if (parentPageId) {
if (destinationId) {
state.allSitePage = {
...state.allSitePage,
nodes: {
...state.allSitePage?.nodes,
[parentPageId]: {
...state.allSitePage?.nodes?.[parentPageId],
children: (
state.allSitePage?.nodes?.[parentPageId]?.children || []
).concat([{id: pageId}])
[destinationId]: {
...state.allSitePage?.nodes?.[destinationId],
children: (nodes[destinationId].children || []).concat([
{id: sourceId}
])
}
}
}
// const parentChildren = state.allSitePage?.nodes?.[parentPageId]?.children
// if (parentChildren) {
// const index = parentChildren.findIndex(child => child?.id === pageId)
// if (index > -1) {
// parentChildren.splice(index, 1)
// }
// }
}

//remove pageId node from oldParent node children newAllSitePage
if (oldParent) {
const children = state.allSitePage?.nodes?.[oldParent]?.children
if (oldDestinationId) {
const children = nodes[oldDestinationId].children

if (children) {
state.allSitePage = {
...state.allSitePage,
nodes: {
...state.allSitePage?.nodes,
[oldParent]: {
...state.allSitePage?.nodes?.[oldParent],
children: children.filter(child => child?.id !== pageId)
[oldDestinationId]: {
...state.allSitePage?.nodes?.[oldDestinationId],
children: children.filter(child => child?.id !== sourceId)
}
}
}
Expand Down

0 comments on commit 26c83d3

Please sign in to comment.