Files
casdoor/web/src/ProductEditPage.js

358 lines
14 KiB
JavaScript
Raw Normal View History

2022-02-27 18:20:58 +08:00
// 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";
2022-02-27 18:20:58 +08:00
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";
2022-02-27 23:50:35 +08:00
import ProductBuyPage from "./ProductBuyPage";
2022-02-27 18:20:58 +08:00
const {Option} = Select;
2022-02-27 18:20:58 +08:00
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: [],
2022-02-27 18:20:58 +08:00
mode: props.location.mode !== undefined ? props.location.mode : "edit",
};
}
UNSAFE_componentWillMount() {
this.getProduct();
this.getPaymentProviders();
}
getProduct() {
ProductBackend.getProduct(this.props.account.owner, this.state.productName)
2022-02-27 18:20:58 +08:00
.then((product) => {
if (product === null) {
this.props.history.push("/404");
return;
}
2022-02-27 18:20:58 +08:00
this.setState({
product: product,
});
});
}
getPaymentProviders() {
ProviderBackend.getProviders(this.props.account.owner)
2022-02-27 18:20:58 +08:00
.then((res) => {
2023-06-02 11:49:38 +08:00
if (res.status === "ok") {
this.setState({
providers: res.data.filter(provider => provider.category === "Payment"),
});
} else {
Setting.showMessage("error", res.msg);
}
2022-02-27 18:20:58 +08:00
});
}
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;
2022-02-27 18:20:58 +08:00
product[key] = value;
this.setState({
product: product,
});
}
renderProduct() {
return (
<Card size="small" title={
<div>
{this.state.mode === "add" ? i18next.t("product:New Product") : i18next.t("product:Edit Product")}&nbsp;&nbsp;&nbsp;&nbsp;
<Button onClick={() => this.submitProductEdit(false)}>{i18next.t("general:Save")}</Button>
<Button style={{marginLeft: "20px"}} type="primary" onClick={() => this.submitProductEdit(true)}>{i18next.t("general:Save & Exit")}</Button>
{this.state.mode === "add" ? <Button style={{marginLeft: "20px"}} onClick={() => this.deleteProduct()}>{i18next.t("general:Cancel")}</Button> : null}
2022-02-27 18:20:58 +08:00
</div>
2022-08-06 23:43:09 +08:00
} style={(Setting.isMobile()) ? {margin: "5px"} : {}} type="inner">
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
2022-02-27 18:20:58 +08:00
{Setting.getLabel(i18next.t("general:Name"), i18next.t("general:Name - Tooltip"))} :
</Col>
<Col span={22} >
<Input value={this.state.product.name} onChange={e => {
this.updateProductField("name", e.target.value);
2022-02-27 18:20:58 +08:00
}} />
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
2022-02-27 18:20:58 +08:00
{Setting.getLabel(i18next.t("general:Display name"), i18next.t("general:Display name - Tooltip"))} :
</Col>
<Col span={22} >
<Input value={this.state.product.displayName} onChange={e => {
this.updateProductField("displayName", e.target.value);
2022-02-27 18:20:58 +08:00
}} />
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
{Setting.getLabel(i18next.t("general:Organization"), i18next.t("general:Organization - Tooltip"))} :
</Col>
<Col span={22} >
<Select virtual={false} style={{width: "100%"}} disabled={!Setting.isAdminUser(this.props.account)} value={this.state.product.owner} onChange={(value => {this.updateProductField("owner", value);})}>
{
this.state.organizations.map((organization, index) => <Option key={index} value={organization.name}>{organization.name}</Option>)
}
</Select>
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
2022-02-27 18:20:58 +08:00
{Setting.getLabel(i18next.t("product:Image"), i18next.t("product:Image - Tooltip"))} :
</Col>
2022-08-06 23:43:09 +08:00
<Col span={22} style={(Setting.isMobile()) ? {maxWidth: "100%"} : {}}>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 1}>
2022-02-27 18:20:58 +08:00
{Setting.getLabel(i18next.t("general:URL"), i18next.t("general:URL - Tooltip"))} :
</Col>
<Col span={23} >
<Input prefix={<LinkOutlined />} value={this.state.product.image} onChange={e => {
this.updateProductField("image", e.target.value);
2022-02-27 18:20:58 +08:00
}} />
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 1}>
2022-02-27 18:20:58 +08:00
{i18next.t("general:Preview")}:
</Col>
<Col span={23} >
<a target="_blank" rel="noreferrer" href={this.state.product.image}>
<img src={this.state.product.image} alt={this.state.product.image} height={90} style={{marginBottom: "20px"}} />
2022-02-27 18:20:58 +08:00
</a>
</Col>
</Row>
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
2023-03-19 01:01:39 +08:00
{Setting.getLabel(i18next.t("user:Tag"), i18next.t("product:Tag - Tooltip"))} :
2022-02-27 18:20:58 +08:00
</Col>
<Col span={22} >
<Input value={this.state.product.tag} onChange={e => {
this.updateProductField("tag", e.target.value);
2022-02-27 18:20:58 +08:00
}} />
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
2022-02-27 23:50:35 +08:00
{Setting.getLabel(i18next.t("product:Detail"), i18next.t("product:Detail - Tooltip"))} :
</Col>
<Col span={22} >
<Input value={this.state.product.detail} onChange={e => {
this.updateProductField("detail", e.target.value);
2022-02-27 23:50:35 +08:00
}} />
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
2022-12-16 23:35:30 +08:00
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
2023-03-19 01:01:39 +08:00
{Setting.getLabel(i18next.t("general:Description"), i18next.t("general:Description - Tooltip"))} :
2022-12-16 23:35:30 +08:00
</Col>
<Col span={22} >
<Input value={this.state.product.description} onChange={e => {
this.updateProductField("description", e.target.value);
}} />
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
2023-03-18 10:16:35 +08:00
{Setting.getLabel(i18next.t("payment:Currency"), i18next.t("payment:Currency - Tooltip"))} :
2022-02-27 18:20:58 +08:00
</Col>
<Col span={22} >
<Select virtual={false} style={{width: "100%"}} value={this.state.product.currency} onChange={(value => {
this.updateProductField("currency", value);
2022-02-27 18:20:58 +08:00
})}>
{
[
{id: "USD", name: "USD"},
{id: "CNY", name: "CNY"},
2022-02-27 18:20:58 +08:00
].map((item, index) => <Option key={index} value={item.id}>{item.name}</Option>)
}
</Select>
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
2022-02-27 18:20:58 +08:00
{Setting.getLabel(i18next.t("product:Price"), i18next.t("product:Price - Tooltip"))} :
</Col>
<Col span={22} >
<InputNumber value={this.state.product.price} onChange={value => {
this.updateProductField("price", value);
2022-02-27 18:20:58 +08:00
}} />
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
2022-02-27 18:20:58 +08:00
{Setting.getLabel(i18next.t("product:Quantity"), i18next.t("product:Quantity - Tooltip"))} :
</Col>
<Col span={22} >
<InputNumber value={this.state.product.quantity} onChange={value => {
this.updateProductField("quantity", value);
2022-02-27 18:20:58 +08:00
}} />
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
2022-02-27 18:20:58 +08:00
{Setting.getLabel(i18next.t("product:Sold"), i18next.t("product:Sold - Tooltip"))} :
</Col>
<Col span={22} >
<InputNumber value={this.state.product.sold} onChange={value => {
this.updateProductField("sold", value);
2022-02-27 18:20:58 +08:00
}} />
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
2022-02-27 18:20:58 +08:00
{Setting.getLabel(i18next.t("product:Payment providers"), i18next.t("product:Payment providers - Tooltip"))} :
</Col>
<Col span={22} >
2023-05-31 17:36:11 +08:00
<Select virtual={false} mode="multiple" style={{width: "100%"}} value={this.state.product.providers} onChange={(value => {this.updateProductField("providers", value);})}>
2022-02-27 18:20:58 +08:00
{
this.state.providers.map((provider, index) => <Option key={index} value={provider.name}>{provider.name}</Option>)
}
</Select>
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
2022-03-13 16:25:54 +08:00
{Setting.getLabel(i18next.t("product:Return URL"), i18next.t("product:Return URL - Tooltip"))} :
</Col>
<Col span={22} >
<Input prefix={<LinkOutlined />} value={this.state.product.returnUrl} onChange={e => {
this.updateProductField("returnUrl", e.target.value);
2022-03-13 16:25:54 +08:00
}} />
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
2022-02-27 18:20:58 +08:00
{Setting.getLabel(i18next.t("general:State"), i18next.t("general:State - Tooltip"))} :
</Col>
<Col span={22} >
<Select virtual={false} style={{width: "100%"}} value={this.state.product.state} onChange={(value => {
this.updateProductField("state", value);
2022-02-27 18:20:58 +08:00
})}>
{
[
{id: "Published", name: "Published"},
{id: "Draft", name: "Draft"},
2022-02-27 18:20:58 +08:00
].map((item, index) => <Option key={index} value={item.id}>{item.name}</Option>)
}
</Select>
</Col>
</Row>
<Row style={{marginTop: "20px"}} >
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
2022-02-27 23:50:35 +08:00
{Setting.getLabel(i18next.t("general:Preview"), i18next.t("general:Preview - Tooltip"))} :
</Col>
{
this.renderPreview()
}
</Row>
2022-02-27 18:20:58 +08:00
</Card>
);
2022-02-27 18:20:58 +08:00
}
2022-02-27 23:50:35 +08:00
renderPreview() {
const buyUrl = `/products/${this.state.product.name}/buy`;
2022-02-27 23:50:35 +08:00
return (
<Col span={22} style={{display: "flex", flexDirection: "column"}}>
<a style={{marginBottom: "10px", display: "flex"}} target="_blank" rel="noreferrer" href={buyUrl}>
<Button type="primary">{i18next.t("product:Test buy page..")}</Button>
</a>
<br />
<br />
2022-02-27 23:50:35 +08:00
<div style={{width: "90%", border: "1px solid rgb(217,217,217)", boxShadow: "10px 10px 5px #888888", alignItems: "center", overflow: "auto", flexDirection: "column", flex: "auto"}}>
<ProductBuyPage product={this.state.product} />
</div>
</Col>
);
2022-02-27 23:50:35 +08:00
}
2022-02-27 18:20:58 +08:00
submitProductEdit(willExist) {
const product = Setting.deepCopy(this.state.product);
2022-02-27 18:20:58 +08:00
ProductBackend.updateProduct(this.state.product.owner, this.state.productName, product)
.then((res) => {
if (res.status === "ok") {
Setting.showMessage("success", i18next.t("general:Successfully saved"));
2022-02-27 18:20:58 +08:00
this.setState({
productName: this.state.product.name,
});
if (willExist) {
this.props.history.push("/products");
2022-02-27 18:20:58 +08:00
} else {
this.props.history.push(`/products/${this.state.product.name}`);
}
} else {
Setting.showMessage("error", `${i18next.t("general:Failed to save")}: ${res.msg}`);
this.updateProductField("name", this.state.productName);
2022-02-27 18:20:58 +08:00
}
})
.catch(error => {
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`);
2022-02-27 18:20:58 +08:00
});
}
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}`);
}
2022-02-27 18:20:58 +08:00
})
.catch(error => {
Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`);
2022-02-27 18:20:58 +08:00
});
}
render() {
return (
<div>
{
this.state.product !== null ? this.renderProduct() : null
}
<div style={{marginTop: "20px", marginLeft: "40px"}}>
2022-02-27 18:20:58 +08:00
<Button size="large" onClick={() => this.submitProductEdit(false)}>{i18next.t("general:Save")}</Button>
<Button style={{marginLeft: "20px"}} type="primary" size="large" onClick={() => this.submitProductEdit(true)}>{i18next.t("general:Save & Exit")}</Button>
{this.state.mode === "add" ? <Button style={{marginLeft: "20px"}} size="large" onClick={() => this.deleteProduct()}>{i18next.t("general:Cancel")}</Button> : null}
2022-02-27 18:20:58 +08:00
</div>
</div>
);
}
}
export default ProductEditPage;