Fixed cold navigation to /dashboard

This commit is contained in:
2020-02-24 16:33:57 +01:00
parent 2683c85c13
commit 896a34f585
2 changed files with 33 additions and 32 deletions
+27 -24
View File
@@ -12,35 +12,38 @@ export default class Dashboard extends React.Component<{
}> {
constructor(props: any) {
super(props);
if (this.props.location.state.dto !== undefined) {
try {
// If the state exists being routed from /, fill in Dashboard State.
const DTO = new API_DTO();
DTO.setApiLink(this.props.location.state.dto.API_Link);
DTO.setApiToken(this.props.location.state.dto.API_Token);
DTO.setFirstDate(localStorage.getItem("First_Date") || "");
DTO.setSecondDate(localStorage.getItem("Second_Date") || "");
this.state = {
dto: DTO,
ReportingRepository: new ReportingRepository(DTO)
if (this.props.location.state.dto) {
const DTO = new API_DTO();
DTO.setApiLink(this.props.location.state.dto.API_Link);
DTO.setApiToken(this.props.location.state.dto.API_Token);
DTO.setFirstDate(localStorage.getItem("First_Date") || "");
DTO.setSecondDate(localStorage.getItem("Second_Date") || "");
this.state = {
dto: DTO,
ReportingRepository: new ReportingRepository(DTO)
}
}
} else if (localStorage.getItem("APILink") !== null &&
localStorage.getItem("AccessToken") !== null) {
} catch (err) {
// Check if localStorage has valid items, if this is the case, we can use those.
// Resolve issue with cold navigation to /dashboard.
const DTO = new API_DTO();
DTO.setApiLink(localStorage.getItem("APILink"));
DTO.setApiToken(localStorage.getItem("AccessToken"));
DTO.setFirstDate(localStorage.getItem("First_Date") || "");
DTO.setSecondDate(localStorage.getItem("Second_Date") || "");
this.state = {
dto: DTO,
ReportingRepository: new ReportingRepository(DTO)
}
} else {
// Route back to / if neither is the case.
this.props.history.push('/');
if (localStorage.getItem("APILink") !== null &&
localStorage.getItem("AccessToken") !== null) {
const DTO = new API_DTO();
DTO.setApiLink(localStorage.getItem("APILink"));
DTO.setApiToken(localStorage.getItem("AccessToken"));
DTO.setFirstDate(localStorage.getItem("First_Date") || "");
DTO.setSecondDate(localStorage.getItem("Second_Date") || "");
this.state = {
dto: DTO,
ReportingRepository: new ReportingRepository(DTO)
}
} else {
// Route back to / if neither is the case.
this.props.history.push('/');
}
}
this.checkFirstDate = this.checkFirstDate.bind(this);
this.checkSecondDate = this.checkSecondDate.bind(this);
this.getItemsFromApi = this.getItemsFromApi.bind(this);
+6 -8
View File
@@ -8,8 +8,9 @@ export default class ReportingRepository {
this.instance = axios.create({
baseURL: t.getApiLink(),
headers: {
'Authorization': 'Bearer ' + t.getApiToken(),
'Access-Control-Allow-Origin': '*'
'Authorization': 'Token ' + t.getApiToken(),
'Access-Control-Allow-Origin': '*',
'Accept': 'application/json'
}
});
}
@@ -18,15 +19,12 @@ export default class ReportingRepository {
throw new Error("Method not implemented.");
}
Read(_t: API_DTO) {
async Read(_t: API_DTO) {
if (this.instance === undefined)
throw new Error("[ReportingRepository]: Please initiate the constructor first before calling a subfunction.");
this.instance.get("api/reporting/v1/rooms/84e0fefa-5675-11e7-a349-00163efdd8db/chat-stats/daily/", {
return (await this.instance.get("api/reporting/v1/rooms/84e0fefa-5675-11e7-a349-00163efdd8db/chat-stats/daily/", {
params: { "start_date": _t.getFirstDate(), "end_date": _t.getFirstDate() }
})
.then((res) => {
return res.data;
});
})).data;
}
Update(_t: any) {