diff --git a/src/routes/NestedTicketRoutes.js b/src/routes/NestedTicketRoutes.js index cc7431d..9409e8d 100644 --- a/src/routes/NestedTicketRoutes.js +++ b/src/routes/NestedTicketRoutes.js @@ -14,9 +14,7 @@ const NestedTicketRoutes = ({ path }) => (
Please select a ticket.
- - - + ); diff --git a/src/views/tickets/Screenshot.js b/src/views/tickets/Screenshot.js index 453abff..00de6f6 100644 --- a/src/views/tickets/Screenshot.js +++ b/src/views/tickets/Screenshot.js @@ -2,67 +2,23 @@ import React, { Component } from 'react'; import { Header, Image, Segment } from 'semantic-ui-react'; export default class Screenshot extends Component { - state = { - id: '', - processed: '', - }; - - onResponseReceived = async e => { - this.state.id = this.props.ticketID; - //there is not reason why this should be called twice but - //I can't figure out how to pass the processed flag through the routes - try { - const res = await fetch( - 'http://localhost:8080/api/tickets/' + this.state.id, - { - method: 'GET', - } - ); - - const response = await res.json(); - var processed = response.ticket.processed; - } catch (error) { - console.log(error); - } - - if (processed) { - this.setState({ processed: true }); - const endpoint = - 'http://localhost:8080/api/tickets/' + this.state.id + '/artifacts'; - try { - const res = await fetch(endpoint, { - method: 'GET', - }); - } catch (error) { - console.log(error); - } - } else { - this.setState({ processed: false }); - } - }; - render() { - this.onResponseReceived(); - + const { ticketID, processed } = this.props.ticketInfo; return (
{' '} - Screenshot for ticket #{this.state.id}{' '} + Screenshot for ticket #{ticketID}{' '}
- {this.state.processed && ( + {processed && ( fullpage screenshot )} - {!this.state.processed && ( + {!processed && (
{' '} This ticket has not been processed. Please wait.{' '} diff --git a/src/views/tickets/Ticket.js b/src/views/tickets/Ticket.js index a4ab0db..9b35015 100644 --- a/src/views/tickets/Ticket.js +++ b/src/views/tickets/Ticket.js @@ -1,16 +1,62 @@ -import React from 'react'; -import { Header } from 'semantic-ui-react'; -import { useParams } from 'react-router-dom'; - +import React, { Component } from 'react'; import Screenshot from './Screenshot'; -const Ticket = () => { - const { ticketID, url } = useParams(); - return ( - <> - - - ); -}; +const REFRESH_EVERY_MS = 1000; + +export default class Ticket extends Component { + constructor(props) { + super(props); + + this.state = { + ticketInfo: { + ticketID: props.match.params.ticketID, + processed: false, + }, + refreshInterval: setInterval(this.refreshTicketState, REFRESH_EVERY_MS), + }; + + this.refreshTicketState(); + } + + stopAutomaticRefreshing() { + clearInterval(this.state.refreshInterval); + } + + componentWillUnmount() { + // clean up in case we're navigating away: + this.stopAutomaticRefreshing(); + } + + refreshTicketState = async e => { + const ticketURL = `http://localhost:8080/api/tickets/${this.state.ticketInfo.ticketID}`; + + try { + fetch(ticketURL, { method: 'GET' }) + .then(res => res.json()) + .then(res => { + const { processed } = res.ticket; + + this.setState(prevState => { + const { ticketInfo } = prevState; + ticketInfo.processed = processed; + return ticketInfo; + }); + + if (processed) { + this.stopAutomaticRefreshing(); + } + }); + } catch (error) { + console.log(error); + } + }; -export default Ticket; + render() { + const { ticketInfo } = this.state; + return ( + <> + + + ); + } +}