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: fetching team_settings data from database and populating forms #3319

Closed
wants to merge 8 commits into from
Closed
12 changes: 6 additions & 6 deletions editor.planx.uk/src/components/Header.test.tsx
Copy link
Contributor

Choose a reason for hiding this comment

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

The changes in this file (removed commas) don't match our linting style. Husky should be picking this up and fixing it as part of a pre-commit hook but this clearly isn't working right atm for your setup it seems. We can also look at checking this at the CI stage if we need to.

You can fix this file with pnpm lint:fix

Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe("Header Component - Editor Route", () => {
id: 123,
email: "[email protected]",
},
}),
})
);

jest.spyOn(ReactNavi, "useCurrentRoute").mockImplementation(
Expand All @@ -98,7 +98,7 @@ describe("Header Component - Editor Route", () => {
data: {
flow: "test-flow",
},
}) as any,
}) as any
);
});

Expand Down Expand Up @@ -139,7 +139,7 @@ for (const route of ["/published", "/preview", "/draft", "/pay", "/invite"]) {
data: {
flow: "test-flow",
},
}) as any,
}) as any
);
});

Expand All @@ -148,15 +148,15 @@ for (const route of ["/published", "/preview", "/draft", "/pay", "/invite"]) {
expect(screen.queryByText("Plan✕")).not.toBeInTheDocument();
expect(screen.getByAltText(`${mockTeam1.name} Logo`)).toHaveAttribute(
"src",
"logo.jpg",
"logo.jpg"
);
});

