2023-02-20 15:21:28 +00:00
import { Content } from "@dashboard/components/AppLayout/Content" ;
import { DetailedContent } from "@dashboard/components/AppLayout/DetailedContent" ;
2023-01-16 09:45:12 +00:00
import { Backlink } from "@dashboard/components/Backlink" ;
import CardSpacer from "@dashboard/components/CardSpacer" ;
import Form from "@dashboard/components/Form" ;
import Grid from "@dashboard/components/Grid" ;
import Savebar from "@dashboard/components/Savebar" ;
import { MenuDetailsFragment , MenuErrorFragment } from "@dashboard/graphql" ;
import { SubmitPromise } from "@dashboard/hooks/useForm" ;
import useNavigator from "@dashboard/hooks/useNavigator" ;
import { sectionNames } from "@dashboard/intl" ;
import { menuListUrl } from "@dashboard/navigation/urls" ;
2021-05-14 08:15:15 +00:00
import { Typography } from "@material-ui/core" ;
2022-05-06 08:59:55 +00:00
import { ConfirmButtonTransitionState } from "@saleor/macaw-ui" ;
2023-02-20 15:21:28 +00:00
import { Box } from "@saleor/macaw-ui/next" ;
2020-05-14 09:30:32 +00:00
import React from "react" ;
import { FormattedMessage , useIntl } from "react-intl" ;
2019-06-19 14:40:52 +00:00
import { MenuItemType } from "../MenuItemDialog" ;
import MenuItems , { TreeOperation } from "../MenuItems" ;
import MenuProperties from "../MenuProperties" ;
2022-02-22 09:29:18 +00:00
import { computeRelativeTree } from "./tree" ;
2019-06-19 14:40:52 +00:00
export interface MenuDetailsFormData {
name : string ;
}
export interface MenuDetailsSubmitData extends MenuDetailsFormData {
operations : TreeOperation [ ] ;
}
export interface MenuDetailsPageProps {
saveButtonState : ConfirmButtonTransitionState ;
disabled : boolean ;
2020-03-11 13:03:31 +00:00
errors : MenuErrorFragment [ ] ;
2022-03-09 08:56:55 +00:00
menu : MenuDetailsFragment ;
2019-06-19 14:40:52 +00:00
onDelete : ( ) = > void ;
onItemAdd : ( ) = > void ;
onItemClick : ( id : string , type : MenuItemType ) = > void ;
onItemEdit : ( id : string ) = > void ;
2020-11-02 13:26:02 +00:00
onSubmit : ( data : MenuDetailsSubmitData ) = > SubmitPromise ;
2019-06-19 14:40:52 +00:00
}
2019-11-07 11:34:54 +00:00
const MenuDetailsPage : React.FC < MenuDetailsPageProps > = ( {
2019-06-19 14:40:52 +00:00
disabled ,
2020-03-11 13:03:31 +00:00
errors ,
2019-06-19 14:40:52 +00:00
menu ,
saveButtonState ,
onDelete ,
onItemAdd ,
onItemClick ,
onItemEdit ,
2022-06-21 09:36:55 +00:00
onSubmit ,
2019-06-19 14:40:52 +00:00
} ) = > {
2019-08-26 14:51:47 +00:00
const intl = useIntl ( ) ;
2022-05-06 08:59:55 +00:00
const navigate = useNavigator ( ) ;
2019-08-26 14:51:47 +00:00
2019-06-19 14:40:52 +00:00
const initialForm : MenuDetailsFormData = {
2022-06-21 09:36:55 +00:00
name : menu?.name ? ? "" ,
2019-06-19 14:40:52 +00:00
} ;
const [ treeOperations , setTreeOperations ] = React . useState < TreeOperation [ ] > (
2022-06-21 09:36:55 +00:00
[ ] ,
2019-06-19 14:40:52 +00:00
) ;
2022-02-22 09:29:18 +00:00
const removeSimulatedMoves = ( operations : TreeOperation [ ] ) = >
operations . filter ( operation = > ! operation . simulatedMove ) ;
2019-06-19 14:40:52 +00:00
const handleSubmit = async ( data : MenuDetailsFormData ) = > {
2020-10-22 11:33:29 +00:00
const result = await onSubmit ( {
name : data.name ,
2022-06-21 09:36:55 +00:00
operations : removeSimulatedMoves ( treeOperations ) ,
2020-10-22 11:33:29 +00:00
} ) ;
if ( result ) {
2019-06-19 14:40:52 +00:00
setTreeOperations ( [ ] ) ;
}
2020-10-22 11:33:29 +00:00
return result ;
2019-06-19 14:40:52 +00:00
} ;
2022-02-22 09:29:18 +00:00
const handleChange = ( operations : TreeOperation [ ] ) = > {
setTreeOperations ( [ . . . treeOperations , . . . operations ] ) ;
2019-06-19 14:40:52 +00:00
} ;
return (
2022-02-01 09:58:06 +00:00
< Form confirmLeave initial = { initialForm } onSubmit = { handleSubmit } >
2022-05-05 07:54:28 +00:00
{ ( { change , data , submit } ) = > (
2023-02-20 15:21:28 +00:00
< DetailedContent >
< Content >
< Box padding = { 9 } margin = "auto" height = "100vh" >
< Backlink href = { menuListUrl ( ) } >
2019-08-26 14:51:47 +00:00
{ intl . formatMessage ( sectionNames . navigation ) }
2023-02-20 15:21:28 +00:00
< / Backlink >
< Grid variant = "inverted" >
< div >
< Typography variant = "h5" >
{ intl . formatMessage ( sectionNames . navigation ) }
< / Typography >
< Typography >
< FormattedMessage
id = "E54eoT"
defaultMessage = "Creating the navigation structure is done by dragging and dropping. Simply create a new menu item and then drag it into its destined place. You can move items inside one another to create a tree structure and drag items up and down to create a hierarchy"
/ >
< / Typography >
< / div >
< div >
< MenuProperties
data = { data }
disabled = { disabled }
errors = { errors }
onChange = { change }
/ >
< CardSpacer / >
< MenuItems
canUndo = { treeOperations . length > 0 }
items = {
menu ? . items
? computeRelativeTree ( menu . items , treeOperations )
: [ ]
}
onChange = { handleChange }
onItemAdd = { onItemAdd }
onItemClick = { onItemClick }
onItemEdit = { onItemEdit }
onUndo = { ( ) = >
setTreeOperations ( operations = > {
if ( operations . length > 1 ) {
// Undo of a simulated move needs removal of 2 moves instead of one
if ( operations [ operations . length - 2 ] . simulatedMove ) {
return operations . slice ( 0 , operations . length - 2 ) ;
}
}
return operations . slice ( 0 , operations . length - 1 ) ;
} )
2022-02-22 09:29:18 +00:00
}
2023-02-20 15:21:28 +00:00
/ >
< / div >
< / Grid >
< Savebar
onCancel = { ( ) = > navigate ( menuListUrl ( ) ) }
disabled = { disabled || treeOperations . length === 0 }
onDelete = { onDelete }
onSubmit = { submit }
state = { saveButtonState }
2019-06-19 14:40:52 +00:00
/ >
2023-02-20 15:21:28 +00:00
< / Box >
< / Content >
< / DetailedContent >
2019-06-19 14:40:52 +00:00
) }
< / Form >
) ;
} ;
MenuDetailsPage . displayName = "MenuDetailsPage" ;
export default MenuDetailsPage ;