Skip to content

Commit

Permalink
add components crud on entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Kluskey committed Oct 17, 2024
1 parent 7feef88 commit 3c82b2a
Showing 1 changed file with 169 additions and 1 deletion.
170 changes: 169 additions & 1 deletion mirror-2/state/api/entities.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,170 @@ export const entitiesApi = createApi({
invalidatesTags: (result, error, entityId) => [
{ type: TAG_NAME_FOR_GENERAL_ENTITY, id: entityId }
]
}),

/**
* Components
*/
addComponentToEntity: builder.mutation<
any,
{
id: EntityId
component: any // The new component data to be added
}
>({
queryFn: async ({ id, component }) => {
const supabase = createSupabaseBrowserClient()

// Fetch the existing components
const { data: existingEntity, error: fetchError } = await supabase
.from('entities')
.select('components')
.eq('id', id)
.single()

if (fetchError) {
return { error: fetchError.message }
}

const updatedComponents = {
...(typeof existingEntity.components === 'object'
? existingEntity.components
: {}),
...(typeof component === 'object' ? component : {}) // Add or merge new component
}

const { data, error } = await supabase
.from('entities')
.update({ components: updatedComponents })
.eq('id', id)
.single()

if (error) {
return { error: error.message }
}

return { data }
},
invalidatesTags: (result, error, { id }) => [
{ type: TAG_NAME_FOR_GENERAL_ENTITY, id }
]
}),

getComponentsOfEntity: builder.query<any, EntityId>({
queryFn: async (id: EntityId) => {
const supabase = createSupabaseBrowserClient()

const { data, error } = await supabase
.from('entities')
.select('components')
.eq('id', id)
.single()

if (error) {
return { error: error.message }
}

return { data: data.components }
},
providesTags: (result, error, id) => [
{ type: TAG_NAME_FOR_GENERAL_ENTITY, id }
]
}),

updateComponentOnEntity: builder.mutation<
any,
{
id: EntityId
componentKey: string
updatedComponentData: any
}
>({
queryFn: async ({ id, componentKey, updatedComponentData }) => {
const supabase = createSupabaseBrowserClient()

// Fetch the existing components
const { data: existingEntity, error: fetchError } = await supabase
.from('entities')
.select('components')
.eq('id', id)
.single()

if (fetchError) {
return { error: fetchError.message }
}

// Update the specific component in the JSONB object
const updatedComponents = {
...(typeof existingEntity.components === 'object' &&
existingEntity.components !== null
? existingEntity.components
: {}),
[componentKey]: updatedComponentData
}

const { data, error } = await supabase
.from('entities')
.update({ components: updatedComponents })
.eq('id', id)
.single()

if (error) {
return { error: error.message }
}

return { data }
},
invalidatesTags: (result, error, { id }) => [
{ type: TAG_NAME_FOR_GENERAL_ENTITY, id }
]
}),

deleteComponentFromEntity: builder.mutation<
any,
{
id: EntityId
componentKey: string
}
>({
queryFn: async ({ id, componentKey }) => {
const supabase = createSupabaseBrowserClient()

// Fetch the existing components
const { data: existingEntity, error: fetchError } = await supabase
.from('entities')
.select('components')
.eq('id', id)
.single()

if (fetchError) {
return { error: fetchError.message }
}

// Ensure existingEntity.components is typed correctly
const components = existingEntity.components as Record<string, any>

// Remove the specific component from the JSONB object
const { [componentKey]: _, ...remainingComponents } = components

const { data, error } = await supabase
.from('entities')
.update({ components: remainingComponents })
.eq('id', id)
.single()

if (error) {
return { error: error.message }
}

return { data }
},
invalidatesTags: (result, error, { id }) => [
{ type: TAG_NAME_FOR_GENERAL_ENTITY, id }
]
})

//
})
})

Expand Down Expand Up @@ -530,5 +693,10 @@ export const {
useUpdateEntityMutation,
useGetSingleEntityQuery,
useLazyGetAllEntitiesQuery,
useDeleteEntityMutation
useDeleteEntityMutation,

useAddComponentToEntityMutation,
useGetComponentsOfEntityQuery,
useUpdateComponentOnEntityMutation,
useDeleteComponentFromEntityMutation
} = entitiesApi

0 comments on commit 3c82b2a

Please sign in to comment.