From 896a34f58558682da8624a442e022a6611551a03 Mon Sep 17 00:00:00 2001 From: Jeroen Vijgen Date: Mon, 24 Feb 2020 16:33:57 +0100 Subject: [PATCH] Fixed cold navigation to /dashboard --- .../DashboardComponent/Dashboard.tsx | 51 ++++++++++--------- src/Repositories/ReportingRepository.ts | 14 +++-- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/Components/DashboardComponent/Dashboard.tsx b/src/Components/DashboardComponent/Dashboard.tsx index 44e3de0..d526753 100644 --- a/src/Components/DashboardComponent/Dashboard.tsx +++ b/src/Components/DashboardComponent/Dashboard.tsx @@ -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); diff --git a/src/Repositories/ReportingRepository.ts b/src/Repositories/ReportingRepository.ts index 0528bde..99935dd 100644 --- a/src/Repositories/ReportingRepository.ts +++ b/src/Repositories/ReportingRepository.ts @@ -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) {