Skip to content

Commit

Permalink
feat: Add New Team for Platform Admins (#3441)
Browse files Browse the repository at this point in the history
  • Loading branch information
RODO94 authored Jul 22, 2024
1 parent 5382dbd commit ca2d5b2
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 8 deletions.
7 changes: 7 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 {
fetchCurrentTeam: () => Promise<Team>;
updateTeamTheme: (theme: Partial<TeamTheme>) => Promise<boolean>;
updateTeamSettings: (teamSettings: Partial<TeamSettings>) => Promise<boolean>;
createTeam: (newTeam: { name: string; slug: string }) => Promise<number>;
}

export const teamStore: StateCreator<
Expand Down Expand Up @@ -65,6 +66,12 @@ export const teamStore: StateCreator<
theme: get().teamTheme,
}),

createTeam: async (newTeam) => {
const { $client } = get();
const isSuccess = await $client.team.create(newTeam);
return isSuccess;
},

initTeamStore: async (slug) => {
const { data } = await client.query({
query: gql`
Expand Down
2 changes: 1 addition & 1 deletion editor.planx.uk/src/pages/Team.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const AddButtonRoot = styled(ButtonBase)(({ theme }) => ({
fontWeight: FONT_WEIGHT_SEMI_BOLD,
}));

function AddButton({
export function AddButton({
children,
onClick,
}: {
Expand Down
53 changes: 49 additions & 4 deletions editor.planx.uk/src/pages/Teams.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import Container from "@mui/material/Container";
import { styled } from "@mui/material/styles";
import Typography from "@mui/material/Typography";
import { Team } from "@opensystemslab/planx-core/types";
import navigation from "lib/navigation";
import React from "react";
import { Link } from "react-navi";
import { borderedFocusStyle } from "theme";
import Permission from "ui/editor/Permission";
import { slugify } from "utils";

import { useStore } from "./FlowEditor/lib/store";
import { AddButton } from "./Team";

interface TeamTheme {
slug: string;
Expand Down Expand Up @@ -46,7 +50,10 @@ const TeamColourBand = styled(Box)(({ theme }) => ({
}));

const Teams: React.FC<Props> = ({ teams, teamTheme }) => {
const canUserEditTeam = useStore.getState().canUserEditTeam;
const [canUserEditTeam, createTeam] = useStore((state) => [
state.canUserEditTeam,
state.createTeam,
]);

const editableTeams: Team[] = [];
const viewOnlyTeams: Team[] = [];
Expand All @@ -72,9 +79,47 @@ const Teams: React.FC<Props> = ({ teams, teamTheme }) => {
});
return (
<Container maxWidth="formWrap">
<Typography variant="h2" component="h1" mb={4}>
Select a team
</Typography>
<Box
pb={1}
sx={{
display: "flex",
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
marginBottom: "40px",
}}
>
<Typography variant="h2" component="h1">
Select a team
</Typography>
<Permission.IsPlatformAdmin>
<AddButton
onClick={async () => {
const newTeamName = prompt("Team name");

if (newTeamName) {
const newSlug = slugify(newTeamName);
const teamSlugDuplicate = teams.find(
(team) => team.slug === newSlug,
);
if (teamSlugDuplicate !== undefined) {
alert(
`A team with the name "${teamSlugDuplicate.name}" already exists. Enter a unique team name to continue.`,
);
} else {
await createTeam({
name: newTeamName,
slug: newSlug,
});
navigation.navigate(`/${newSlug}`);
}
}
}}
>
Add a new team
</AddButton>
</Permission.IsPlatformAdmin>
</Box>
{editableTeams.length > 0 && (
<>
<Typography variant="h3" component="h2" mb={2}>
Expand Down
51 changes: 51 additions & 0 deletions hasura.planx.uk/metadata/tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,26 @@
- name: team
using:
foreign_key_constraint_on: team_id
insert_permissions:
- role: platformAdmin
permission:
check: {}
columns:
- id
- team_id
- production_bops_submission_url
- staging_bops_submission_url
- production_power_automate_api_key
- staging_power_automate_api_key
- has_planning_data
- production_file_api_key
- staging_file_api_key
- production_bops_secret
- staging_bops_secret
- production_govpay_secret
- staging_govpay_secret
- power_automate_webhook_url
comment: ""
select_permissions:
- role: api
permission:
Expand Down Expand Up @@ -1727,6 +1747,24 @@
- name: team
using:
foreign_key_constraint_on: team_id
insert_permissions:
- role: platformAdmin
permission:
check: {}
columns:
- id
- external_planning_site_name
- external_planning_site_url
- homepage
- help_email
- help_opening_hours
- help_phone
- email_reply_to_id
- team_id
- boundary_bbox
- reference_code
- boundary_url
comment: ""
select_permissions:
- role: api
permission:
Expand Down Expand Up @@ -1843,6 +1881,19 @@
- name: team
using:
foreign_key_constraint_on: team_id
insert_permissions:
- role: platformAdmin
permission:
check: {}
columns:
- id
- team_id
- favicon
- logo
- action_colour
- link_colour
- primary_colour
comment: ""
select_permissions:
- role: api
permission:
Expand Down
6 changes: 4 additions & 2 deletions hasura.planx.uk/tests/team_integrations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ describe("team_integrations", () => {
expect(i.queries).not.toContain("team_integrations");
});

test("cannot create, update, or delete team_integrations", () => {
expect(i).toHaveNoMutationsFor("team_integrations");
test("can create but, cannot update, or delete team_integrations", () => {
expect(i.mutations).toContain("insert_team_integrations");
expect(i.mutations).not.toContain("update_team_integrations_by_pk");
expect(i.mutations).not.toContain("delete_team_integrations");
});
});

Expand Down
6 changes: 5 additions & 1 deletion hasura.planx.uk/tests/team_themes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ describe("team_themes", () => {
expect(i.queries).toContain("team_themes");
});

test("cannot insert team_themes", () => {
test("cannot query insert team_themes", () => {
expect(i.queries).not.toContain("insert_team_themes");
});

test("can query team_themes", async () => {
expect(i.queries).toContain("team_themes");
});

test("can insert team_themes", () => {
expect(i.mutations).toContain("insert_team_themes");
});

test("can mutate team_themes", async () => {
expect(i.mutations).toContain("update_team_themes");
expect(i.mutations).toContain("update_team_themes_by_pk");
Expand Down

0 comments on commit ca2d5b2

Please sign in to comment.