Add kinds of improvements to event create page
This commit is contained in:
@@ -3,14 +3,68 @@ import Helmet from "react-helmet";
|
||||
import "./EventCreatePage.scss";
|
||||
import { isAuthenticated } from "../../auth";
|
||||
import Form from "react-jsonschema-form";
|
||||
import { Tag, getTags } from "../../models/Tag";
|
||||
import { createEvent } from "../../models/Event";
|
||||
|
||||
export interface EventCreatePageProps {}
|
||||
export interface EventCreatePageProps {
|
||||
history: {
|
||||
push: (to: string) => void;
|
||||
};
|
||||
}
|
||||
export interface EventCreatePageState {
|
||||
tags: Tag[];
|
||||
error?: string;
|
||||
}
|
||||
|
||||
class EventCreatePage extends React.Component<EventCreatePageProps, EventCreatePageState> {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
tags: [],
|
||||
};
|
||||
|
||||
this.fetchTags();
|
||||
}
|
||||
|
||||
fetchTags = async () => {
|
||||
const getTagsPromise = getTags();
|
||||
try {
|
||||
const tags = await getTagsPromise;
|
||||
this.setState({
|
||||
tags,
|
||||
});
|
||||
return getTagsPromise;
|
||||
} catch (err) {
|
||||
this.setState({
|
||||
error: String(err),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onSubmit = async (data) => {
|
||||
console.log("submitted, data:");
|
||||
console.log(data);
|
||||
|
||||
const { history } = this.props;
|
||||
|
||||
try {
|
||||
const payload = data.formData;
|
||||
await createEvent(payload);
|
||||
history.push("/admin/events");
|
||||
} catch (err) {
|
||||
this.setState({
|
||||
error: String(err),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onError = (data) => {
|
||||
console.error("error, data:");
|
||||
console.log(data);
|
||||
}
|
||||
|
||||
render() {
|
||||
const { tags, error } = this.state;
|
||||
const schema = {
|
||||
title: "New Event",
|
||||
type: "object",
|
||||
@@ -18,7 +72,7 @@ class EventCreatePage extends React.Component<EventCreatePageProps, EventCreateP
|
||||
properties: {
|
||||
title: {
|
||||
type: "string",
|
||||
title: "Name",
|
||||
title: "Title",
|
||||
default: ""
|
||||
},
|
||||
location: {
|
||||
@@ -26,17 +80,13 @@ class EventCreatePage extends React.Component<EventCreatePageProps, EventCreateP
|
||||
title: "Location",
|
||||
default: "",
|
||||
},
|
||||
multipleChoicesList: {
|
||||
tags: {
|
||||
type: "array",
|
||||
title: "Event type",
|
||||
title: "Event tags",
|
||||
items: {
|
||||
type: "string",
|
||||
enum: [
|
||||
"Virallinen",
|
||||
"Fuksit",
|
||||
"Opinnot",
|
||||
"Urheilu"
|
||||
]
|
||||
type: "number",
|
||||
enum: tags.map(t => t.id),
|
||||
enumNames: tags.map(t => t.name),
|
||||
},
|
||||
uniqueItems: true
|
||||
},
|
||||
@@ -51,8 +101,9 @@ class EventCreatePage extends React.Component<EventCreatePageProps, EventCreateP
|
||||
<Form schema={schema}
|
||||
idPrefix="rjsf"
|
||||
onChange={() => console.log("changed")}
|
||||
onSubmit={() => console.log("submitted")}
|
||||
onError={() => console.log("error")} />
|
||||
onSubmit={this.onSubmit}
|
||||
onError={this.onError} />
|
||||
{ error && <div className="error">{error}</div> }
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user