// Copyright 2022 The Casdoor Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. import React from "react"; import {Button, Card, Col, Input, InputNumber, Row, Select, Switch} from "antd"; import * as AdapterBackend from "./backend/AdapterBackend"; import * as OrganizationBackend from "./backend/OrganizationBackend"; import * as Setting from "./Setting"; import i18next from "i18next"; const {Option} = Select; class AdapterEditPage extends React.Component { constructor(props) { super(props); this.state = { classes: props, organizationName: props.organizationName !== undefined ? props.organizationName : props.match.params.organizationName, adapterName: props.match.params.adapterName, adapter: null, organizations: [], mode: props.location.mode !== undefined ? props.location.mode : "edit", }; } UNSAFE_componentWillMount() { this.getAdapter(); this.getOrganizations(); } getAdapter() { AdapterBackend.getAdapter(this.state.organizationName, this.state.adapterName) .then((res) => { if (res.status === "ok") { if (res.data === null) { this.props.history.push("/404"); return; } this.setState({ adapter: res.data, }); } }); } getOrganizations() { OrganizationBackend.getOrganizations("admin") .then((res) => { this.setState({ organizations: res.data || [], }); }); } parseAdapterField(key, value) { // if ([].includes(key)) { // value = Setting.myParseInt(value); // } return value; } updateAdapterField(key, value) { value = this.parseAdapterField(key, value); const adapter = this.state.adapter; adapter[key] = value; this.setState({ adapter: adapter, }); } renderAdapter() { return ( {this.state.mode === "add" ? i18next.t("adapter:New Adapter") : i18next.t("adapter:Edit Adapter")}     {this.state.mode === "add" ? : null} } style={(Setting.isMobile()) ? {margin: "5px"} : {}} type="inner"> {Setting.getLabel(i18next.t("general:Organization"), i18next.t("general:Organization - Tooltip"))} : {Setting.getLabel(i18next.t("general:Name"), i18next.t("general:Name - Tooltip"))} : { this.updateAdapterField("name", e.target.value); }} /> {Setting.getLabel(i18next.t("syncer:Table"), i18next.t("syncer:Table - Tooltip"))} : { this.updateAdapterField("table", e.target.value); }} /> {Setting.getLabel(i18next.t("adapter:Use same DB"), i18next.t("adapter:Use same DB - Tooltip"))} : { this.updateAdapterField("useSameDb", checked); if (checked) { this.updateAdapterField("type", ""); this.updateAdapterField("databaseType", ""); this.updateAdapterField("host", ""); this.updateAdapterField("port", 0); this.updateAdapterField("user", ""); this.updateAdapterField("password", ""); this.updateAdapterField("database", ""); } else { this.updateAdapterField("type", "Database"); this.updateAdapterField("databaseType", "mysql"); this.updateAdapterField("host", "localhost"); this.updateAdapterField("port", 3306); this.updateAdapterField("user", "root"); this.updateAdapterField("password", "123456"); this.updateAdapterField("database", "dbName"); } }} /> { (this.state.adapter.useSameDb || Setting.builtInObject(this.state.adapter)) ? null : ( {Setting.getLabel(i18next.t("provider:Type"), i18next.t("provider:Type - Tooltip"))} : {Setting.getLabel(i18next.t("syncer:Database type"), i18next.t("syncer:Database type - Tooltip"))} : {Setting.getLabel(i18next.t("provider:Host"), i18next.t("provider:Host - Tooltip"))} : { this.updateAdapterField("host", e.target.value); }} /> {Setting.getLabel(i18next.t("provider:Port"), i18next.t("provider:Port - Tooltip"))} : { this.updateAdapterField("port", value); }} /> {Setting.getLabel(i18next.t("general:User"), i18next.t("general:User - Tooltip"))} : { this.updateAdapterField("user", e.target.value); }} /> {Setting.getLabel(i18next.t("general:Password"), i18next.t("general:Password - Tooltip"))} : { this.updateAdapterField("password", e.target.value); }} /> {Setting.getLabel(i18next.t("syncer:Database"), i18next.t("syncer:Database - Tooltip"))} : { this.updateAdapterField("database", e.target.value); }} /> ) } {Setting.getLabel(i18next.t("provider:DB test"), i18next.t("provider:DB test - Tooltip"))} : ); } submitAdapterEdit(exitAfterSave) { const adapter = Setting.deepCopy(this.state.adapter); AdapterBackend.updateAdapter(this.state.organizationName, this.state.adapterName, adapter) .then((res) => { if (res.status === "ok") { Setting.showMessage("success", i18next.t("general:Successfully saved")); this.setState({ organizationName: this.state.adapter.owner, adapterName: this.state.adapter.name, }); if (exitAfterSave) { this.props.history.push("/adapters"); } else { this.props.history.push(`/adapters/${this.state.adapter.owner}/${this.state.adapter.name}`); } } else { Setting.showMessage("error", `${i18next.t("general:Failed to save")}: ${res.msg}`); this.updateAdapterField("name", this.state.adapterName); } }) .catch(error => { Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`); }); } deleteAdapter() { AdapterBackend.deleteAdapter(this.state.adapter) .then((res) => { if (res.status === "ok") { this.props.history.push("/adapters"); } else { Setting.showMessage("error", `${i18next.t("general:Failed to delete")}: ${res.msg}`); } }) .catch(error => { Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`); }); } render() { return (
{ this.state.adapter !== null ? this.renderAdapter() : null }
{this.state.mode === "add" ? : null}
); } } export default AdapterEditPage;