// 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} 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"; 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: [], mode: props.location.mode !== undefined ? props.location.mode : "edit", }; } UNSAFE_componentWillMount() { this.getProduct(); this.getPaymentProviders(); } getProduct() { ProductBackend.getProduct("admin", this.state.productName) .then((product) => { this.setState({ product: product, }); }); } getPaymentProviders() { ProviderBackend.getProviders("admin") .then((res) => { this.setState({ providers: res.filter(provider => provider.category === "Payment"), }); }); } parseProductField(key, value) { if ([""].includes(key)) { value = Setting.myParseInt(value); } return value; } updateProductField(key, value) { value = this.parseProductField(key, value); let product = this.state.product; product[key] = value; this.setState({ product: product, }); } renderProduct() { 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: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")}: {this.state.product.image} {Setting.getLabel(i18next.t("product: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("product:Currency"), i18next.t("product:Currency - Tooltip"))} : {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("general:State"), i18next.t("general:State - Tooltip"))} : {Setting.getLabel(i18next.t("general:Preview"), i18next.t("general:Preview - Tooltip"))} : { this.renderPreview() } ) } renderPreview() { let buyUrl = `/products/${this.state.product.name}/buy`; return (

) } submitProductEdit(willExist) { let product = Setting.deepCopy(this.state.product); ProductBackend.updateProduct(this.state.product.owner, this.state.productName, product) .then((res) => { if (res.msg === "") { Setting.showMessage("success", `Successfully saved`); this.setState({ productName: this.state.product.name, }); if (willExist) { this.props.history.push(`/products`); } else { this.props.history.push(`/products/${this.state.product.name}`); } } else { Setting.showMessage("error", res.msg); this.updateProductField('name', this.state.productName); } }) .catch(error => { Setting.showMessage("error", `Failed to connect to server: ${error}`); }); } deleteProduct() { ProductBackend.deleteProduct(this.state.product) .then(() => { this.props.history.push(`/products`); }) .catch(error => { Setting.showMessage("error", `Product failed to delete: ${error}`); }); } render() { return (
{ this.state.product !== null ? this.renderProduct() : null }
{this.state.mode === "add" ? : null}
); } } export default ProductEditPage;