feat: use context wherever token and baseUrl are needed

pull/8/head
c-cal 6 years ago
parent 3222d591c8
commit 396c15f568
Signed by: watcha
GPG Key ID: 87DD78E7F7A1581D
  1. 52
      src/AdminHome.js
  2. 8
      src/App.js
  3. 8
      src/CollapsableRightPanel.js
  4. 14
      src/CreateUserRightPanel.js
  5. 13
      src/DataToTable.js
  6. 4
      src/Login.js
  7. 30
      src/Monitoring.js
  8. 26
      src/StatsTab.js
  9. 28
      src/UserRightPanel.js

@ -1,27 +1,22 @@
import React, { Component } from "react";
import { Tab, Tabs } from "react-bootstrap";
import { withTranslation } from "react-i18next";
import Tab from "react-bootstrap/Tab";
import Tabs from "react-bootstrap/Tabs";
import DataToTable from "./DataToTable";
// import Monitoring from './Monitoring';
import StatsTab from "./StatsTab";
import { withTranslation } from "react-i18next";
//import Monitoring from './Monitoring';
class AdminHome extends Component {
constructor(props) {
super(props);
constructor() {
super();
this.state = {
refresh: true,
key: 1,
};
}
componentDidMount = () => {};
onClose = () => {
this.setState({
rightPanel: false,
});
};
onClose = () => this.setState({ rightPanel: false });
onTabSelected = (tabKey, data) => {
this.setState({
@ -30,21 +25,13 @@ class AdminHome extends Component {
});
};
handleSelect = key => {
this.setState({ key });
};
handleSelect = key => this.setState({ key });
render() {
const KEY = this.state.key ? this.state.key : 1;
const SELECTED = this.state.data ? this.state.data : false;
const { t } = this.props;
const STATSTAB = (
<StatsTab
token={this.props.token}
server={this.props.server}
onTabSelected={this.onTabSelected}
/>
);
const STATSTAB = <StatsTab onTabSelected={this.onTabSelected} />;
return (
<div className="AdminHomeContainer">
@ -57,24 +44,18 @@ class AdminHome extends Component {
<Tab eventKey={1} title={t("Overview")}>
{STATSTAB}
</Tab>
<Tab eventKey={2} title={t("Users")}>
<DataToTable
tableName="user"
token={this.props.token}
server={this.props.server}
setRightPanel={this.setRightPanel}
onClose={this.onClose}
value={SELECTED}
onTabSelected={this.onTabSelected}
/>
</Tab>
<Tab eventKey={3} title={t("Rooms")}>
<DataToTable
tableName="room"
token={this.props.token}
server={this.props.server}
setRightPanel={this.setRightPanel}
onClose={this.onClose}
stats={this.state.statsData}
@ -83,17 +64,8 @@ class AdminHome extends Component {
/>
</Tab>
{/* not functional yet
<Tab
eventKey={4}
title={t("Monitoring")}
token={this.props.token}
server={this.props.server}
>
<Monitoring
token={this.props.token}
server={this.props.server}
onTabSelected={this.onTabSelected}
/>
<Tab eventKey={4} title={t("Monitoring")}>
<Monitoring onTabSelected={this.onTabSelected} />
</Tab> */}
</Tabs>
</div>

@ -4,7 +4,7 @@ import sdk from "matrix-js-sdk";
import AdminHome from "./AdminHome.js";
import ErrorBoundary from "./ErrorBoundary.js";
import Login from "./Login.js";
import MatrixClientContext from "./MatrixClientContext"
import MatrixClientContext from "./MatrixClientContext";
import "./App.css";
import "./User.css";
@ -78,11 +78,7 @@ class App extends Component {
<ErrorBoundary>
<MatrixClientContext.Provider value={this.state.client}>
{this.state.clientPrepared ? (
<AdminHome
className="AdminHome"
token={this.state.client.getAccessToken()}
server={this.state.client.baseUrl}
/>
<AdminHome className="AdminHome" />
) : this.state.loginError ? (
<Login setupClient={this.setupClient} />
) : null}

@ -18,8 +18,6 @@ class CollapsableRightPanel extends Component {
<UserRightPanel
data={this.props.data}
onClose={this.props.onClose}
server={this.props.server}
token={this.props.token}
lang={this.props.lang}
refresh={this.props.refresh}
onTabSelected={this.props.onTabSelected}
@ -34,8 +32,6 @@ class CollapsableRightPanel extends Component {
<RoomRightPanel
data={this.props.data}
onClose={this.props.onClose}
server={this.props.server}
token={this.props.token}
refresh={this.props.refresh}
onTabSelected={this.props.onTabSelected}
/>
@ -47,8 +43,6 @@ class CollapsableRightPanel extends Component {
<CreateUserRightPanel
data={this.props.data}
onClose={this.props.onClose}
server={this.props.server}
token={this.props.token}
refresh={this.props.refresh}
refreshRightPanel={this.props.refreshRightPanel}
isEmailAvailable={this.props.isEmailAvailable}
@ -62,8 +56,6 @@ class CollapsableRightPanel extends Component {
<UserRightPanel
data={this.props.data}
onClose={this.props.onClose}
server={this.props.server}
token={this.props.token}
refresh={this.props.refresh}
lang={this.props.lang}
/>

@ -6,6 +6,8 @@ import Card from "react-bootstrap/Card";
import Collapse from "react-bootstrap/Collapse";
import Table from "react-bootstrap/Table";
import MatrixClientContext from "./MatrixClientContext";
class CreateUserRightPanel extends Component {
constructor(props) {
super(props);
@ -21,6 +23,8 @@ class CreateUserRightPanel extends Component {
};
}
static contextType = MatrixClientContext;
onEmailChange = ev => {
this.setState({
emailValue: ev.target.value,
@ -51,8 +55,7 @@ class CreateUserRightPanel extends Component {
onUserIdEdit = () => this.setState({ editUserId: !this.state.editUserId });
createUser = async () => {
const HOME_SERVER = this.props.server;
const ACCESS_TOKEN = this.props.token;
const client = this.context;
const { t } = this.props;
if (!this.state.isEmail) {
this.setState({
@ -112,11 +115,14 @@ class CreateUserRightPanel extends Component {
? this.state.userIdValue
: this.state.suggestedUserId;
const USER_REQUEST = await fetch(
new URL("_matrix/client/r0/watcha_register", HOME_SERVER),
new URL(
"_matrix/client/r0/watcha_register",
client.baseUrl
),
{
method: "POST",
headers: {
Authorization: "Bearer " + ACCESS_TOKEN,
Authorization: "Bearer " + client.getAccessToken(),
Accept: "application/json",
"Content-Type": "application/json",
},

@ -6,6 +6,8 @@ import CollapsableRightPanel from "./CollapsableRightPanel";
import Datatorow from "./DataToRow";
import TableToolBar from "./TableToolBar";
import MatrixClientContext from "./MatrixClientContext";
const TABLE_TYPE =
// here we declare all the type of table we wish to display
{
@ -134,6 +136,8 @@ class DataToTable extends Component {
};
}
static contextType = MatrixClientContext;
componentDidMount() {
document.addEventListener("keydown", this.escFunction, false); //allow esc to close right panel
this.setState({ header: this.getHeader(this.state.type) }); //initialize header
@ -168,18 +172,17 @@ class DataToTable extends Component {
};
getData = async () => {
const client = this.context;
let jsonData;
const arrayData = [];
const HOME_SERVER = this.props.server;
const ACCESS_TOKEN = this.props.token;
const HEADERS = this.state.type["header"];
try {
const TABLE_REQUEST = await fetch(
new URL(this.state.type["apiAdress"], HOME_SERVER),
new URL(this.state.type["apiAdress"], client.baseUrl),
{
method: "GET",
headers: {
Authorization: "Bearer " + ACCESS_TOKEN,
Authorization: "Bearer " + client.getAccessToken(),
},
}
);
@ -480,8 +483,6 @@ class DataToTable extends Component {
data={this.state.rightPanel["data"]}
onClose={this.onClose}
lang={this.props.lang}
token={this.props.token}
server={this.props.server}
isEmailAvailable={this.isEmailAvailable}
refresh={this.onRefresh}
onTabSelected={this.props.onTabSelected}

@ -11,8 +11,8 @@ import MatrixClientContext from "./MatrixClientContext"
import logo from "./images/logo.svg";
class Login extends Component {
constructor(props) {
super(props);
constructor() {
super();
this.state = {
user: "",
password: "",

@ -1,29 +1,35 @@
import React, { Component } from "react";
import Card from "react-bootstrap/Card";
import MatrixClientContext from "./MatrixClientContext";
class Monitoring extends Component {
constructor(props) {
super(props);
this.state = {};
constructor() {
super();
this.state = {
log: null,
list: null,
};
}
static contextType = MatrixClientContext;
componentDidMount() {
this.getLogs();
this.getServerState();
}
getLogs = async () => {
const client = this.context;
let logData;
const HOME_SERVER = this.props.server;
const ACCESS_TOKEN = this.props.token;
try {
const LOG_REQUEST = await fetch(
new URL("_matrix/client/r0/watcha_log", HOME_SERVER),
new URL("_matrix/client/r0/watcha_log", client.baseUrl),
{
method: "GET",
headers: {
Authorization: "Bearer " + ACCESS_TOKEN,
Authorization: "Bearer " + client.getAccessToken(),
},
}
);
@ -38,17 +44,19 @@ class Monitoring extends Component {
};
getServerState = async () => {
const client = this.context;
let serverReport;
const HOME_SERVER = this.props.server;
const ACCESS_TOKEN = this.props.token;
try {
const SERVER_REPORT_REQUET = await fetch(
new URL("_matrix/client/r0/watcha_server_state", HOME_SERVER),
new URL(
"_matrix/client/r0/watcha_server_state",
client.baseUrl
),
{
method: "GET",
headers: {
Authorization: "Bearer " + ACCESS_TOKEN,
Authorization: "Bearer " + client.getAccessToken(),
},
}
);

@ -2,32 +2,37 @@ import React, { Component } from "react";
import { withTranslation } from "react-i18next";
import CardStats from "./CardStats";
import MatrixClientContext from "./MatrixClientContext";
import logo from "./images/logo.svg";
class StatsTab extends Component {
constructor(props) {
super(props);
this.state = {};
constructor() {
super();
this.state = { stats: null };
}
static contextType = MatrixClientContext;
componentDidMount() {
this.getStats();
this.getServerState();
}
getServerState = async () => {
const client = this.context;
let serverReport;
const HOME_SERVER = this.props.server;
const ACCESS_TOKEN = this.props.token;
try {
const SERVER_REPORT_REQUET = await fetch(
new URL("_matrix/client/r0/watcha_server_state", HOME_SERVER),
new URL(
"_matrix/client/r0/watcha_server_state",
client.baseUrl
),
{
method: "GET",
headers: {
Authorization: "Bearer " + ACCESS_TOKEN,
Authorization: "Bearer " + client.getAccessToken(),
},
}
);
@ -40,17 +45,16 @@ class StatsTab extends Component {
};
getStats = async () => {
const client = this.context;
let statsData;
const HOME_SERVER = this.props.server;
const ACCESS_TOKEN = this.props.token;
try {
const STATS_REQUEST = await fetch(
new URL("_matrix/client/r0/watcha_admin_stats", HOME_SERVER),
new URL("_matrix/client/r0/watcha_admin_stats", client.baseUrl),
{
method: "GET",
headers: {
Authorization: "Bearer " + ACCESS_TOKEN,
Authorization: "Bearer " + client.getAccessToken(),
},
}
);

@ -70,8 +70,7 @@ class UserRightPanel extends Component {
onEmailEdit = () => this.setState({ editEmail: !this.state.editEmail });
onEmailValidate = async () => {
const HOME_SERVER = this.props.server;
const ACCESS_TOKEN = this.props.token;
const client = this.context;
try {
const userId = encodeURIComponent(
this.props.data["User name"]["data"]
@ -79,12 +78,12 @@ class UserRightPanel extends Component {
const SERVER_REQUEST = await fetch(
new URL(
`_matrix/client/r0/watcha_update_email/${userId}`,
HOME_SERVER
client.baseUrl
),
{
method: "PUT",
headers: {
Authorization: "Bearer " + ACCESS_TOKEN,
Authorization: "Bearer " + client.getAccessToken(),
Accept: "application/json",
"Content-Type": "application/json",
},
@ -138,8 +137,7 @@ class UserRightPanel extends Component {
};
getUsersAdvancedInfos = async () => {
const HOME_SERVER = this.props.server;
const ACCESS_TOKEN = this.props.token;
const client = this.context;
try {
const userId = encodeURIComponent(
this.props.data["User name"]["data"]
@ -147,12 +145,12 @@ class UserRightPanel extends Component {
const SERVER_REQUEST = await fetch(
new URL(
`_matrix/client/r0/watcha_user_ip/${userId}`,
HOME_SERVER
client.baseUrl
),
{
method: "GET",
headers: {
Authorization: "Bearer " + ACCESS_TOKEN,
Authorization: "Bearer " + client.getAccessToken(),
Accept: "application/json",
"Content-Type": "application/json",
},
@ -180,8 +178,7 @@ class UserRightPanel extends Component {
};
_doResetPassword = async isActivating => {
const HOME_SERVER = this.props.server;
const ACCESS_TOKEN = this.props.token;
const client = this.context;
const { t } = this.props;
// activating is the same as resetting the password,
// but with a different success message
@ -204,12 +201,12 @@ class UserRightPanel extends Component {
const SERVER_REQUEST = await fetch(
new URL(
`_matrix/client/r0/watcha_reset_password/${userId}`,
HOME_SERVER
client.baseUrl
),
{
method: "POST",
headers: {
Authorization: "Bearer " + ACCESS_TOKEN,
Authorization: "Bearer " + client.getAccessToken(),
Accept: "application/json",
"Content-Type": "application/json",
},
@ -339,8 +336,7 @@ class UserRightPanel extends Component {
};
upgradePartner = async () => {
const HOME_SERVER = this.props.server;
const ACCESS_TOKEN = this.props.token;
const client = this.context;
try {
const userId = encodeURIComponent(
this.props.data["User name"]["data"]
@ -348,12 +344,12 @@ class UserRightPanel extends Component {
const SERVER_REQUEST = await fetch(
new URL(
`_matrix/client/r0/watcha_update_partner_to_member/${userId}`,
HOME_SERVER
client.baseUrl
),
{
method: "PUT",
headers: {
Authorization: "Bearer " + ACCESS_TOKEN,
Authorization: "Bearer " + client.getAccessToken(),
Accept: "application/json",
"Content-Type": "application/json",
},

Loading…
Cancel
Save