UNPKG

@craftercms/studio-ui

Version:

Services, components, models & utils to build CrafterCMS authoring extensions.

155 lines (153 loc) 6.1 kB
/* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import DashletCard from '../DashletCard/DashletCard'; import palette from '../../styles/palette'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import React, { useEffect, useMemo } from 'react'; import { DashletEmptyMessage, getItemSkeleton, List, ListItem, ListItemAvatar, PersonAvatar } from '../DashletCard/dashletCommons'; import ListItemText from '@mui/material/ListItemText'; import Typography from '@mui/material/Typography'; import { LIVE_COLOUR, STAGING_COLOUR } from '../ItemPublishingTargetIcon/styles'; import useSpreadState from '../../hooks/useSpreadState'; import useLocale from '../../hooks/useLocale'; import useActiveSiteId from '../../hooks/useActiveSiteId'; import { fetchPublishingHistory } from '../../services/dashboard'; import { asLocalizedDateTime } from '../../utils/datetime'; import IconButton from '@mui/material/IconButton'; import { RefreshRounded } from '@mui/icons-material'; const messages = defineMessages({ staging: { id: 'words.staging', defaultMessage: 'Staging' }, live: { id: 'words.live', defaultMessage: 'Live' } }); export function RecentlyPublishedDashlet(props) { const { borderLeftColor = palette.blue.tint } = props; const [{ items, total, loading }, setState] = useSpreadState({ items: null, total: null, loading: false }); const locale = useLocale(); const site = useActiveSiteId(); const { formatMessage } = useIntl(); const onRefresh = useMemo( () => () => { setState({ items: null, loading: true }); fetchPublishingHistory(site, { limit: 10, offset: 0, dateFrom: '2022-03-28T22:00:00.000Z', dateTo: '2022-04-28T22:00:00.000Z' }).subscribe((packages) => { setState({ items: packages, total: packages.total, loading: false }); }); }, [setState, site] ); useEffect(() => { onRefresh(); }, [onRefresh]); return React.createElement( DashletCard, Object.assign({}, props, { borderLeftColor: borderLeftColor, title: React.createElement(FormattedMessage, { id: 'recentlyPublishedDashlet.widgetTitle', defaultMessage: 'Recently Published' }), headerAction: React.createElement(IconButton, { onClick: onRefresh }, React.createElement(RefreshRounded, null)) }), loading && getItemSkeleton({ numOfItems: 3, showAvatar: true }), items && React.createElement( List, null, items.map((item) => React.createElement( ListItem, { key: item.id }, React.createElement(ListItemAvatar, null, React.createElement(PersonAvatar, { person: item.submitter })), React.createElement(ListItemText, { primary: React.createElement(FormattedMessage, { id: 'recentlyPublishedDashlet.entryPrimaryText', defaultMessage: '{name} published {count} {count, plural, one {package} other {packages}} to <render_target>{publishingTarget}</render_target> <render_date>{date}</render_date>', values: { count: item.size, name: item.submitter.firstName, publishingTarget: item.publishingTarget, date: item.schedule, render_target(target) { return React.createElement( Typography, { component: 'span', fontWeight: 'bold', color: target[0] === 'live' ? LIVE_COLOUR : STAGING_COLOUR }, messages[target[0]] ? formatMessage(messages[target[0]]).toLowerCase() : target[0] ); }, render_date(date) { return asLocalizedDateTime(date[0], locale.localeCode, locale.dateTimeFormatOptions); } } }), secondary: React.createElement( Typography, { color: 'text.secondary', variant: 'body2' }, React.createElement(FormattedMessage, { id: 'recentlyPublishedDashlet.noSubmissionCommentAvailable', defaultMessage: 'Submission comment not provided' }) ) }) ) ) ), total === 0 && React.createElement( DashletEmptyMessage, null, React.createElement(FormattedMessage, { id: 'recentlyPublishedDashlet.noRecentlyPublishedItems', defaultMessage: 'There are no items have been published recently' }) ) ); } export default RecentlyPublishedDashlet;