Skip to content

Commit

Permalink
refactor: minor refactor from prev api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lekotros committed Sep 11, 2022
1 parent e570fbd commit 070e2b7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 49 deletions.
6 changes: 3 additions & 3 deletions app/components/TaskRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const ErrorList = ({ errors }: any) => {
color="error"
/>
<Chip
label={'line no: ' + errors[index].line}
label={`line: ${errors[index].line || 'unknown'}`}
variant="outlined"
size="small"
/>
Expand All @@ -155,7 +155,7 @@ const ErrorList = ({ errors }: any) => {
);
};

const TaskTableRow = ({ validation }: any) => {
const TaskTableRow = ({ session, name: taskName, validation }: any) => {
const { name, status, valid, errors } = validation;
const [open, setOpen] = React.useState(false);

Expand Down Expand Up @@ -265,7 +265,7 @@ const TaskRow = ({ session, task }: TaskRowProps) => {
</TableRow>
</TableHead>
<TableBody>
{ validations.map(v => <TaskTableRow key={v.name} validation={v} />) }
{ validations.map(v => <TaskTableRow key={v.name} session={session} name={task.name} validation={v} />) }
</TableBody>
</Table>
</TableContainer>
Expand Down
19 changes: 13 additions & 6 deletions app/components/ValidationConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const ruleOptions = [
label: 'Every stop place is referenced',
},
{
value: 'everyStopPointHaveAnArrivalAndDepartureTime',
value: 'everyStopPointHaveArrivalAndDepartureTime',
label: 'Every stop point have an arrival and departure time',
},
{
Expand Down Expand Up @@ -90,14 +90,14 @@ type ValidationConfigProps = {

const ValidationConfig = (props: ValidationConfigProps) => {
const { session, onValidate } = props;
const [schema, setSchema] = React.useState<string>('netex');
const [schema, setSchema] = React.useState<string>('netex@1.2');
const [rules, setRules] = React.useState<string[]>([
'everyLineIsReferenced',
'everyScheduledStopPointHasAName',
'everyStopPlaceHasACorrectStopPlaceType',
'everyStopPlaceHasAName',
'everyStopPlaceIsReferenced',
'everyStopPointHaveAnArrivalAndDepartureTime',
'everyStopPointHaveArrivalAndDepartureTime',
'frameDefaultsHaveALocaleAndTimeZone',
'locationsAreReferencingTheSamePoint',
'passingTimesHaveIncreasingTimes',
Expand Down Expand Up @@ -180,6 +180,12 @@ const ValidationConfig = (props: ValidationConfigProps) => {
}
}

React.useState(() => {
if (session.files.length) {
setCanValidate(true);
}
}, [session, setCanValidate]);

return (
<Stack spacing={4}>
<Stack spacing={1} direction="row">
Expand All @@ -204,9 +210,10 @@ const ValidationConfig = (props: ValidationConfigProps) => {
value={schema}
onChange={handleSelectChange}
>
<MenuItem key="netex" value="netex">NeTEx</MenuItem>
<MenuItem key="netex-light" value="netex-light">NeTEx Light</MenuItem>
<MenuItem key="epip" value="epip">EPIP</MenuItem>
<MenuItem key="netex" value="[email protected]">NeTEx (v1.2)</MenuItem>
<MenuItem key="netex-light" value="[email protected]">NeTEx Light (v1.2)</MenuItem>
<MenuItem key="epip" value="[email protected]">EPIP (v1.1.1)</MenuItem>
<MenuItem key="epip-light" value="[email protected]">EPIP Light (v1.1.1)</MenuItem>
</Select>
</FormControl>

Expand Down
82 changes: 44 additions & 38 deletions app/components/ValidationResult.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown';
import { Box, Button, ButtonGroup, Grid, Menu, MenuItem, Skeleton, Stack, Typography } from '@mui/material';
import { Box, Button, ButtonGroup, Divider, Grid, Menu, MenuItem, Skeleton, Stack, Typography } from '@mui/material';
import Link from 'next/link';
import { useRouter } from 'next/router';
import React from 'react';
Expand Down Expand Up @@ -32,7 +32,7 @@ type ValidationResultProps = {
const ValidationResult = (props: ValidationResultProps) => {
const { session } = props;
const router = useRouter();
const message = useSubscription(session ? `progress/${session.id}` : '');
const documentStatus = useSubscription(session ? `sessions/${session.id}/documents/+` : '');
const [tasks, setTasks] = React.useState<any[]>([]);
const { setSession } = useSessionStore();
const [ errorOpen, setErrorOpen ] = React.useState<boolean>(false);
Expand All @@ -41,6 +41,28 @@ const ValidationResult = (props: ValidationResultProps) => {
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const open = Boolean(anchorEl);

React.useEffect(() => {
if (!documentStatus) {
return;
}

const data = documentStatus.d;
const taskIndex = tasks.findIndex(t => t.originalName === data.document);

if (taskIndex === -1) {
setTasks([
...tasks,
{
name: truncName(data.document),
originalName: data.document,
valid: false,
status: documentStatus.t === 'VALIDATE_DOCUMENT_START' ? 'running' : 'complete',
validations: [],
}
].sort((a: any, b: any) => a.name > b.name ? 1 : -1));
}
}, [documentStatus]);

const handleValidateAnother = () => {
apiClient.createSession()
.then(session => {
Expand All @@ -65,47 +87,30 @@ const ValidationResult = (props: ValidationResultProps) => {
};

React.useEffect(() => {
if (!session) {
if (!session || !session.results) {
return;
}

if (session.status !== 'running') {
const tasks = session.results.map(v => {
return {
name: truncName(v.name),
originalName: v.name,
const tasks = session.results.map(v => {
const running = v.validations.find(v => !v.valid && !v.errors);

return {
name: truncName(v.name),
originalName: v.name,
valid: v.valid,
status: running ? 'running' : 'complete',
validations: v.validations.map((v: any) => ({
name: v.name,
valid: v.valid,
status: 'complete',
validations: v.validations.map((v: any) => ({
name: v.name,
valid: v.valid,
errors: v.errors || [],
})),
}
})
.sort((a, b) => a.name > b.name ? 1 : -1);

setTasks(tasks);
} else if (message) {
const tasks = message.map((p: any) => {
return {
name: truncName(p.name),
originalName: p.name,
valid: p.status === 'valid',
status: p.status === 'running' ? 'running' : 'complete',
validations: Object.keys(p.jobStatus).map((k) => ({
name: k,
valid: p.jobStatus[k] === 'valid',
status: p.jobStatus[k],
errors: [],
})),
};
})
.sort((a: any, b: any) => a.name > b.name ? 1 : -1)
status: running ? 'running' : 'complete',
errors: v.errors || [],
})),
}
})
.sort((a, b) => a.name > b.name ? 1 : -1);

setTasks(tasks);
}
}, [message, session]);
setTasks(tasks);
}, [session]);

return (
<Stack spacing={4}>
Expand Down Expand Up @@ -135,6 +140,7 @@ const ValidationResult = (props: ValidationResultProps) => {
</>
)}
</Box>
<Divider />
<Grid container>
<Grid item xs={12} md={6}>
<ButtonGroup disabled={session.status !== 'complete'}>
Expand Down
7 changes: 6 additions & 1 deletion app/hooks/useMqttClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ const useMqttClient = () => {

export const useSubscription = (topic: string) => {
const [message, setMessage] = React.useState<any>(null);
const pattern = new RegExp(`^${topic.replace(/[+]/g, '[^\\/]+').replace(/[#]/g, '.+')}$`);

const handler = React.useCallback((topic: string, payload: any) => {
if (!topic.match(pattern)) {
return;
}

try {
setMessage(JSON.parse(payload.toString()));
} catch (err) {
Expand All @@ -21,7 +26,7 @@ export const useSubscription = (topic: string) => {
}, [topic]);

React.useEffect(() => {
mqttClient.subscribe(topic); // TODO unsubscribe
mqttClient.subscribe(topic);
mqttClient.on('message', handler);

return () => {
Expand Down
2 changes: 1 addition & 1 deletion app/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const Home: NextPage = () => {
return (
<React.Fragment>
<Head>
<title>Greenlight | NeTEx validation</title>
<title>NeTEx validation | Greenlight</title>
<meta name="description" content="Fast and simple NeTEx validation" />
</Head>

Expand Down

0 comments on commit 070e2b7

Please sign in to comment.