2021-05-14 08:15:15 +00:00
import { Typography } from "@material-ui/core" ;
2019-06-19 14:40:52 +00:00
import CardSpacer from "@saleor/components/CardSpacer" ;
import Container from "@saleor/components/Container" ;
import Form from "@saleor/components/Form" ;
import Grid from "@saleor/components/Grid" ;
2021-07-21 08:59:52 +00:00
import Savebar from "@saleor/components/Savebar" ;
2020-07-07 10:14:12 +00:00
import { MenuErrorFragment } from "@saleor/fragments/types/MenuErrorFragment" ;
2020-11-02 13:26:02 +00:00
import { SubmitPromise } from "@saleor/hooks/useForm" ;
2019-08-26 14:51:47 +00:00
import { sectionNames } from "@saleor/intl" ;
2022-01-28 12:34:20 +00:00
import { ConfirmButtonTransitionState } from "@saleor/macaw-ui" ;
2021-07-21 08:59:52 +00:00
import { Backlink } from "@saleor/macaw-ui" ;
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 { maybe } from "../../../misc" ;
import { MenuDetails_menu } from "../../types/MenuDetails" ;
import { MenuItemType } from "../MenuItemDialog" ;
import MenuItems , { TreeOperation } from "../MenuItems" ;
import MenuProperties from "../MenuProperties" ;
import { computeTree } from "./tree" ;
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 [ ] ;
2019-06-19 14:40:52 +00:00
menu : MenuDetails_menu ;
onBack : ( ) = > void ;
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 ,
onBack ,
onDelete ,
onItemAdd ,
onItemClick ,
onItemEdit ,
onSubmit
} ) = > {
2019-08-26 14:51:47 +00:00
const intl = useIntl ( ) ;
2019-06-19 14:40:52 +00:00
const initialForm : MenuDetailsFormData = {
name : maybe ( ( ) = > menu . name , "" )
} ;
const [ treeOperations , setTreeOperations ] = React . useState < TreeOperation [ ] > (
[ ]
) ;
const handleSubmit = async ( data : MenuDetailsFormData ) = > {
2020-10-22 11:33:29 +00:00
const result = await onSubmit ( {
name : data.name ,
operations : treeOperations
} ) ;
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
} ;
const handleChange = ( operation : TreeOperation ) = > {
if ( ! ! operation ) {
setTreeOperations ( [ . . . treeOperations , operation ] ) ;
}
} ;
return (
< Form initial = { initialForm } onSubmit = { handleSubmit } >
{ ( { change , data , hasChanged , submit } ) = > (
< Container >
2021-07-21 08:59:52 +00:00
< Backlink onClick = { onBack } >
2019-08-26 14:51:47 +00:00
{ intl . formatMessage ( sectionNames . navigation ) }
2021-07-21 08:59:52 +00:00
< / Backlink >
2019-06-19 14:40:52 +00:00
< Grid variant = "inverted" >
< div >
2019-08-26 14:51:47 +00:00
< Typography variant = "h5" >
{ intl . formatMessage ( sectionNames . navigation ) }
< / Typography >
2019-06-19 14:40:52 +00:00
< Typography >
2019-08-26 14:51:47 +00:00
< FormattedMessage
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"
id = "menuDetailsPageHelperText"
/ >
2019-06-19 14:40:52 +00:00
< / Typography >
< / div >
< div >
< MenuProperties
data = { data }
disabled = { disabled }
2020-03-11 13:03:31 +00:00
errors = { errors }
2019-06-19 14:40:52 +00:00
onChange = { change }
/ >
< CardSpacer / >
< MenuItems
canUndo = { treeOperations . length > 0 }
items = { maybe ( ( ) = >
computeTree ( menu . items , [ . . . treeOperations ] )
) }
onChange = { handleChange }
onItemAdd = { onItemAdd }
onItemClick = { onItemClick }
onItemEdit = { onItemEdit }
onUndo = { ( ) = >
setTreeOperations (
treeOperations . slice ( 0 , treeOperations . length - 1 )
)
}
/ >
< / div >
< / Grid >
2021-07-21 08:59:52 +00:00
< Savebar
2019-06-19 14:40:52 +00:00
disabled = { disabled || ( ! hasChanged && treeOperations . length === 0 ) }
onCancel = { onBack }
onDelete = { onDelete }
2021-07-21 08:59:52 +00:00
onSubmit = { submit }
2019-06-19 14:40:52 +00:00
state = { saveButtonState }
/ >
< / Container >
) }
< / Form >
) ;
} ;
MenuDetailsPage . displayName = "MenuDetailsPage" ;
export default MenuDetailsPage ;