diff --git a/graphql/src/query.rs b/graphql/src/query.rs index 41a13fc0c..2507294ef 100644 --- a/graphql/src/query.rs +++ b/graphql/src/query.rs @@ -947,8 +947,7 @@ impl Mutation { .data::>()? .loader() .insert_edited_collection(input) - .await? - .to_string()) + .await?) } #[graphql(guard = "GroupGuard::new(UserGroup::Editors)")] diff --git a/types/.sqlx/query-26fb16ba5cc531729a4e24aa202c2c4aba2ea7895d20b129dbe7c330e08270c8.json b/types/.sqlx/query-f6b89363873a4af799c8fde3c0c2f95334b25b895fd7772308d0dc6cdc8c1371.json similarity index 62% rename from types/.sqlx/query-26fb16ba5cc531729a4e24aa202c2c4aba2ea7895d20b129dbe7c330e08270c8.json rename to types/.sqlx/query-f6b89363873a4af799c8fde3c0c2f95334b25b895fd7772308d0dc6cdc8c1371.json index 157963eb0..de05a7f81 100644 --- a/types/.sqlx/query-26fb16ba5cc531729a4e24aa202c2c4aba2ea7895d20b129dbe7c330e08270c8.json +++ b/types/.sqlx/query-f6b89363873a4af799c8fde3c0c2f95334b25b895fd7772308d0dc6cdc8c1371.json @@ -1,12 +1,12 @@ { "db_name": "PostgreSQL", - "query": "insert into edited_collection(title, slug, description, thumbnail_url)\nvalues ($1, $2, $3, $4)\nreturning id\n", + "query": "insert into edited_collection(title, slug, description, thumbnail_url)\nvalues ($1, $2, $3, $4)\nreturning slug\n", "describe": { "columns": [ { "ordinal": 0, - "name": "id", - "type_info": "Uuid" + "name": "slug", + "type_info": "Text" } ], "parameters": { @@ -21,5 +21,5 @@ false ] }, - "hash": "26fb16ba5cc531729a4e24aa202c2c4aba2ea7895d20b129dbe7c330e08270c8" + "hash": "f6b89363873a4af799c8fde3c0c2f95334b25b895fd7772308d0dc6cdc8c1371" } diff --git a/types/queries/insert_edited_collection.sql b/types/queries/insert_edited_collection.sql index 2e298cea2..b2bf26b3f 100644 --- a/types/queries/insert_edited_collection.sql +++ b/types/queries/insert_edited_collection.sql @@ -1,3 +1,3 @@ insert into edited_collection(title, slug, description, thumbnail_url) values ($1, $2, $3, $4) -returning id +returning slug diff --git a/types/src/database_sql.rs b/types/src/database_sql.rs index 1a4ee47b3..867fdec35 100644 --- a/types/src/database_sql.rs +++ b/types/src/database_sql.rs @@ -1842,9 +1842,9 @@ impl Database { pub async fn insert_edited_collection( &self, collection: CreateEditedCollectionInput, - ) -> Result { + ) -> Result { // let mut tx = self.client.begin().await?; - let collection_id = query_file_scalar!( + let collection_slug = query_file_scalar!( "queries/insert_edited_collection.sql", collection.title, slug::slugify(&collection.title).replace("-", "_"), @@ -1853,7 +1853,7 @@ impl Database { ) .fetch_one(&self.client) .await?; - Ok(collection_id) + Ok(collection_slug) } pub async fn document_breadcrumbs( diff --git a/website/src/pages/edited-collections/new.page.tsx b/website/src/pages/edited-collections/new.page.tsx index 7bf70c4b0..fd223b240 100644 --- a/website/src/pages/edited-collections/new.page.tsx +++ b/website/src/pages/edited-collections/new.page.tsx @@ -13,6 +13,23 @@ interface NewEditedCollectionForm { thumbnail: File | null } +interface MenuItemLike { + label: string + path: string + items?: ReadonlyArray | null +} + +const toMenuItemInput = ( + nodes: ReadonlyArray | null | undefined +): ReadonlyArray | null => { + if (!nodes?.length) return null + return nodes.map((n) => ({ + label: n.label, + path: n.path, + items: toMenuItemInput(n.items), + })) +} + const NewEditedCollectionPage = () => { const { user } = useUser() const [formData, setFormData] = useState({ @@ -23,6 +40,10 @@ const NewEditedCollectionPage = () => { const [isSubmitting, setIsSubmitting] = useState(false) const [error, setError] = useState(null) const [, addEditedCollection] = Dailp.useAddEditedCollectionMutation() + const [, updateMenu] = Dailp.useUpdateMenuMutation() + const [{ data: menuData }] = Dailp.useMenuBySlugQuery({ + variables: { slug: "default-nav" }, + }) const handleEditedCollectionSubmit = async ( e: React.FormEvent @@ -56,10 +77,42 @@ const NewEditedCollectionPage = () => { } if (result.data?.createEditedCollection) { - console.log( - "Successfully created collection with ID:", - result.data.createEditedCollection - ) + // console.log( + // "Successfully created collection with ID:", + // result.data.createEditedCollection + // ) + + const menu = menuData?.menuBySlug + if (menu) { + const updatedItems: MenuItemLike[] = (menu.items ?? []).map( + (item) => { + if (item.label.toLowerCase() === "collections") { + return { + label: item.label, + path: item.path, + items: [ + ...(item.items ?? []), + { + label: formData.title, + path: `/collections/${result.data?.createEditedCollection}`, + items: [], + }, + ], + } + } + return item + } + ) + + updateMenu({ + menu: { + id: menu.id, + name: menu.name, + items: toMenuItemInput(updatedItems), + }, + }) + } + // Navigate to the new collection or show success message navigate(`/collections/${result.data.createEditedCollection}`) } else {