2022-01-28 12:34:20 +00:00
import { DialogContentText } from "@material-ui/core" ;
2019-12-17 17:13:56 +00:00
import ActionDialog from "@saleor/components/ActionDialog" ;
2022-03-09 08:56:55 +00:00
import {
usePageBulkPublishMutation ,
usePageBulkRemoveMutation ,
usePageListQuery
} from "@saleor/graphql" ;
2019-12-17 17:13:56 +00:00
import useBulkActions from "@saleor/hooks/useBulkActions" ;
import useListSettings from "@saleor/hooks/useListSettings" ;
import useNavigator from "@saleor/hooks/useNavigator" ;
import useNotifier from "@saleor/hooks/useNotifier" ;
2021-09-28 12:24:45 +00:00
import { usePaginationReset } from "@saleor/hooks/usePaginationReset" ;
2019-12-17 17:13:56 +00:00
import usePaginator , {
createPaginationState
} from "@saleor/hooks/usePaginator" ;
2022-01-28 12:34:20 +00:00
import { Button , DeleteIcon , IconButton } from "@saleor/macaw-ui" ;
2019-12-17 17:13:56 +00:00
import { maybe } from "@saleor/misc" ;
import { ListViews } from "@saleor/types" ;
2019-12-06 14:58:28 +00:00
import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers" ;
2020-05-14 09:30:32 +00:00
import createSortHandler from "@saleor/utils/handlers/sortHandler" ;
2021-05-10 15:25:54 +00:00
import { mapEdgesToItems } from "@saleor/utils/maps" ;
2020-05-14 09:30:32 +00:00
import { getSortParams } from "@saleor/utils/sort" ;
import React from "react" ;
import { FormattedMessage , useIntl } from "react-intl" ;
2019-12-17 17:13:56 +00:00
import PageListPage from "../../components/PageListPage/PageListPage" ;
import {
pageCreateUrl ,
pageListUrl ,
PageListUrlDialog ,
PageListUrlQueryParams ,
pageUrl
} from "../../urls" ;
2022-03-22 08:53:31 +00:00
import { getFilterVariables , getSortQueryVariables } from "./sort" ;
2019-12-17 17:13:56 +00:00
interface PageListProps {
params : PageListUrlQueryParams ;
}
export const PageList : React.FC < PageListProps > = ( { params } ) = > {
const navigate = useNavigator ( ) ;
const notify = useNotifier ( ) ;
const paginate = usePaginator ( ) ;
const { isSelected , listElements , reset , toggle , toggleAll } = useBulkActions (
params . ids
) ;
const { updateListSettings , settings } = useListSettings (
ListViews . PAGES_LIST
) ;
2021-09-28 12:24:45 +00:00
2021-10-25 15:29:27 +00:00
usePaginationReset ( pageListUrl , params , settings . rowNumber ) ;
2021-09-28 12:24:45 +00:00
2019-12-17 17:13:56 +00:00
const intl = useIntl ( ) ;
const paginationState = createPaginationState ( settings . rowNumber , params ) ;
const queryVariables = React . useMemo (
( ) = > ( {
. . . paginationState ,
2022-03-22 08:53:31 +00:00
filter : getFilterVariables ( params ) ,
2019-12-17 17:13:56 +00:00
sort : getSortQueryVariables ( params )
} ) ,
2021-08-23 16:38:50 +00:00
[ params , settings . rowNumber ]
2019-12-17 17:13:56 +00:00
) ;
const { data , loading , refetch } = usePageListQuery ( {
displayLoader : true ,
variables : queryVariables
} ) ;
const { loadNextPage , loadPreviousPage , pageInfo } = paginate (
maybe ( ( ) = > data . pages . pageInfo ) ,
paginationState ,
params
) ;
2019-12-06 14:58:28 +00:00
const [ openModal , closeModal ] = createDialogActionHandlers <
PageListUrlDialog ,
PageListUrlQueryParams
> ( navigate , pageListUrl , params ) ;
2019-12-17 17:13:56 +00:00
2022-03-09 08:56:55 +00:00
const [ bulkPageRemove , bulkPageRemoveOpts ] = usePageBulkRemoveMutation ( {
onCompleted : data = > {
if ( data . pageBulkDelete . errors . length === 0 ) {
closeModal ( ) ;
notify ( {
status : "success" ,
text : intl.formatMessage ( {
defaultMessage : "Removed pages" ,
description : "notification"
} )
} ) ;
reset ( ) ;
refetch ( ) ;
}
2019-12-17 17:13:56 +00:00
}
2022-03-09 08:56:55 +00:00
} ) ;
2019-12-17 17:13:56 +00:00
2022-03-09 08:56:55 +00:00
const [ bulkPagePublish , bulkPagePublishOpts ] = usePageBulkPublishMutation ( {
onCompleted : data = > {
if ( data . pageBulkPublish . errors . length === 0 ) {
closeModal ( ) ;
notify ( {
status : "success" ,
text : intl.formatMessage ( {
defaultMessage : "Published pages" ,
description : "notification"
} )
} ) ;
reset ( ) ;
refetch ( ) ;
}
2019-12-17 17:13:56 +00:00
}
2022-03-09 08:56:55 +00:00
} ) ;
2019-12-17 17:13:56 +00:00
const handleSort = createSortHandler ( navigate , pageListUrl , params ) ;
return (
2022-03-09 08:56:55 +00:00
< >
< PageListPage
disabled = { loading }
settings = { settings }
pages = { mapEdgesToItems ( data ? . pages ) }
pageInfo = { pageInfo }
onAdd = { ( ) = > navigate ( pageCreateUrl ( ) ) }
onNextPage = { loadNextPage }
onPreviousPage = { loadPreviousPage }
onUpdateListSettings = { updateListSettings }
onRowClick = { id = > ( ) = > navigate ( pageUrl ( id ) ) }
onSort = { handleSort }
2022-03-22 08:53:31 +00:00
actionDialogOpts = { {
open : openModal ,
close : closeModal
} }
params = { params }
2022-03-09 08:56:55 +00:00
toolbar = {
< >
< Button
onClick = { ( ) = >
openModal ( "unpublish" , {
ids : listElements
} )
}
>
< FormattedMessage
defaultMessage = "Unpublish"
description = "unpublish page, button"
/ >
< / Button >
< Button
onClick = { ( ) = >
openModal ( "publish" , {
ids : listElements
} )
}
>
< FormattedMessage
defaultMessage = "Publish"
description = "publish page, button"
2019-12-17 17:13:56 +00:00
/ >
2022-03-09 08:56:55 +00:00
< / Button >
< IconButton
variant = "secondary"
color = "primary"
onClick = { ( ) = >
openModal ( "remove" , {
ids : listElements
} )
}
>
< DeleteIcon / >
< / IconButton >
< / >
}
isChecked = { isSelected }
selected = { listElements . length }
sort = { getSortParams ( params ) }
toggle = { toggle }
toggleAll = { toggleAll }
/ >
< ActionDialog
open = { params . action === "publish" }
onClose = { closeModal }
confirmButtonState = { bulkPagePublishOpts . status }
onConfirm = { ( ) = >
bulkPagePublish ( {
variables : {
ids : params.ids ,
isPublished : true
}
} )
}
title = { intl . formatMessage ( {
defaultMessage : "Publish Pages" ,
description : "dialog header"
} ) }
>
< DialogContentText >
< FormattedMessage
defaultMessage = "{counter,plural,one{Are you sure you want to publish this page?} other{Are you sure you want to publish {displayQuantity} pages?}}"
description = "dialog content"
values = { {
counter : maybe ( ( ) = > params . ids . length ) ,
displayQuantity : < strong > { maybe ( ( ) = > params . ids . length ) } < / strong >
} }
/ >
< / DialogContentText >
< / ActionDialog >
< ActionDialog
open = { params . action === "unpublish" }
onClose = { closeModal }
confirmButtonState = { bulkPagePublishOpts . status }
onConfirm = { ( ) = >
bulkPagePublish ( {
variables : {
ids : params.ids ,
isPublished : false
}
} )
}
title = { intl . formatMessage ( {
defaultMessage : "Unpublish Pages" ,
description : "dialog header"
} ) }
>
< FormattedMessage
defaultMessage = "{counter,plural,one{Are you sure you want to unpublish this page?} other{Are you sure you want to unpublish {displayQuantity} pages?}}"
description = "dialog content"
values = { {
counter : maybe ( ( ) = > params . ids . length ) ,
displayQuantity : < strong > { maybe ( ( ) = > params . ids . length ) } < / strong >
} }
/ >
< / ActionDialog >
< ActionDialog
open = { params . action === "remove" }
onClose = { closeModal }
confirmButtonState = { bulkPageRemoveOpts . status }
onConfirm = { ( ) = >
bulkPageRemove ( {
variables : {
ids : params.ids
}
} )
}
variant = "delete"
title = { intl . formatMessage ( {
defaultMessage : "Delete Pages" ,
description : "dialog header"
} ) }
>
< FormattedMessage
defaultMessage = "{counter,plural,one{Are you sure you want to delete this page?} other{Are you sure you want to delete {displayQuantity} pages?}}"
description = "dialog content"
values = { {
counter : maybe ( ( ) = > params . ids . length ) ,
displayQuantity : < strong > { maybe ( ( ) = > params . ids . length ) } < / strong >
} }
/ >
< / ActionDialog >
< / >
2019-12-17 17:13:56 +00:00
) ;
} ;
export default PageList ;