|
| 1 | +import { Table, Typography } from '@equinor/eds-core-react' |
| 2 | +import { useLanguageContext } from 'components/Contexts/LanguageContext' |
| 3 | +import { useMissionDefinitionsContext } from 'components/Contexts/MissionDefinitionsContext' |
| 4 | +import { StyledTableBody, StyledTableCell } from 'components/Styles/StyledComponents' |
| 5 | +import { DaysOfWeek } from 'models/AutoScheduleFrequency' |
| 6 | +import { config } from 'config' |
| 7 | +import { useNavigate } from 'react-router-dom' |
| 8 | +import styled from 'styled-components' |
| 9 | + |
| 10 | +const StyledSection = styled.div` |
| 11 | + display: flex; |
| 12 | + flex-direction: column; |
| 13 | + max-width: 960px; |
| 14 | + gap: 1rem; |
| 15 | +` |
| 16 | +const StyledTableRow = styled.div` |
| 17 | + display: flex; |
| 18 | + flex-direction: row; |
| 19 | + gap: 1rem; |
| 20 | +` |
| 21 | +const StyledHeader = styled.div` |
| 22 | + gap: 0px; |
| 23 | +` |
| 24 | +const StyledDayOverview = styled.div` |
| 25 | + display: grid; |
| 26 | + gap: 0px; |
| 27 | +` |
| 28 | + |
| 29 | +const AutoScheduleList = () => { |
| 30 | + const { TranslateText } = useLanguageContext() |
| 31 | + const { missionDefinitions } = useMissionDefinitionsContext() |
| 32 | + const navigate = useNavigate() |
| 33 | + |
| 34 | + const autoScheduleMissionDefinitions = missionDefinitions.filter((m) => m.autoScheduleFrequency) |
| 35 | + |
| 36 | + const allDays = [ |
| 37 | + DaysOfWeek.Monday, |
| 38 | + DaysOfWeek.Tuesday, |
| 39 | + DaysOfWeek.Wednesday, |
| 40 | + DaysOfWeek.Thursday, |
| 41 | + DaysOfWeek.Friday, |
| 42 | + DaysOfWeek.Saturday, |
| 43 | + DaysOfWeek.Sunday, |
| 44 | + ] |
| 45 | + |
| 46 | + const DayOverview = () => |
| 47 | + allDays.map((day) => { |
| 48 | + const missionDefinitions = autoScheduleMissionDefinitions.filter((m) => |
| 49 | + m.autoScheduleFrequency!.daysOfWeek.includes(day) |
| 50 | + ) |
| 51 | + const timeMissionPairs = missionDefinitions |
| 52 | + .map((mission) => |
| 53 | + mission.autoScheduleFrequency!.timesOfDay.map((time) => { |
| 54 | + return { time, mission } |
| 55 | + }) |
| 56 | + ) |
| 57 | + .flat() |
| 58 | + .sort((a, b) => (a.time > b.time ? 1 : -1)) |
| 59 | + |
| 60 | + return ( |
| 61 | + <Table key={day}> |
| 62 | + <Table.Head> |
| 63 | + <Table.Row> |
| 64 | + <StyledTableCell>{TranslateText(day)}</StyledTableCell> |
| 65 | + </Table.Row> |
| 66 | + </Table.Head> |
| 67 | + <StyledTableBody> |
| 68 | + {timeMissionPairs.length > 0 ? ( |
| 69 | + timeMissionPairs.map(({ time, mission }) => ( |
| 70 | + <Table.Row |
| 71 | + key={mission.id + time} |
| 72 | + onClick={() => |
| 73 | + navigate(`${config.FRONTEND_BASE_ROUTE}/mission-definition/${mission.id}`) |
| 74 | + } |
| 75 | + > |
| 76 | + <Table.Cell> |
| 77 | + <StyledTableRow> |
| 78 | + <Typography>{`${time.substring(0, 5)}`}</Typography> |
| 79 | + <Typography link>{mission.name}</Typography> |
| 80 | + </StyledTableRow> |
| 81 | + </Table.Cell> |
| 82 | + </Table.Row> |
| 83 | + )) |
| 84 | + ) : ( |
| 85 | + <Table.Row> |
| 86 | + <Table.Cell> |
| 87 | + <Typography>{TranslateText('No missions')}</Typography> |
| 88 | + </Table.Cell> |
| 89 | + </Table.Row> |
| 90 | + )} |
| 91 | + </StyledTableBody> |
| 92 | + </Table> |
| 93 | + ) |
| 94 | + }) |
| 95 | + |
| 96 | + return ( |
| 97 | + <> |
| 98 | + {autoScheduleMissionDefinitions.length > 0 && ( |
| 99 | + <StyledSection> |
| 100 | + <StyledHeader> |
| 101 | + <Typography variant="h1" color="resting"> |
| 102 | + {TranslateText('Auto Scheduling')} |
| 103 | + </Typography> |
| 104 | + <Typography> |
| 105 | + {TranslateText('These missions will be automatically scheduled at the specified time')} |
| 106 | + </Typography> |
| 107 | + </StyledHeader> |
| 108 | + <StyledDayOverview> |
| 109 | + <DayOverview /> |
| 110 | + </StyledDayOverview> |
| 111 | + </StyledSection> |
| 112 | + )} |
| 113 | + </> |
| 114 | + ) |
| 115 | +} |
| 116 | + |
| 117 | +export const AutoScheduleSection = () => { |
| 118 | + return AutoScheduleList() |
| 119 | +} |
0 commit comments