CCM NG: PageModelEditor: Adding new containers now works

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5540 8810af33-2d31-482b-a856-94f89814c4df
ccm-docs
jensp 2018-06-21 17:54:19 +00:00
parent e3e72c0b15
commit 9e22c6aeec
2 changed files with 53 additions and 21 deletions

View File

@ -31,6 +31,8 @@ import java.util.Objects;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
@ -72,6 +74,7 @@ public class ContainerModel implements Serializable {
*/ */
@Id @Id
@Column(name = "CONTAINER_ID") @Column(name = "CONTAINER_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
@XmlElement(name = "container-model-id") @XmlElement(name = "container-model-id")
private long containerId; private long containerId;

View File

@ -513,7 +513,7 @@ interface ContainerListProps {
interface ContainerListState { interface ContainerListState {
errorMsg: string; errorMsg: string;
newContainerName: string; containerName: string;
containers: ContainerModel[]; containers: ContainerModel[];
} }
@ -527,7 +527,7 @@ class ContainerListComponent
this.state = { this.state = {
errorMsg: "", errorMsg: "",
containers: props.containers, containers: props.containers,
newContainerName: "", containerName: "",
} }
this.addContainer = this.addContainer.bind(this); this.addContainer = this.addContainer.bind(this);
@ -543,7 +543,8 @@ class ContainerListComponent
<input id="newContainerName" <input id="newContainerName"
onChange={this.updateNewContainerName} onChange={this.updateNewContainerName}
size={32} size={32}
type="text" /> type="text"
value={this.state.containerName} />
<button type="submit"> <button type="submit">
<span className="fa fa-plus-circle"></span> <span className="fa fa-plus-circle"></span>
Add container Add container
@ -560,20 +561,17 @@ class ContainerListComponent
<li> <li>
<span>{container.key}</span> <span>{container.key}</span>
<button> <button>
<span className="fa fa-arrow-alt-circle-down"> <span className="fa fa-edit">
</span> </span>
Down Rename
</button>
<button>
<span className="fa fa-arrow-alt-circle-up">
</span>
Up
</button> </button>
<button> <button>
<span className="fa fa-minus-circle"></span> <span className="fa fa-minus-circle"></span>
Delete Delete
</button> </button>
</li>)} </li>)
}
}
</ul> </ul>
</div>; </div>;
} }
@ -585,7 +583,7 @@ class ContainerListComponent
this.setState({ this.setState({
...this.state, ...this.state,
newContainerName: target.value, containerName: target.value,
}); });
} }
@ -594,13 +592,28 @@ class ContainerListComponent
event.preventDefault(); event.preventDefault();
if (this.state.newContainerName === null if (this.state.containerName === null
|| this.state.newContainerName === "") { || this.state.containerName === "") {
this.setState({ this.setState({
...this.state, ...this.state,
errorMsg: "A container needs a name!", errorMsg: "A container needs a name!",
}); });
return;
}
if(this.state.containers.findIndex((container: ContainerModel) => {
return container.key === this.state.containerName;
}) >= 0) {
this.setState({
...this.state,
errorMsg: `A container with the key `
+ `"${this.state.containerName}" already exists.`,
});
return;
} }
const headers: Headers = new Headers(); const headers: Headers = new Headers();
@ -618,7 +631,7 @@ class ContainerListComponent
+ `/page-models/${this.props.ccmApplication}/` + `/page-models/${this.props.ccmApplication}/`
+ `${this.props.pageModelName}` + `${this.props.pageModelName}`
+ `/containers/` + `/containers/`
+ `${this.state.newContainerName}`; + `${this.state.containerName}`;
fetch(url, init) fetch(url, init)
.then((response: Response) => { .then((response: Response) => {
@ -627,12 +640,28 @@ class ContainerListComponent
response response
.json() .json()
.then((newContainer) => { .then((newContainer) => {
const containers = [
...this.state.containers,
newContainer,
];
containers.sort((container1, container2) => {
const key1: string = container1.key;
const key2: string = container2.key;
if (key1 < key2) {
return -1;
} else if(key1 > key2) {
return 1;
} else {
return 0;
}
});
this.setState({ this.setState({
...this.state, ...this.state,
containers: [ containers,
...this.state.containers, containerName: "",
newContainer],
newContainerName: "",
}); });
}) })
.catch((error) => { .catch((error) => {