Admin event select signup form
This commit is contained in:
+25
-9
@@ -9,6 +9,20 @@ export interface Signup {
|
||||
list_name: string;
|
||||
}
|
||||
|
||||
export async function getSignup(id: number): Promise<Signup> {
|
||||
try {
|
||||
const resp = await axios.get(`${url}${id}`, {
|
||||
headers: {
|
||||
"Authorization": getAuthHeader()
|
||||
},
|
||||
});
|
||||
return resp.data;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function createSignup(data: Signup): Promise<Signup> {
|
||||
try {
|
||||
const resp = await axios.post(url, data);
|
||||
@@ -19,12 +33,14 @@ export async function createSignup(data: Signup): Promise<Signup> {
|
||||
}
|
||||
}
|
||||
|
||||
// export async function updateSignup(data: Signup): Promise<Signup> {
|
||||
// try {
|
||||
// const resp = await axios.post(url, data);
|
||||
// return resp.data;
|
||||
// } catch (err) {
|
||||
// console.error(err);
|
||||
// throw err;
|
||||
// }
|
||||
// }
|
||||
export async function updateSignup(data: Signup): Promise<Signup> {
|
||||
try {
|
||||
const { id } = data;
|
||||
if (!id) throw new Error("SignupId required!");
|
||||
const resp = await axios.put(`${url}${id}`, data);
|
||||
return resp.data;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import "./EventCreatePage.scss";
|
||||
import { isAuthenticated } from "../../auth";
|
||||
import Form from "react-jsonschema-form";
|
||||
import { Tag, getTags } from "../../models/Tag";
|
||||
import { SignupForm, getForms } from "../../models/SignupForm";
|
||||
import { createEvent, getEvent, updateEvent, Event } from "../../models/Event";
|
||||
import DatetimeWidget from "../../components/DatetimeWidget";
|
||||
import SectionDividerWidget from "../../components/SectionDividerWidget";
|
||||
@@ -27,6 +28,7 @@ export interface EventCreatePageProps {
|
||||
}
|
||||
export interface EventCreatePageState {
|
||||
tags: Tag[];
|
||||
signupForm: SignupForm[];
|
||||
error?: string;
|
||||
statusMessage?: string;
|
||||
formData: any;
|
||||
@@ -37,10 +39,12 @@ class EventCreatePage extends React.Component<EventCreatePageProps, EventCreateP
|
||||
super(props);
|
||||
this.state = {
|
||||
tags: [],
|
||||
signupForm: [],
|
||||
formData: {},
|
||||
};
|
||||
|
||||
this.fetchTags();
|
||||
this.fetchSignupForms();
|
||||
|
||||
const { id } = props.match.params;
|
||||
if (id !== undefined) {
|
||||
@@ -52,6 +56,7 @@ class EventCreatePage extends React.Component<EventCreatePageProps, EventCreateP
|
||||
try {
|
||||
const data = await getEvent(id);
|
||||
data.tags = data.tag_id as any;
|
||||
data.signupForm = data.signup_id as any;
|
||||
this.setState({
|
||||
formData: data,
|
||||
});
|
||||
@@ -63,33 +68,49 @@ class EventCreatePage extends React.Component<EventCreatePageProps, EventCreateP
|
||||
}
|
||||
|
||||
fetchTags = async () => {
|
||||
const getTagsPromise = getTags();
|
||||
try {
|
||||
const tags = await getTagsPromise;
|
||||
const tags = await getTags();
|
||||
this.setState({
|
||||
tags,
|
||||
});
|
||||
return getTagsPromise;
|
||||
return tags;
|
||||
} catch (err) {
|
||||
this.setState({
|
||||
error: String(err),
|
||||
});
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
fetchSignupForms = async () => {
|
||||
try {
|
||||
const signupForm = await getForms();
|
||||
this.setState({
|
||||
signupForm
|
||||
})
|
||||
return signupForm;
|
||||
} catch (err) {
|
||||
this.setState({
|
||||
error: String(err),
|
||||
});
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
onSubmit = async (data) => {
|
||||
console.log("submitted, data:");
|
||||
console.log(data);
|
||||
console.log(data.formData);
|
||||
|
||||
const { history } = this.props;
|
||||
|
||||
try {
|
||||
const payload = data.formData;
|
||||
payload.signup_id = [];
|
||||
payload.signup_id = payload.signupForm;
|
||||
payload.tag_id = payload.tags;
|
||||
if (payload.id === undefined) {
|
||||
const resp = await createEvent(payload);
|
||||
resp.tags = resp.tag_id as any;
|
||||
resp.signupForm = resp.signup_id as any;
|
||||
this.setState({
|
||||
formData: resp,
|
||||
statusMessage: "Event created successfully",
|
||||
@@ -97,6 +118,7 @@ class EventCreatePage extends React.Component<EventCreatePageProps, EventCreateP
|
||||
} else {
|
||||
const resp = await updateEvent(payload);
|
||||
resp.tags = resp.tag_id as any;
|
||||
resp.signupForm = resp.signup_id as any;
|
||||
this.setState({
|
||||
formData: resp,
|
||||
statusMessage: "Event updated successfully.",
|
||||
@@ -127,7 +149,7 @@ class EventCreatePage extends React.Component<EventCreatePageProps, EventCreateP
|
||||
}
|
||||
|
||||
buildSchema = () => {
|
||||
const { tags, error } = this.state;
|
||||
const { tags, signupForm, error } = this.state;
|
||||
|
||||
const formData = this.state.formData as Event;
|
||||
|
||||
@@ -173,6 +195,17 @@ class EventCreatePage extends React.Component<EventCreatePageProps, EventCreateP
|
||||
title: "End time",
|
||||
default: tomorrowDatetime,
|
||||
},
|
||||
signupForm: {
|
||||
type: "array",
|
||||
title: "Signup forms",
|
||||
items: {
|
||||
type: "number",
|
||||
enum: signupForm.map(form => form.id),
|
||||
enumNames: signupForm.map(form => form.title),
|
||||
},
|
||||
uniqueItems: true,
|
||||
default: [],
|
||||
},
|
||||
finnish_section_divider: {
|
||||
title: "Finnish",
|
||||
type: "string",
|
||||
@@ -263,11 +296,11 @@ class EventCreatePage extends React.Component<EventCreatePageProps, EventCreateP
|
||||
</Helmet>
|
||||
<h1>{title}</h1>
|
||||
{statusMessage && <div className="success">{statusMessage}</div>}
|
||||
<Form schema={schema}
|
||||
<Form schema={schema as any}
|
||||
uiSchema={uiSchema}
|
||||
formData={formData}
|
||||
idPrefix="rjsf"
|
||||
widgets={widgets}
|
||||
widgets={widgets as any}
|
||||
onChange={this.onChange}
|
||||
onSubmit={this.onSubmit}
|
||||
onError={this.onError}
|
||||
|
||||
Reference in New Issue
Block a user