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: Add duplicate name logic to Rename flow onClick #3457

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Changes from all 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
62 changes: 37 additions & 25 deletions editor.planx.uk/src/pages/Team.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,15 @@ export function AddButton({

interface FlowItemProps {
flow: any;
flows: any;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use of the any type followed current practices in the Team component, may be worth a refactor in future to follow TS best practices

teamId: number;
teamSlug: string;
refreshFlows: () => void;
}

const FlowItem: React.FC<FlowItemProps> = ({
flow,
flows,
teamId,
teamSlug,
refreshFlows,
Expand Down Expand Up @@ -200,34 +202,43 @@ const FlowItem: React.FC<FlowItemProps> = ({
const newName = prompt("New name", flow.name);
if (newName && newName !== flow.name) {
const newSlug = slugify(newName);
await client.mutate({
mutation: gql`
mutation UpdateFlowSlug(
$teamId: Int
$slug: String
$newSlug: String
$newName: String
) {
update_flows(
where: {
team: { id: { _eq: $teamId } }
slug: { _eq: $slug }
}
_set: { slug: $newSlug, name: $newName }
const duplicateFlowName = flows?.find(
(flow: any) => flow.slug === newSlug,
);
if (!duplicateFlowName) {
await client.mutate({
mutation: gql`
mutation UpdateFlowSlug(
$teamId: Int
$slug: String
$newSlug: String
$newName: String
) {
affected_rows
update_flows(
where: {
team: { id: { _eq: $teamId } }
slug: { _eq: $slug }
}
_set: { slug: $newSlug, name: $newName }
) {
affected_rows
}
}
}
`,
variables: {
teamId: teamId,
slug: flow.slug,
newSlug: newSlug,
newName: newName,
},
});
`,
variables: {
teamId: teamId,
slug: flow.slug,
newSlug: newSlug,
newName: newName,
},
});

refreshFlows();
refreshFlows();
} else if (duplicateFlowName) {
alert(
`The flow "${newName}" already exists. Enter a unique flow name to continue`,
);
}
}
},
label: "Rename",
Expand Down Expand Up @@ -351,6 +362,7 @@ const Team: React.FC = () => {
{flows.map((flow: any) => (
<FlowItem
flow={flow}
flows={flows}
key={flow.slug}
teamId={teamId}
teamSlug={slug}
Expand Down
Loading