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
@@ -12,8 +12,9 @@ export default class Dashboard extends React.Component<{
}> { }> {
constructor(props: any) { constructor(props: any) {
super(props); super(props);
if (this.props.location.state.dto !== undefined) { try {
// If the state exists being routed from /, fill in Dashboard State. // If the state exists being routed from /, fill in Dashboard State.
if (this.props.location.state.dto) {
const DTO = new API_DTO(); const DTO = new API_DTO();
DTO.setApiLink(this.props.location.state.dto.API_Link); DTO.setApiLink(this.props.location.state.dto.API_Link);
DTO.setApiToken(this.props.location.state.dto.API_Token); DTO.setApiToken(this.props.location.state.dto.API_Token);
@@ -23,10 +24,12 @@ export default class Dashboard extends React.Component<{
dto: DTO, dto: DTO,
ReportingRepository: new ReportingRepository(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. // Check if localStorage has valid items, if this is the case, we can use those.
// Resolve issue with cold navigation to /dashboard. // Resolve issue with cold navigation to /dashboard.
if (localStorage.getItem("APILink") !== null &&
localStorage.getItem("AccessToken") !== null) {
const DTO = new API_DTO(); const DTO = new API_DTO();
DTO.setApiLink(localStorage.getItem("APILink")); DTO.setApiLink(localStorage.getItem("APILink"));
DTO.setApiToken(localStorage.getItem("AccessToken")); DTO.setApiToken(localStorage.getItem("AccessToken"));
@@ -40,7 +43,7 @@ export default class Dashboard extends React.Component<{
// Route back to / if neither is the case. // Route back to / if neither is the case.
this.props.history.push('/'); this.props.history.push('/');
} }
}
this.checkFirstDate = this.checkFirstDate.bind(this); this.checkFirstDate = this.checkFirstDate.bind(this);
this.checkSecondDate = this.checkSecondDate.bind(this); this.checkSecondDate = this.checkSecondDate.bind(this);
this.getItemsFromApi = this.getItemsFromApi.bind(this); this.getItemsFromApi = this.getItemsFromApi.bind(this);
+6 -8
View File
@@ -8,8 +8,9 @@ export default class ReportingRepository {
this.instance = axios.create({ this.instance = axios.create({
baseURL: t.getApiLink(), baseURL: t.getApiLink(),
headers: { headers: {
'Authorization': 'Bearer ' + t.getApiToken(), 'Authorization': 'Token ' + t.getApiToken(),
'Access-Control-Allow-Origin': '*' 'Access-Control-Allow-Origin': '*',
'Accept': 'application/json'
} }
}); });
} }
@@ -18,15 +19,12 @@ export default class ReportingRepository {
throw new Error("Method not implemented."); throw new Error("Method not implemented.");
} }
Read(_t: API_DTO) { async Read(_t: API_DTO) {
if (this.instance === undefined) if (this.instance === undefined)
throw new Error("[ReportingRepository]: Please initiate the constructor first before calling a subfunction."); 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() } params: { "start_date": _t.getFirstDate(), "end_date": _t.getFirstDate() }
}) })).data;
.then((res) => {
return res.data;
});
} }
Update(_t: any) { Update(_t: any) {