-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from csci4950tgt/fix/infinite-page-loop
feat(ticket, screenshot): refactor, auto refresh, stop infinite loop
- Loading branch information
Showing
3 changed files
with
65 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<> | ||
<Screenshot ticketID={ticketID} url={url} /> | ||
</> | ||
); | ||
}; | ||
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 ( | ||
<> | ||
<Screenshot ticketInfo={ticketInfo} /> | ||
</> | ||
); | ||
} | ||
} |