Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Fetch team_settings data for Editor forms and adding Update fn #3366

Merged
merged 8 commits into from
Jul 3, 2024
Merged
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useFormik } from "formik";
import { useStore } from "pages/FlowEditor/lib/store";
import React, { ChangeEvent } from "react";
import InputLabel from "ui/editor/InputLabel";
import Input from "ui/shared/Input";
Expand All @@ -9,9 +10,14 @@ import { FormProps } from ".";
export default function BoundaryForm({ formikConfig, onSuccess }: FormProps) {
const formik = useFormik({
...formikConfig,
onSubmit(values, { resetForm }) {
onSuccess();
resetForm({ values });
onSubmit: async (values, { resetForm }) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're still missing some basic validation on this field. I appreciate there's still work to be done on the boundary field, but if we're going to unhide the menu and allow Editors to access it we should either put this in place first, or hide this field.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do the error handling in another PR, hopefully make it a lot cleaner. Current ones were just to test my understanding of formik and yup

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added basic validation now though

const isSuccess = await useStore.getState().updateTeamSettings({
boundaryUrl: values.boundaryUrl,
});
if (isSuccess) {
onSuccess();
resetForm({ values });
}
},
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useFormik } from "formik";
import { useStore } from "pages/FlowEditor/lib/store";
import React, { ChangeEvent } from "react";
import InputLabel from "ui/editor/InputLabel";
import Input from "ui/shared/Input";
Expand All @@ -13,16 +14,26 @@ export default function ContactForm({ formikConfig, onSuccess }: FormProps) {
.email("Please enter valid email")
.required("Help Email is required"),
helpPhone: Yup.string().required("Help Phone is required"),
helpOpeningHours: Yup.string(),
homepage: Yup.string().url("Please enter a valid URL for the homepage"),
helpOpeningHours: Yup.string().required(),
homepage: Yup.string()
.url("Please enter a valid URL for the homepage")
.required("Enter a homepage"),
});

const formik = useFormik({
...formikConfig,
validationSchema: formSchema,
onSubmit(values, { resetForm }) {
onSuccess();
resetForm({ values });
onSubmit: async (values, { resetForm }) => {
const isSuccess = await useStore.getState().updateTeamSettings({
helpEmail: values.helpEmail,
helpOpeningHours: values.helpOpeningHours,
helpPhone: values.helpPhone,
Comment on lines +27 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

homepage is missing here which means that changes to the URL are not persisted.

homepage: values.homepage,
});
if (isSuccess) {
onSuccess();
resetForm({ values });
}
},
});

Expand All @@ -41,18 +52,20 @@ export default function ContactForm({ formikConfig, onSuccess }: FormProps) {
}
input={
<>
<InputLabel label="Homepage URL" htmlFor="homepageUrl">
<InputLabel label="Homepage URL" htmlFor="homepage">
<Input
name="homepage"
onChange={(event) => {
onChangeFn("homepage", event);
}}
id="homepageUrl"
value={formik.values.homepage}
id="homepage"
/>
</InputLabel>
<InputLabel label="Contact email address" htmlFor="helpEmail">
<Input
name="helpEmail"
value={formik.values.helpEmail}
onChange={(event) => {
onChangeFn("helpEmail", event);
}}
Expand All @@ -62,6 +75,7 @@ export default function ContactForm({ formikConfig, onSuccess }: FormProps) {
<InputLabel label="Phone number" htmlFor="helpPhone">
<Input
name="helpPhone"
value={formik.values.helpPhone}
onChange={(event) => {
onChangeFn("helpPhone", event);
}}
Expand All @@ -72,6 +86,7 @@ export default function ContactForm({ formikConfig, onSuccess }: FormProps) {
<Input
multiline
name="helpOpeningHours"
value={formik.values.helpOpeningHours}
onChange={(event) => {
onChangeFn("helpOpeningHours", event);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,33 @@ import Alert from "@mui/material/Alert";
import Box from "@mui/material/Box";
import Snackbar from "@mui/material/Snackbar";
import Typography from "@mui/material/Typography";
import { TeamSettings } from "@opensystemslab/planx-core/types";
import { FormikConfig } from "formik";
import { useStore } from "pages/FlowEditor/lib/store";
import React, { useEffect, useState } from "react";
import SettingsSection from "ui/editor/SettingsSection";

import BoundaryForm from "./BoundaryForm";
import ContactForm from "./ContactForm";

export interface GeneralSettings {
boundaryUrl: string;
helpEmail: string;
helpPhone: string;
helpOpeningHours: string;
homepage: string;
}

export interface FormProps {
formikConfig: FormikConfig<GeneralSettings>;
formikConfig: FormikConfig<TeamSettings>;
onSuccess: () => void;
}

const GeneralSettings: React.FC = () => {
const [formikConfig, setFormikConfig] = useState<
FormikConfig<GeneralSettings> | undefined
FormikConfig<TeamSettings> | undefined
>(undefined);

const initialValues = {
boundaryUrl: "",
helpEmail: "",
helpPhone: "",
helpOpeningHours: "",
homepage: "",
};

useEffect(() => {
const fetchTeam = async () => {
try {
const fetchedTeam = await useStore.getState().fetchCurrentTeam();
if (!fetchedTeam) throw Error("Unable to find team");

setFormikConfig({
initialValues: initialValues,
initialValues: fetchedTeam.teamSettings,
onSubmit: () => {},
validateOnBlur: false,
validateOnChange: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const SettingsForm = <TFormikValues,>({
{preview}
</Box>
)}

<ErrorWrapper
error={Object.values(formik.errors).join(", ")}
id="settings-error"
Expand Down
10 changes: 10 additions & 0 deletions editor.planx.uk/src/pages/FlowEditor/lib/store/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface TeamStore {
clearTeamStore: () => void;
fetchCurrentTeam: () => Promise<Team>;
updateTeamTheme: (theme: Partial<TeamTheme>) => Promise<boolean>;
updateTeamSettings: (teamSettings: Partial<TeamSettings>) => Promise<boolean>;
}

export const teamStore: StateCreator<
Expand Down Expand Up @@ -128,4 +129,13 @@ export const teamStore: StateCreator<
const isSuccess = await $client.team.updateTheme(teamId, theme);
return isSuccess;
},

updateTeamSettings: async (teamSettings: Partial<TeamSettings>) => {
const { teamId, $client } = get();
const isSuccess = await $client.team.updateTeamSettings(
teamId,
teamSettings,
);
return isSuccess;
},
});
10 changes: 5 additions & 5 deletions editor.planx.uk/src/routes/teamSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const teamSettingsRoutes = compose(
<Settings
currentTab={req.params.tab}
tabs={[
{
name: "General",
route: "general",
Component: GeneralSettings,
},
{
name: "Team",
route: "team",
Expand All @@ -47,11 +52,6 @@ const teamSettingsRoutes = compose(
route: "design",
Component: DesignSettings,
},
// {
// name: "General",
// route: "general",
// Component: GeneralSettings,
// },
]}
/>
),
Expand Down
Loading