it("falls back to the PlanX link when a logo is not present", () => {
act(() => setState({ teamTheme: mockTeam2.theme }));
setup(<Header />);
expect(
screen.queryByAltText(`${mockTeam2.name} Logo`),
screen.queryByAltText(`${mockTeam2.name} Logo`)
).not.toBeInTheDocument();
expect(screen.getByText("Plan✕")).toBeInTheDocument();
act(() => setState({ teamTheme: mockTeam1.theme }));
Expand Down Expand Up @@ -190,7 +190,7 @@ describe("Section navigation bar", () => {
data: {
flow: "test-flow",
},
}) as any,
}) as any
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const DesignSettings: React.FC = () => {
try {
const fetchedTeam = await useStore.getState().fetchCurrentTeam();
if (!fetchedTeam) throw Error("Unable to find team");

setFormikConfig({
initialValues: fetchedTeam.theme,
// This value will be set per form section
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 @@ -9,9 +10,16 @@ import { FormProps } from ".";
export default function BoundaryForm({ formikConfig, onSuccess }: FormProps) {
const formik = useFormik({
...formikConfig,
onSubmit(values, { resetForm }) {
onSuccess();
resetForm({ values });
onSubmit: async (values, { resetForm }) => {
const isSuccess = await useStore.getState().updateGeneralSettings({
boundaryUrl: values.boundaryUrl,
});
console.log("button pressed");
console.log({ successBool: isSuccess });
if (isSuccess) {
onSuccess();
resetForm({ values });
}
},
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
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";
import InputRow from "ui/shared/InputRow";
import InputRowLabel from "ui/shared/InputRowLabel";
import * as Yup from "yup";

import { SettingsForm } from "../shared/SettingsForm";
Expand All @@ -20,9 +23,17 @@ export default function ContactForm({ formikConfig, onSuccess }: FormProps) {
const formik = useFormik({
...formikConfig,
validationSchema: formSchema,
onSubmit(values, { resetForm }) {
onSuccess();
resetForm({ values });
onSubmit: async (values, { resetForm }) => {
const isSuccess = await useStore.getState().updateGeneralSettings({
homepage: values.homepage,
helpEmail: values.helpEmail,
helpPhone: values.helpPhone,
helpOpeningHours: values.helpOpeningHours,
});
if (isSuccess) {
onSuccess();
resetForm({ values });
}
},
});

Expand All @@ -41,43 +52,55 @@ export default function ContactForm({ formikConfig, onSuccess }: FormProps) {
}
input={
<>
<InputLabel label="Homepage URL" htmlFor="homepageUrl">
<Input
name="homepage"
onChange={(event) => {
onChangeFn("homepage", event);
}}
id="homepageUrl"
/>
</InputLabel>
<InputLabel label="Contact email address" htmlFor="helpEmail">
<Input
name="helpEmail"
onChange={(event) => {
onChangeFn("helpEmail", event);
}}
id="helpEmail"
/>
</InputLabel>
<InputLabel label="Phone number" htmlFor="helpPhone">
<Input
name="helpPhone"
onChange={(event) => {
onChangeFn("helpPhone", event);
}}
id="helpPhone"
/>
</InputLabel>
<InputLabel label="Opening hours" htmlFor="helpOpeningHours">
<Input
multiline
name="helpOpeningHours"
onChange={(event) => {
onChangeFn("helpOpeningHours", event);
}}
id="helpOpeningHours"
/>
</InputLabel>
<InputRow>
<InputRowLabel>
Homepage URL
<Input
name="homepage"
value={formik.values.homepage}
onChange={(event) => {
onChangeFn("homepage", event);
}}
/>
</InputRowLabel>
</InputRow>
<InputRow>
<InputRowLabel>
Contact email address
<Input
name="helpEmail"
value={formik.values.helpEmail}
onChange={(event) => {
onChangeFn("helpEmail", event);
}}
/>
</InputRowLabel>
</InputRow>
<InputRow>
<InputRowLabel>
Phone number
<Input
name="helpPhone"
value={formik.values.helpPhone}
onChange={(event) => {
onChangeFn("helpPhone", event);
}}
/>
</InputRowLabel>
</InputRow>
<InputRow>
<InputRowLabel>
Opening hours
<Input
multiline
name="helpOpeningHours"
value={formik.values.helpOpeningHours}
onChange={(event) => {
onChangeFn("helpOpeningHours", event);
}}
/>
</InputRowLabel>
</InputRow>
</>
}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ 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 { GeneralTeamSettings } 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";

Expand All @@ -18,28 +20,23 @@ export interface GeneralSettings {
}

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

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

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

useEffect(() => {
const fetchTeam = async () => {
try {
const fetchedTeam = await useStore.getState().fetchCurrentTeam();
console.log(fetchedTeam);
if (!fetchedTeam) throw Error("Unable to find team");
setFormikConfig({
initialValues: initialValues,
initialValues: fetchedTeam.team_settings,
onSubmit: () => {},
validateOnBlur: false,
validateOnChange: false,
Expand Down
19 changes: 19 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 @@ -18,13 +18,17 @@ export interface TeamStore {
teamSettings: TeamSettings;
teamSlug: string;
teamTheme: TeamTheme;
teamGeneralSettings: GeneralTeamSettings;

setTeam: (team: Team) => void;
getTeam: () => Team;
initTeamStore: (slug: string) => Promise<void>;
clearTeamStore: () => void;
fetchCurrentTeam: () => Promise<Team>;
updateTeamTheme: (theme: Partial<TeamTheme>) => Promise<boolean>;
updateGeneralSettings: (
generalSettings: Partial<GeneralTeamSettings>,
) => Promise<boolean>;
}

export const teamStore: StateCreator<
Expand All @@ -40,6 +44,7 @@ export const teamStore: StateCreator<
teamSettings: {} as TeamSettings,
teamSlug: "",
teamTheme: {} as TeamTheme,
teamGeneralSettings: {} as GeneralTeamSettings,

setTeam: (team) => {
set({
Expand All @@ -50,6 +55,7 @@ export const teamStore: StateCreator<
teamSettings: team.teamSettings,
teamSlug: team.slug,
teamTheme: team.theme,
teamGeneralSettings: team.team_settings,
});

if (team.theme?.favicon) {
Expand All @@ -66,6 +72,7 @@ export const teamStore: StateCreator<
teamSettings: get().teamSettings,
slug: get().teamSlug,
theme: get().teamTheme,
team_settings: get().teamGeneralSettings,
}),

initTeamStore: async (slug) => {
Expand Down Expand Up @@ -128,4 +135,16 @@ export const teamStore: StateCreator<
const isSuccess = await $client.team.updateTheme(teamId, theme);
return isSuccess;
},

updateGeneralSettings: async (
generalSettings: Partial<GeneralTeamSettings>,
) => {
const { teamId, $client } = get();
console.log(generalSettings);
const isSuccess = await $client.team.updateGeneralSettings(
teamId,
generalSettings,
);
return isSuccess;
},
});
14 changes: 7 additions & 7 deletions editor.planx.uk/src/routes/teamSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const teamSettingsRoutes = compose(

if (!isAuthorised)
throw new NotFoundError(
`User does not have access to ${req.originalUrl}`,
`User does not have access to ${req.originalUrl}`
);

return route(async (req) => ({
Expand All @@ -47,17 +47,17 @@ const teamSettingsRoutes = compose(
route: "design",
Component: DesignSettings,
},
// {
// name: "General",
// route: "general",
// Component: GeneralSettings,
// },
{
name: "General",
route: "general",
Component: GeneralSettings,
},
]}
/>
),
}));
}),
}),
})
);

export default teamSettingsRoutes;
Loading