From 9e23ee2aa3b314f5f74d0793afd97db8df055540 Mon Sep 17 00:00:00 2001 From: Kluskey Date: Tue, 15 Oct 2024 01:24:58 -0700 Subject: [PATCH] update crud component --- .../[spaceId]/build/inspector/inspector.tsx | 2 +- .../create-component.button.tsx | 73 +++++++++++++++---- mirror-2/state/api/entities.tsx | 24 +++--- 3 files changed, 74 insertions(+), 25 deletions(-) diff --git a/mirror-2/app/space/[spaceId]/build/inspector/inspector.tsx b/mirror-2/app/space/[spaceId]/build/inspector/inspector.tsx index 1eb8a9a3..4c67d4f1 100644 --- a/mirror-2/app/space/[spaceId]/build/inspector/inspector.tsx +++ b/mirror-2/app/space/[spaceId]/build/inspector/inspector.tsx @@ -15,7 +15,7 @@ export default function Inspector({ className }) { <> {/* Create Component Button */} - + } diff --git a/mirror-2/components/ui/custom-buttons/create-component.button.tsx b/mirror-2/components/ui/custom-buttons/create-component.button.tsx index 5f6ca39e..c63db9ec 100644 --- a/mirror-2/components/ui/custom-buttons/create-component.button.tsx +++ b/mirror-2/components/ui/custom-buttons/create-component.button.tsx @@ -29,71 +29,118 @@ import { DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu' +import { DatabaseEntity } from '@/state/api/entities' +import { useAddComponentToEntityMutation } from '@/state/api/entities' -export function CreateComponentButton() { +export function CreateComponentButton({ entity }: { entity: DatabaseEntity }) { const iconClassName = 'mr-2 h-4 w-4' const itemClassName = 'cursor-pointer' + // Access the addComponentToEntity mutation hook + const [addComponentToEntity] = useAddComponentToEntityMutation() + + // Function to handle adding a component when a menu item is clicked + // Function to handle adding a component when a menu item is clicked + const handleAddComponent = (componentKey: string, componentData: any) => { + addComponentToEntity({ + id: entity.id, + componentKey, // The specific component key (e.g., 'render', 'camera') + componentData // The component data to be added + }) + } + return ( - + handleAddComponent('2D Sprite', {})} + > 2D Sprite - + handleAddComponent('3D Model', {})} + > 3D Model - + handleAddComponent('Animation', {})} + > Animation - + handleAddComponent('Camera', {})} + > Camera {/* */} - + handleAddComponent('Light', {})} + > Light - + handleAddComponent('Physics', {})} + > Physics - + handleAddComponent('Gaussian Splat', {})} + > Gaussian Splat - + handleAddComponent('Particles', {})} + > Particles - + handleAddComponent('Script', {})} + > Script - + handleAddComponent('Sound', {})} + > Sound - + handleAddComponent('UI', {})} + > UI diff --git a/mirror-2/state/api/entities.tsx b/mirror-2/state/api/entities.tsx index 2f6c9dac..f8e91e64 100644 --- a/mirror-2/state/api/entities.tsx +++ b/mirror-2/state/api/entities.tsx @@ -401,10 +401,11 @@ export const entitiesApi = createApi({ any, { id: EntityId - component: any // The new component data to be added + componentKey: string // The key for the component (e.g., 'render') + componentData: any // The new component data to be added } >({ - queryFn: async ({ id, component }) => { + queryFn: async ({ id, componentKey, componentData }) => { const supabase = createSupabaseBrowserClient() // Fetch the existing components @@ -418,11 +419,12 @@ export const entitiesApi = createApi({ return { error: fetchError.message } } + // Merge the new component data under the specified key (componentKey) const updatedComponents = { ...(typeof existingEntity.components === 'object' ? existingEntity.components : {}), - ...(typeof component === 'object' ? component : {}) // Add or merge new component + [componentKey]: componentData // Add or overwrite the specific component } const { data, error } = await supabase @@ -467,8 +469,8 @@ export const entitiesApi = createApi({ any, { id: EntityId - componentKey: string - updatedComponentData: any + componentKey: string // The key for the component (e.g., 'render') + updatedComponentData: any // The new data for the component } >({ queryFn: async ({ id, componentKey, updatedComponentData }) => { @@ -487,11 +489,10 @@ export const entitiesApi = createApi({ // Update the specific component in the JSONB object const updatedComponents = { - ...(typeof existingEntity.components === 'object' && - existingEntity.components !== null + ...(typeof existingEntity.components === 'object' ? existingEntity.components : {}), - [componentKey]: updatedComponentData + [componentKey]: updatedComponentData // Update only the specific component } const { data, error } = await supabase @@ -515,7 +516,7 @@ export const entitiesApi = createApi({ any, { id: EntityId - componentKey: string + componentKey: string // The key of the component to be deleted } >({ queryFn: async ({ id, componentKey }) => { @@ -554,8 +555,9 @@ export const entitiesApi = createApi({ { type: TAG_NAME_FOR_GENERAL_ENTITY, id } ] }) - - // + /** + * End Components + */ }) })