import { localizedTimestamp, TimeAgo } from 'components/TimeAgo' import Card from '@material-ui/core/Card' import Dialog from '@material-ui/core/Dialog' import Grid from '@material-ui/core/Grid' import List from '@material-ui/core/List' import ListItem from '@material-ui/core/ListItem' import Badge from '@material-ui/core/Badge' import { createStyles, Theme, withStyles, WithStyles, } from '@material-ui/core/styles' import Typography from '@material-ui/core/Typography' import { ApiResponse } from 'utils/json-api-client' import { JobSpec } from 'core/store/models' import classNames from 'classnames' import React from 'react' import { connect } from 'react-redux' import { Redirect, useLocation } from 'react-router-dom' import { createJobRun, deleteJobSpec } from 'actionCreators' import BaseLink from 'components/BaseLink' import Button from 'components/Button' import CopyJobSpec from 'components/CopyJobSpec' import Close from 'components/Icons/Close' import Link from 'components/Link' import ErrorMessage from 'components/Notifications/DefaultError' import { JobData } from './sharedTypes' const styles = (theme: Theme) => createStyles({ badgePadding: { paddingLeft: theme.spacing.unit * 2, paddingRight: theme.spacing.unit * 2, marginLeft: theme.spacing.unit * -2, marginRight: theme.spacing.unit * -2, lineHeight: '1rem', }, container: { backgroundColor: theme.palette.common.white, padding: theme.spacing.unit * 5, paddingBottom: 0, }, mainRow: { marginBottom: theme.spacing.unit * 2, }, actions: { textAlign: 'right', }, regionalNavButton: { marginLeft: theme.spacing.unit, marginRight: theme.spacing.unit, }, horizontalNav: { paddingBottom: 0, }, horizontalNavItem: { display: 'inline', paddingLeft: 0, paddingRight: 0, }, horizontalNavLink: { padding: `${theme.spacing.unit * 4}px ${theme.spacing.unit * 4}px`, textDecoration: 'none', display: 'inline-block', borderBottom: 'solid 1px', borderBottomColor: theme.palette.common.white, '&:hover': { borderBottomColor: theme.palette.primary.main, }, }, activeNavLink: { color: theme.palette.primary.main, borderBottomColor: theme.palette.primary.main, }, jobSpecId: { overflow: 'hidden', textOverflow: 'ellipsis', }, dialogPaper: { minHeight: '240px', maxHeight: '240px', minWidth: '670px', maxWidth: '670px', overflow: 'hidden', borderRadius: theme.spacing.unit * 3, }, warningText: { fontWeight: 500, marginLeft: theme.spacing.unit * 3, marginTop: theme.spacing.unit * 3, marginBottom: theme.spacing.unit, }, closeButton: { marginRight: theme.spacing.unit * 3, marginTop: theme.spacing.unit * 3, }, infoText: { fontSize: theme.spacing.unit * 2, fontWeight: 450, marginLeft: theme.spacing.unit * 6, }, modalContent: { width: 'inherit', }, archiveButton: { marginTop: theme.spacing.unit * 4, }, }) const isWebInitiator = ( initiators: ApiResponse['data']['attributes']['initiators'], ) => initiators.find((initiator) => initiator.type === 'web') const CreateRunSuccessNotification = ({ data }: any) => ( Successfully created job run{' '} {data.id} ) const DeleteSuccessNotification = ({ id }: any) => ( Successfully archived job {id} ) interface Props extends WithStyles { createJobRun: Function deleteJobSpec: Function jobSpecId: string job: JobData['job'] runsCount: JobData['recentRunsCount'] url: string getJobSpecRuns: (props: { page?: number; size?: number }) => Promise } const RegionalNavComponent = ({ classes, createJobRun, jobSpecId, job, deleteJobSpec, getJobSpecRuns, runsCount, }: Props) => { const location = useLocation() const navErrorsActive = location.pathname.endsWith('/errors') const navDefinitionActive = location.pathname.endsWith('/definition') const navRunsActive = location.pathname.endsWith('/runs') const navOverviewActive = !navDefinitionActive && !navErrorsActive && !navRunsActive const [modalOpen, setModalOpen] = React.useState(false) const [archived, setArchived] = React.useState(false) const handleRun = () => { const params = new URLSearchParams(location.search) const page = params.get('page') const size = params.get('size') createJobRun(jobSpecId, CreateRunSuccessNotification, ErrorMessage).then( () => getJobSpecRuns({ page: page ? parseInt(page, 10) : undefined, size: size ? parseInt(size, 10) : undefined, }), ) } const handleDelete = (id: string) => { deleteJobSpec( id, () => DeleteSuccessNotification({ id }), ErrorMessage, job?.type, ) setArchived(true) } return ( <> setModalOpen(false)} > Warning: This Action Cannot Be Undone setModalOpen(false)} /> - All associated job runs will be deleted - Access to this page will be lost {job && ( {job?.type} job spec detail )} {job && ( {job.name || jobSpecId} )} {job && ( <> {job.type === 'Direct request' && job.initiators && isWebInitiator(job.initiators) && ( )} {job.definition && ( <> )} )} {job?.name && ( {job.id} )} {job?.createdAt && ( Created {job.createdAt} ( {localizedTimestamp(job.createdAt)}) )} Overview Definition {job?.errors && job.errors.length > 0 ? ( Errors ) : ( 'Errors' )} Runs ) } const mapStateToProps = (state: any) => ({ url: state.notifications.currentUrl, }) export const ConnectedRegionalNav = connect(mapStateToProps, { createJobRun, deleteJobSpec, })(RegionalNavComponent) export const RegionalNav = withStyles(styles)(ConnectedRegionalNav) export default RegionalNav