Skip to content

Commit

Permalink
better redirects & auto activate
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarrazin committed Jul 3, 2024
1 parent 1aa1073 commit 47e665b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
18 changes: 15 additions & 3 deletions src/routes/tools/ToolEdit.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import { colors, icons } from "$lib/utils/tools";
import { applyAction, enhance } from "$app/forms";
import { getGradioApi } from "$lib/utils/getGradioApi";
import { useSettingsStore } from "$lib/stores/settings";
import { goto } from "$app/navigation";
import { base } from "$app/paths";
type ActionData = {
error: boolean;
Expand Down Expand Up @@ -40,8 +43,9 @@
let editableTool: CommunityToolEditable = tool ?? {
displayName: "",
description: "",
color: "blue",
icon: "wikis",
// random color & icon for new tools
color: colors[Math.floor(Math.random() * colors.length)],
icon: icons[Math.floor(Math.random() * icons.length)],
baseUrl: "",
endpoint: "",
name: "process_image",
Expand Down Expand Up @@ -136,6 +140,8 @@
return "str";
}
}
const settings = useSettingsStore();
</script>

<form
Expand All @@ -147,7 +153,13 @@

return async ({ result }) => {
formLoading = false;
await applyAction(result);

if (result.type === "success" && result.data && typeof result.data.toolId === "string") {
$settings.tools = [...($settings.tools ?? []), result.data.toolId];
goto(`${base}/tools/${result.data.toolId}`, { invalidateAll: true });
} else {
await applyAction(result);
}
};
}}
>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/tools/[toolId]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
let previousPage: string = base;
afterNavigate(({ from }) => {
if (!from?.url.pathname.includes("settings")) {
if (!from?.url.pathname.includes("tools/")) {
previousPage = from?.url.toString() || previousPage;
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/routes/tools/[toolId]/edit/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ export const actions = {
}
);

throw redirect(302, `${base}/tools`);
throw redirect(302, `${base}/tools/${tool._id.toString()}`);
},
};
4 changes: 3 additions & 1 deletion src/routes/tools/[toolId]/edit/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
export let form;
afterNavigate(({ from }) => {
previousPage = from?.url.toString() || previousPage;
if (!from?.url.pathname.includes("tools/")) {
previousPage = from?.url.toString() || previousPage;
}
});
</script>

Expand Down
7 changes: 1 addition & 6 deletions src/routes/tools/new/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ export const actions = {
featured: false,
searchTokens: generateSearchTokens(parse.data.name),
});
// add insertedId to user settings

await collections.settings.updateOne(authCondition(locals), {
$addToSet: { tools: insertedId.toString() },
});

throw redirect(302, `${base}/tools`);
return { toolId: insertedId.toString() };
},
};

0 comments on commit 47e665b

Please sign in to comment.