From c51f9d57eb031469d52f20a00fd90c5838340ac5 Mon Sep 17 00:00:00 2001 From: Kyle Deal Date: Tue, 22 Oct 2024 17:14:00 +0000 Subject: [PATCH] Add a create subscription page This allows us to make new subscriptions from the UI instead of having to curl the api with the correct fields filled out. --- frontend/containers/CreateSubscription.js | 68 +++++++++++++++++++++++ frontend/containers/SubscriptionsList.js | 3 + frontend/index.js | 5 ++ frontend/webapp.js | 4 ++ 4 files changed, 80 insertions(+) create mode 100644 frontend/containers/CreateSubscription.js diff --git a/frontend/containers/CreateSubscription.js b/frontend/containers/CreateSubscription.js new file mode 100644 index 0000000..6e230c7 --- /dev/null +++ b/frontend/containers/CreateSubscription.js @@ -0,0 +1,68 @@ +import axios from "axios"; +import React from "react"; + +import { prepareSubscriptionPayload } from "../lib/subscription"; +import EditSubscription from "../components/EditSubscription"; +import DisplayErrorMessage from "../components/DisplayErrors"; + +const DEFAULT_OPTIONS = { + location: "Online", + name: "", + office: "Remote", + rule_logic: null, + rules: [], + size: 2, + time_slots: [], + timezone: "America/Los_Angeles", + default_auto_opt_in: false, +}; + +function CreateSubscription() { + const [subscription, setSubscription] = React.useState({ + ...DEFAULT_OPTIONS, + }); + const [saving, setSaving] = React.useState(false); + const [errors, setErrors] = React.useState(null); + + const createSubscription = () => { + setSaving(true); + const payloadSubscription = prepareSubscriptionPayload(subscription); + + setSaving(false); + axios + .post(`/v1/subscriptions/`, payloadSubscription) + .then(() => { + setSaving(false); + setErrors(null); + window.location.assign("/admin/subscriptions"); + }) + .catch((error) => { + setSaving(false); + if (error.response) { + setErrors(error.response.data); + } else { + setErrors([{ msg: "Error sending POST request to server" }]); + } + }); + }; + + return ( +
+ {errors && } + + +
+ ); +} + +export default CreateSubscription; diff --git a/frontend/containers/SubscriptionsList.js b/frontend/containers/SubscriptionsList.js index 8d5e9ed..e67bdd5 100644 --- a/frontend/containers/SubscriptionsList.js +++ b/frontend/containers/SubscriptionsList.js @@ -28,6 +28,9 @@ function SubscriptionList() { return (

Subscriptions

+ + New Subscription + diff --git a/frontend/index.js b/frontend/index.js index c29212e..08dbb9b 100644 --- a/frontend/index.js +++ b/frontend/index.js @@ -10,6 +10,7 @@ import MeetingRequest from "./containers/MeetingRequest"; import User from "./containers/User"; import SubscriptionsList from "./containers/SubscriptionsList"; import Subscription from "./containers/Subscription"; +import CreateSubscription from "./containers/CreateSubscription"; import Subscribe from "./containers/Subscribe"; const router = createBrowserRouter([ @@ -33,6 +34,10 @@ const router = createBrowserRouter([ path: "/subscribe/:id", element: , }, + { + path: "/admin/subscriptions/create", + element: , + }, { path: "/admin/subscriptions/:id", element: , diff --git a/frontend/webapp.js b/frontend/webapp.js index a5bd6cc..4bdacc7 100644 --- a/frontend/webapp.js +++ b/frontend/webapp.js @@ -69,6 +69,10 @@ app.get("/admin/subscriptions", (req, res) => { res.sendFile(`${__dirname}/index.html`); }); +app.get("/admin/subscriptions/create", (req, res) => { + res.sendFile(`${__dirname}/index.html`); +}); + app.get("/admin/subscriptions/:id", (req, res) => { res.sendFile(`${__dirname}/index.html`); });