// 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 ProductBackend from "./backend/ProductBackend";
import * as Setting from "./Setting";
import i18next from "i18next";
import {LinkOutlined} from "@ant-design/icons";
import * as ProviderBackend from "./backend/ProviderBackend";
import ProductBuyPage from "./ProductBuyPage";
import * as OrganizationBackend from "./backend/OrganizationBackend";
const {Option} = Select;
class ProductEditPage extends React.Component {
constructor(props) {
super(props);
this.state = {
classes: props,
organizationName: props.organizationName !== undefined ? props.organizationName : props.match.params.organizationName,
productName: props.match.params.productName,
product: null,
providers: [],
organizations: [],
mode: props.location.mode !== undefined ? props.location.mode : "edit",
};
}
UNSAFE_componentWillMount() {
this.getProduct();
this.getOrganizations();
this.getPaymentProviders(this.state.organizationName);
}
getProduct() {
ProductBackend.getProduct(this.state.organizationName, this.state.productName)
.then((res) => {
if (res.data === null) {
this.props.history.push("/404");
return;
}
this.setState({
product: res.data,
});
});
}
getOrganizations() {
OrganizationBackend.getOrganizations("admin")
.then((res) => {
this.setState({
organizations: res.data || [],
});
});
}
getPaymentProviders(organizationName) {
ProviderBackend.getProviders(organizationName)
.then((res) => {
if (res.status === "ok") {
this.setState({
providers: res.data.filter(provider => provider.category === "Payment"),
});
} else {
Setting.showMessage("error", res.msg);
}
});
}
parseProductField(key, value) {
if ([""].includes(key)) {
value = Setting.myParseInt(value);
}
return value;
}
updateProductField(key, value) {
value = this.parseProductField(key, value);
const product = this.state.product;
product[key] = value;
this.setState({
product: product,
});
}
renderProduct() {
const isCreatedByPlan = this.state.product.tag === "auto_created_product_for_plan";
return (
{this.state.mode === "add" ? i18next.t("product:New Product") : i18next.t("product:Edit Product")}
{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.updateProductField("name", e.target.value);
}} />
{Setting.getLabel(i18next.t("general:Display name"), i18next.t("general:Display name - Tooltip"))} :
{
this.updateProductField("displayName", e.target.value);
}} />
{Setting.getLabel(i18next.t("product:Image"), i18next.t("product:Image - Tooltip"))} :
{Setting.getLabel(i18next.t("general:URL"), i18next.t("general:URL - Tooltip"))} :
} value={this.state.product.image} onChange={e => {
this.updateProductField("image", e.target.value);
}} />
{i18next.t("general:Preview")}:
{Setting.getLabel(i18next.t("user:Tag"), i18next.t("product:Tag - Tooltip"))} :
{
this.updateProductField("tag", e.target.value);
}} />
{Setting.getLabel(i18next.t("product:Detail"), i18next.t("product:Detail - Tooltip"))} :
{
this.updateProductField("detail", e.target.value);
}} />
{Setting.getLabel(i18next.t("general:Description"), i18next.t("general:Description - Tooltip"))} :
{
this.updateProductField("description", e.target.value);
}} />
{Setting.getLabel(i18next.t("payment:Currency"), i18next.t("payment:Currency - Tooltip"))} :
{Setting.getLabel(i18next.t("product:Is recharge"), i18next.t("product:Is recharge - Tooltip"))} :
{
this.updateProductField("isRecharge", value);
}} />
{
this.state.product.isRecharge ? null : (
{Setting.getLabel(i18next.t("product:Price"), i18next.t("product:Price - Tooltip"))} :
{
this.updateProductField("price", value);
}} />
)}
{Setting.getLabel(i18next.t("product:Quantity"), i18next.t("product:Quantity - Tooltip"))} :
{
this.updateProductField("quantity", value);
}} />
{Setting.getLabel(i18next.t("product:Sold"), i18next.t("product:Sold - Tooltip"))} :
{
this.updateProductField("sold", value);
}} />
{Setting.getLabel(i18next.t("product:Payment providers"), i18next.t("product:Payment providers - Tooltip"))} :
{Setting.getLabel(i18next.t("product:Return URL"), i18next.t("product:Return URL - Tooltip"))} :
} value={this.state.product.returnUrl} onChange={e => {
this.updateProductField("returnUrl", e.target.value);
}} />
{Setting.getLabel(i18next.t("product:Success URL"), i18next.t("product:Success URL - Tooltip"))} :
} value={this.state.product.successUrl} onChange={e => {
this.updateProductField("successUrl", e.target.value);
}} />
{Setting.getLabel(i18next.t("general:State"), i18next.t("general:State - Tooltip"))} :
{Setting.getLabel(i18next.t("general:Preview"), i18next.t("general:Preview - Tooltip"))} :
{
this.renderPreview()
}
);
}
renderPreview() {
const buyUrl = `/products/${this.state.product.owner}/${this.state.product.name}/buy`;
return (
);
}
submitProductEdit(exitAfterSave) {
const product = Setting.deepCopy(this.state.product);
ProductBackend.updateProduct(this.state.organizationName, this.state.productName, product)
.then((res) => {
if (res.status === "ok") {
Setting.showMessage("success", i18next.t("general:Successfully saved"));
this.setState({
productName: this.state.product.name,
});
if (exitAfterSave) {
this.props.history.push("/products");
} else {
this.props.history.push(`/products/${this.state.product.owner}/${this.state.product.name}`);
}
} else {
Setting.showMessage("error", `${i18next.t("general:Failed to save")}: ${res.msg}`);
this.updateProductField("name", this.state.productName);
}
})
.catch(error => {
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`);
});
}
deleteProduct() {
ProductBackend.deleteProduct(this.state.product)
.then((res) => {
if (res.status === "ok") {
this.props.history.push("/products");
} 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.product !== null ? this.renderProduct() : null
}
{this.state.mode === "add" ? : null}
);
}
}
export default ProductEditPage;