From 2683c85c13000bd5bfdeb414df81357c585def67 Mon Sep 17 00:00:00 2001 From: Jeroen Vijgen Date: Sun, 23 Feb 2020 21:05:04 +0100 Subject: [PATCH] Enabled Repository --- .../DashboardComponent/Dashboard.tsx | 24 +++++++++++--- src/DTOs/IDTO.ts | 1 - src/DTOs/api.dto.ts | 4 +-- src/Repositories/IRepository.ts | 6 ---- src/Repositories/ReportingRepository.ts | 31 ++++++++++++++++--- 5 files changed, 48 insertions(+), 18 deletions(-) delete mode 100644 src/DTOs/IDTO.ts delete mode 100644 src/Repositories/IRepository.ts diff --git a/src/Components/DashboardComponent/Dashboard.tsx b/src/Components/DashboardComponent/Dashboard.tsx index 7732009..44e3de0 100644 --- a/src/Components/DashboardComponent/Dashboard.tsx +++ b/src/Components/DashboardComponent/Dashboard.tsx @@ -4,16 +4,25 @@ import API_DTO from '../../DTOs/api.dto'; import ReportingRepository from '../../Repositories/ReportingRepository'; export default class Dashboard extends React.Component<{ - history: any + history: any, + location: any }, { dto: API_DTO, ReportingRepository: ReportingRepository }> { constructor(props: any) { super(props); - if (props.location.state.dto !== undefined) { + if (this.props.location.state.dto !== undefined) { // If the state exists being routed from /, fill in Dashboard State. - this.setState({ dto: 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) { // Check if localStorage has valid items, if this is the case, we can use those. @@ -21,11 +30,17 @@ export default class Dashboard extends React.Component<{ const DTO = new API_DTO(); DTO.setApiLink(localStorage.getItem("APILink")); DTO.setApiToken(localStorage.getItem("AccessToken")); - this.setState({ dto: DTO }); + 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); @@ -41,6 +56,7 @@ export default class Dashboard extends React.Component<{ getItemsFromApi() { // TODO: Connect to ReportingRepository. + console.log(this.state.ReportingRepository.Read(this.state.dto)); } render () { diff --git a/src/DTOs/IDTO.ts b/src/DTOs/IDTO.ts deleted file mode 100644 index d5c1913..0000000 --- a/src/DTOs/IDTO.ts +++ /dev/null @@ -1 +0,0 @@ -export default interface IDTO { }; diff --git a/src/DTOs/api.dto.ts b/src/DTOs/api.dto.ts index df5a370..940534d 100644 --- a/src/DTOs/api.dto.ts +++ b/src/DTOs/api.dto.ts @@ -1,6 +1,4 @@ -import IDTO from './IDTO'; - -export default class API_DTO implements IDTO { +export default class API_DTO { API_Link: string; API_Token: string; First_Date: string; diff --git a/src/Repositories/IRepository.ts b/src/Repositories/IRepository.ts deleted file mode 100644 index b6046b3..0000000 --- a/src/Repositories/IRepository.ts +++ /dev/null @@ -1,6 +0,0 @@ -export default interface IRepository { - Create(t: any): any; - Read(t: any): any; - Update(t: any): any; - Delete(t: any): any; -} diff --git a/src/Repositories/ReportingRepository.ts b/src/Repositories/ReportingRepository.ts index 588c24a..0528bde 100644 --- a/src/Repositories/ReportingRepository.ts +++ b/src/Repositories/ReportingRepository.ts @@ -1,15 +1,38 @@ -import IRepository from './IRepository'; +import API_DTO from '../DTOs/api.dto'; +import axios, { AxiosInstance } from 'axios'; + +export default class ReportingRepository { + private instance: AxiosInstance | undefined; + + constructor(t: API_DTO) { + this.instance = axios.create({ + baseURL: t.getApiLink(), + headers: { + 'Authorization': 'Bearer ' + t.getApiToken(), + 'Access-Control-Allow-Origin': '*' + } + }); + } -export default class ReportingRepository implements IRepository { Create(_t: any) { throw new Error("Method not implemented."); } - Read(_t: any) { - throw new Error("Method not implemented."); + + 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/", { + params: { "start_date": _t.getFirstDate(), "end_date": _t.getFirstDate() } + }) + .then((res) => { + return res.data; + }); } + Update(_t: any) { throw new Error("Method not implemented."); } + Delete(_t: any) { throw new Error("Method not implemented."); }