Skip to content

Commit 02469a3

Browse files
authored
feat: support Form.Tag and remove unique formType checks (casibase#1634)
1 parent 452299a commit 02469a3

File tree

6 files changed

+40
-57
lines changed

6 files changed

+40
-57
lines changed

object/form.go

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -38,42 +38,12 @@ type Form struct {
3838
Position string `xorm:"varchar(100)" json:"position"`
3939
Category string `xorm:"varchar(100)" json:"category"`
4040
Type string `xorm:"varchar(100)" json:"type"`
41+
Tag string `xorm:"varchar(100)" json:"tag"`
4142
Url string `xorm:"varchar(100)" json:"url"`
4243

4344
FormItems []*FormItem `xorm:"varchar(5000)" json:"formItems"`
4445
}
4546

46-
func getFormByType(formType string) ([]*Form, error) {
47-
forms := []*Form{}
48-
err := adapter.engine.Desc("created_time").Find(&forms, &Form{Type: formType})
49-
if err != nil {
50-
return forms, err
51-
}
52-
53-
return forms, nil
54-
}
55-
56-
func checkFormTypeExists(formType string, excludeId string) (bool, error) {
57-
forms, err := getFormByType(formType)
58-
if err != nil {
59-
return false, err
60-
}
61-
62-
if len(forms) == 0 {
63-
return false, nil
64-
}
65-
66-
if excludeId != "" {
67-
for _, form := range forms {
68-
if form.GetId() != excludeId {
69-
return true, nil
70-
}
71-
}
72-
return false, nil
73-
}
74-
return true, nil
75-
}
76-
7747
func GetMaskedForm(form *Form, isMaskEnabled bool) *Form {
7848
if !isMaskEnabled {
7949
return form
@@ -149,16 +119,6 @@ func UpdateForm(id string, form *Form) (bool, error) {
149119
return false, nil
150120
}
151121

152-
if form.Type != "" && form.Type != existingForm.Type {
153-
exists, err := checkFormTypeExists(form.Type, id)
154-
if err != nil {
155-
return false, err
156-
}
157-
if exists {
158-
return false, fmt.Errorf("form type %s already exists", form.Type)
159-
}
160-
}
161-
162122
_, err = adapter.engine.ID(core.PK{owner, name}).AllCols().Update(form)
163123
if err != nil {
164124
return false, err
@@ -169,16 +129,6 @@ func UpdateForm(id string, form *Form) (bool, error) {
169129
}
170130

171131
func AddForm(form *Form) (bool, error) {
172-
if form.Type != "" {
173-
exists, err := checkFormTypeExists(form.Type, "")
174-
if err != nil {
175-
return false, err
176-
}
177-
if exists {
178-
return false, fmt.Errorf("form type %s already exists", form.Type)
179-
}
180-
}
181-
182132
affected, err := adapter.engine.Insert(form)
183133
if err != nil {
184134
return false, err

web/src/BaseListPage.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,29 @@ class BaseListPage extends React.Component {
7070
UNSAFE_componentWillMount() {
7171
const {pagination} = this.state;
7272
this.fetch({pagination});
73-
FormBackend.getForm(this.props.account.name, this.props.match?.path?.replace(/^\//, ""))
73+
this.getForm();
74+
}
75+
getForm() {
76+
const tag = this.props.account.tag;
77+
const formType = this.props.match?.path?.replace(/^\//, "");
78+
let formName = formType;
79+
if (tag !== "") {
80+
formName = formType + "-tag-" + tag;
81+
FormBackend.getForm(this.props.account.owner, formName)
82+
.then(res => {
83+
if (res.status === "ok" && res.data) {
84+
this.setState({formItems: res.data.formItems});
85+
} else {
86+
this.fetchFormWithoutTag(formType);
87+
}
88+
});
89+
} else {
90+
this.fetchFormWithoutTag(formType);
91+
}
92+
}
93+
94+
fetchFormWithoutTag(formName) {
95+
FormBackend.getForm(this.props.account.owner, formName)
7496
.then(res => {
7597
if (res.status === "ok" && res.data) {
7698
this.setState({formItems: res.data.formItems});

web/src/FormDataPage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class FormDataPage extends Component {
3434
}
3535

3636
getForm() {
37-
FormBackend.getForm(this.props.account.name, this.state.formName)
37+
FormBackend.getForm(this.props.account.owner, this.state.formName)
3838
.then((res) => {
3939
if (res.status === "ok") {
4040
this.setState({

web/src/FormDataTablePage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class FormDataPage extends BaseListPage {
4343
}
4444

4545
getForm() {
46-
FormBackend.getForm(this.props.account.name, this.state.formName)
46+
FormBackend.getForm(this.props.account.owner, this.state.formName)
4747
.then((res) => {
4848
if (res.status === "ok") {
4949
this.setState({

web/src/FormEditPage.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class FormEditPage extends React.Component {
4747
}
4848

4949
getForm() {
50-
FormBackend.getForm(this.props.account.name, this.state.formName)
50+
FormBackend.getForm(this.props.account.owner, this.state.formName)
5151
.then((res) => {
5252
if (res.status === "ok") {
5353
this.setState({
@@ -187,6 +187,17 @@ class FormEditPage extends React.Component {
187187
</Select>
188188
</Col>
189189
</Row>
190+
<Row style={{marginTop: "20px"}}>
191+
<Col style={{marginTop: "5px"}} span={Setting.isMobile() ? 22 : 2}>
192+
{Setting.getLabel(i18next.t("general:Tag"), i18next.t("general:Tag - Tooltip"))} :
193+
</Col>
194+
<Col span={22}>
195+
<Input value={this.state.form.tag} onChange={e => {
196+
this.updateFormField("tag", e.target.value);
197+
this.updateFormField("name", e.target.value ? `${this.state.form.type}-tag-${e.target.value}` : this.state.form.type);
198+
}} />
199+
</Col>
200+
</Row>
190201
<Row style={{marginTop: "20px"}}>
191202
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
192203
{Setting.getLabel(i18next.t("form:Form items"), i18next.t("form:Form items - Tooltip"))} :

web/src/FormListPage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class FormListPage extends BaseListPage {
3232
newForm() {
3333
const randomName = Setting.getRandomName();
3434
return {
35-
owner: this.props.account.name,
35+
owner: this.props.account.owner,
3636
name: `form_${randomName}`,
3737
createdTime: moment().format(),
3838
displayName: `New Form - ${randomName}`,
@@ -256,7 +256,7 @@ class FormListPage extends BaseListPage {
256256
const field = params.searchedColumn, value = params.searchText;
257257
const sortField = params.sortField, sortOrder = params.sortOrder;
258258
this.setState({loading: true});
259-
FormBackend.getForms(this.props.account.name, params.pagination.current, params.pagination.pageSize, field, value, sortField, sortOrder)
259+
FormBackend.getForms(this.props.account.owner, params.pagination.current, params.pagination.pageSize, field, value, sortField, sortOrder)
260260
.then((res) => {
261261
this.setState({
262262
loading: false,

0 commit comments

Comments
 (0)