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
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ updates:
- "theopensystemslab/planx"
ignore:
- dependency-name: "*"
update-types: ["version-update:semver-patch"]
update-types: ["version-update:semver-patch", "version-update:semver-minor"]

# Hasura
- package-ecosystem: "npm"
Expand All @@ -38,7 +38,7 @@ updates:
- "theopensystemslab/planx"
ignore:
- dependency-name: "*"
update-types: ["version-update:semver-patch"]
update-types: ["version-update:semver-patch", "version-update:semver-minor"]

- package-ecosystem: "docker"
directory: "/hasura.planx.uk"
Expand Down Expand Up @@ -76,7 +76,7 @@ updates:
- "theopensystemslab/planx"
ignore:
- dependency-name: "*"
update-types: ["version-update:semver-patch"]
update-types: ["version-update:semver-patch", "version-update:semver-minor"]

# ShareDB
- package-ecosystem: "npm"
Expand All @@ -90,7 +90,7 @@ updates:
- "theopensystemslab/planx"
ignore:
- dependency-name: "*"
update-types: ["version-update:semver-patch"]
update-types: ["version-update:semver-patch", "version-update:semver-minor"]

- package-ecosystem: "docker"
directory: "/sharedb.planx.uk"
Expand All @@ -115,7 +115,7 @@ updates:
- "theopensystemslab/planx"
ignore:
- dependency-name: "*"
update-types: ["version-update:semver-patch"]
update-types: ["version-update:semver-patch", "version-update:semver-minor"]

- package-ecosystem: "docker"
directory: "/api.planx.uk"
Expand All @@ -142,7 +142,7 @@ updates:
- "theopensystemslab/planx"
ignore:
- dependency-name: "*"
update-types: ["version-update:semver-patch"]
update-types: ["version-update:semver-patch", "version-update:semver-minor"]

# Infrastructure
# - package-ecosystem: "npm"
Expand Down
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().updateTeamSettings({
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().updateTeamSettings({
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,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();
console.log(fetchedTeam);
if (!fetchedTeam) throw Error("Unable to find team");
setFormikConfig({
initialValues: initialValues,
initialValues: fetchedTeam.teamSettings,
onSubmit: () => {},
validateOnBlur: false,
validateOnChange: false,
Expand Down
19 changes: 15 additions & 4 deletions editor.planx.uk/src/pages/FlowEditor/lib/store/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ export interface TeamStore {
teamId: number;
teamIntegrations: TeamIntegrations;
teamName: string;
teamSettings: TeamSettings;
teamSlug: string;
teamTheme: TeamTheme;
teamSettings: TeamSettings;

setTeam: (team: Team) => void;
getTeam: () => Team;
initTeamStore: (slug: string) => Promise<void>;
clearTeamStore: () => void;
fetchCurrentTeam: () => Promise<Team>;
updateTeamTheme: (theme: Partial<TeamTheme>) => Promise<boolean>;
updateTeamSettings: (teamSettings: Partial<TeamSettings>) => Promise<boolean>;
}

export const teamStore: StateCreator<
Expand All @@ -37,19 +38,19 @@ export const teamStore: StateCreator<
teamId: 0,
teamIntegrations: {} as TeamIntegrations,
teamName: "",
teamSettings: {} as TeamSettings,
teamSlug: "",
teamTheme: {} as TeamTheme,
teamSettings: {} as TeamSettings,

setTeam: (team) => {
set({
boundaryBBox: team.boundaryBBox,
teamId: team.id,
teamIntegrations: team.integrations,
teamName: team.name,
teamSettings: team.teamSettings,
teamSlug: team.slug,
teamTheme: team.theme,
teamSettings: team.teamSettings,
});

if (team.theme?.favicon) {
Expand All @@ -63,9 +64,9 @@ export const teamStore: StateCreator<
id: get().teamId,
integrations: get().teamIntegrations,
name: get().teamName,
teamSettings: get().teamSettings,
slug: get().teamSlug,
theme: get().teamTheme,
teamSettings: get().teamSettings,
}),

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

updateTeamSettings: async (generalSettings: Partial<TeamSettings>) => {
const { teamId, $client } = get();
console.log(generalSettings);
const isSuccess = await $client.team.updateTeamSettings(
teamId,
generalSettings,
);
return isSuccess;
},
});
Loading
Loading