Skip to content

Commit 0ea7de7

Browse files
committed
fix: assign tags created from the project dialog
The tag dialog's form is nested inside the project dialog's form in the React tree, and React propagates events along the React tree rather than the DOM tree, so submitting the tag form also submitted the project form. The project was created immediately with no tags and the dialog closed before more than one tag could be picked. Stop propagation in the tag form's onSubmit, and select the new tag in the selector so it is included when the project is saved. Fixes Dokploy#5117
1 parent fa788b0 commit 0ea7de7

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎apps/dokploy/components/dashboard/settings/tags/handle-tag.tsx‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,14 @@ type Tag = z.infer<typeof TagSchema>;
5353

5454
interface HandleTagProps {
5555
tagId?: string;
56+
/**
57+
* Called with the new tag's id after it is created, so callers embedding
58+
* this dialog (e.g. the tag selector) can select the tag right away.
59+
*/
60+
onCreated?: (tagId: string) => void;
5661
}
5762

58-
export const HandleTag = ({ tagId }: HandleTagProps) => {
63+
export const HandleTag = ({ tagId, onCreated }: HandleTagProps) => {
5964
const utils = api.useUtils();
6065
const [isOpen, setIsOpen] = useState(false);
6166
const colorInputRef = useRef<HTMLInputElement>(null);
@@ -101,8 +106,11 @@ export const HandleTag = ({ tagId }: HandleTagProps) => {
101106
color: data.color,
102107
tagId: tagId || "",
103108
})
104-
.then(async () => {
109+
.then(async (result) => {
105110
await utils.tag.all.invalidate();
111+
if (!tagId && result?.tagId) {
112+
onCreated?.(result.tagId);
113+
}
106114
toast.success(tagId ? "Tag Updated" : "Tag Created");
107115
setIsOpen(false);
108116
form.reset();
@@ -139,9 +147,18 @@ export const HandleTag = ({ tagId }: HandleTagProps) => {
139147
</DialogHeader>
140148
{isError && <AlertBlock type="error">{error?.message}</AlertBlock>}
141149
<Form {...form}>
150+
{/*
151+
* This dialog can be rendered inside another form (e.g. the tag
152+
* selector in the project dialog). React propagates events through the
153+
* React tree, not the DOM tree, so without stopPropagation submitting
154+
* this form would also submit the outer one.
155+
*/}
142156
<form
143157
id="hook-form-tag"
144-
onSubmit={form.handleSubmit(onSubmit)}
158+
onSubmit={(e) => {
159+
e.stopPropagation();
160+
form.handleSubmit(onSubmit)(e);
161+
}}
145162
className="grid w-full gap-4"
146163
>
147164
<FormField

‎apps/dokploy/components/shared/tag-selector.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,13 @@ export function TagSelector({
147147
})}
148148
</CommandGroup>
149149
<div className="flex items-center justify-center p-2 border-t">
150-
<HandleTag />
150+
<HandleTag
151+
onCreated={(tagId) => {
152+
if (!selectedTags.includes(tagId)) {
153+
onTagsChange([...selectedTags, tagId]);
154+
}
155+
}}
156+
/>
151157
</div>
152158
</CommandList>
153159
</Command>

0 commit comments

Comments
 (0)