mirror of
https://github.com/utopia-os/utopia-ui.git
synced 2025-12-12 23:36:00 +00:00
fix(lib): fix user_created reset when updating other users profile marker (#380)
* fix user_created new set when updating other users profile marker * fix same issue on different places * fix linting * fix tests
This commit is contained in:
parent
5de172e739
commit
298876a269
@ -127,14 +127,14 @@ export function ItemFormPopup(props: Props) {
|
|||||||
const itemWithLayer = {
|
const itemWithLayer = {
|
||||||
...result.data,
|
...result.data,
|
||||||
layer: popupForm.layer,
|
layer: popupForm.layer,
|
||||||
user_created: user ?? undefined,
|
user_created: formItem.user_created,
|
||||||
}
|
}
|
||||||
updateItem(itemWithLayer)
|
updateItem(itemWithLayer)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result.success
|
return result.success
|
||||||
},
|
},
|
||||||
[popupForm, handleApiOperation, updateItem, user],
|
[popupForm, handleApiOperation, updateItem],
|
||||||
)
|
)
|
||||||
|
|
||||||
// Create new item or update existing user profile
|
// Create new item or update existing user profile
|
||||||
|
|||||||
@ -11,8 +11,6 @@
|
|||||||
import { createContext, useContext, useEffect, useState, useCallback } from 'react'
|
import { createContext, useContext, useEffect, useState, useCallback } from 'react'
|
||||||
import { toast } from 'react-toastify'
|
import { toast } from 'react-toastify'
|
||||||
|
|
||||||
import { useAuth } from '#components/Auth/useAuth'
|
|
||||||
|
|
||||||
import { useUpdateItem } from './useItems'
|
import { useUpdateItem } from './useItems'
|
||||||
import { useLayers } from './useLayers'
|
import { useLayers } from './useLayers'
|
||||||
import { useHasUserPermission } from './usePermissions'
|
import { useHasUserPermission } from './usePermissions'
|
||||||
@ -49,7 +47,6 @@ function useSelectPositionManager(): {
|
|||||||
const updateItem = useUpdateItem()
|
const updateItem = useUpdateItem()
|
||||||
const hasUserPermission = useHasUserPermission()
|
const hasUserPermission = useHasUserPermission()
|
||||||
const layers = useLayers()
|
const layers = useLayers()
|
||||||
const { user } = useAuth()
|
|
||||||
|
|
||||||
// Handle API operations with consistent error handling and return response data
|
// Handle API operations with consistent error handling and return response data
|
||||||
const handleApiOperation = useCallback(
|
const handleApiOperation = useCallback(
|
||||||
@ -144,7 +141,7 @@ function useSelectPositionManager(): {
|
|||||||
if (result.success && result.data) {
|
if (result.success && result.data) {
|
||||||
// Find the layer object by ID from server response
|
// Find the layer object by ID from server response
|
||||||
const layer = layers.find((l) => l.id === (result.data!.layer as unknown as string))
|
const layer = layers.find((l) => l.id === (result.data!.layer as unknown as string))
|
||||||
const itemWithLayer = { ...result.data, layer, user_created: user ?? undefined }
|
const itemWithLayer = { ...result.data, layer, user_created: updatedItem.user_created }
|
||||||
updateItem(itemWithLayer)
|
updateItem(itemWithLayer)
|
||||||
await linkItem(updatedItem.id)
|
await linkItem(updatedItem.id)
|
||||||
setSelectPosition(null)
|
setSelectPosition(null)
|
||||||
@ -178,9 +175,8 @@ function useSelectPositionManager(): {
|
|||||||
)
|
)
|
||||||
|
|
||||||
if (result.success && result.data) {
|
if (result.success && result.data) {
|
||||||
// Find the layer object by ID from server response
|
|
||||||
const layer = layers.find((l) => l.id === (result.data!.layer as unknown as string))
|
const layer = layers.find((l) => l.id === (result.data!.layer as unknown as string))
|
||||||
const itemWithLayer = { ...result.data, layer, user_created: user ?? undefined }
|
const itemWithLayer = { ...result.data, layer, user_created: updatedItem.user_created }
|
||||||
updateItem(itemWithLayer)
|
updateItem(itemWithLayer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -206,7 +202,7 @@ function useSelectPositionManager(): {
|
|||||||
if (result.success && result.data) {
|
if (result.success && result.data) {
|
||||||
// Find the layer object by ID from server response
|
// Find the layer object by ID from server response
|
||||||
const layer = layers.find((l) => l.id === (result.data!.layer as unknown as string))
|
const layer = layers.find((l) => l.id === (result.data!.layer as unknown as string))
|
||||||
const itemWithLayer = { ...result.data, layer, user_created: user ?? undefined }
|
const itemWithLayer = { ...result.data, layer, user_created: markerClicked.user_created }
|
||||||
updateItem(itemWithLayer)
|
updateItem(itemWithLayer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
import { describe, it, expect, vi } from 'vitest'
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||||
|
|
||||||
import { linkItem } from './itemFunctions'
|
import { linkItem } from './itemFunctions'
|
||||||
|
|
||||||
@ -22,12 +22,6 @@ vi.mock('react-toastify', () => ({
|
|||||||
describe('linkItem', () => {
|
describe('linkItem', () => {
|
||||||
const id = 'some-id'
|
const id = 'some-id'
|
||||||
let updateApi: (item: Partial<Item>) => Promise<Item> = vi.fn()
|
let updateApi: (item: Partial<Item>) => Promise<Item> = vi.fn()
|
||||||
const mockUser = {
|
|
||||||
id: 'user-1',
|
|
||||||
first_name: 'Test',
|
|
||||||
last_name: 'User',
|
|
||||||
email: 'test@example.com',
|
|
||||||
}
|
|
||||||
const item: Item = {
|
const item: Item = {
|
||||||
layer: {
|
layer: {
|
||||||
id: 'test-layer-id',
|
id: 'test-layer-id',
|
||||||
@ -75,11 +69,16 @@ describe('linkItem', () => {
|
|||||||
|
|
||||||
describe('api rejects', () => {
|
describe('api rejects', () => {
|
||||||
it('toasts an error', async () => {
|
it('toasts an error', async () => {
|
||||||
updateApi = vi.fn().mockRejectedValue('autsch')
|
updateApi = vi.fn().mockRejectedValue(new Error('autsch'))
|
||||||
await linkItem(id, item, updateItem, mockUser)
|
await linkItem(id, item, updateItem)
|
||||||
expect(toastUpdateMock).toHaveBeenCalledWith(123, expect.objectContaining({ type: 'error' }))
|
expect(toastUpdateMock).toHaveBeenCalledWith(
|
||||||
|
123,
|
||||||
|
expect.objectContaining({
|
||||||
|
type: 'error',
|
||||||
|
render: 'autsch',
|
||||||
|
}),
|
||||||
|
)
|
||||||
expect(updateItem).not.toHaveBeenCalled()
|
expect(updateItem).not.toHaveBeenCalled()
|
||||||
expect(toastSuccessMock).not.toHaveBeenCalled()
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -92,16 +91,21 @@ describe('linkItem', () => {
|
|||||||
}
|
}
|
||||||
updateApi = vi.fn().mockResolvedValue(serverResponse)
|
updateApi = vi.fn().mockResolvedValue(serverResponse)
|
||||||
|
|
||||||
await linkItem(id, item, updateItem, mockUser)
|
await linkItem(id, item, updateItem)
|
||||||
|
|
||||||
expect(toastUpdateMock).toHaveBeenCalledWith(
|
expect(toastUpdateMock).toHaveBeenCalledWith(
|
||||||
123,
|
123,
|
||||||
expect.objectContaining({ type: 'success' }),
|
expect.objectContaining({
|
||||||
|
type: 'success',
|
||||||
|
render: 'Item linked',
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
expect(updateItem).toHaveBeenCalledWith(
|
expect(updateItem).toHaveBeenCalledWith(
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
...serverResponse,
|
...serverResponse,
|
||||||
layer: item.layer,
|
layer: item.layer,
|
||||||
|
relations: [{ items_id: item.id, related_items_id: id }],
|
||||||
|
user_created: item.user_created,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
@ -198,8 +198,8 @@ export function ProfileForm() {
|
|||||||
state={state}
|
state={state}
|
||||||
setState={setState}
|
setState={setState}
|
||||||
updatePermission={updatePermission}
|
updatePermission={updatePermission}
|
||||||
linkItem={(id: string) => linkItem(id, item, updateItem, user)}
|
linkItem={(id: string) => linkItem(id, item, updateItem)}
|
||||||
unlinkItem={(id: string) => unlinkItem(id, item, updateItem, user)}
|
unlinkItem={(id: string) => unlinkItem(id, item, updateItem)}
|
||||||
setUrlParams={setUrlParams}
|
setUrlParams={setUrlParams}
|
||||||
></TabsForm>
|
></TabsForm>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@ -12,7 +12,6 @@ import { useEffect, useState } from 'react'
|
|||||||
import { useMap } from 'react-leaflet'
|
import { useMap } from 'react-leaflet'
|
||||||
import { useLocation, useNavigate } from 'react-router-dom'
|
import { useLocation, useNavigate } from 'react-router-dom'
|
||||||
|
|
||||||
import { useAuth } from '#components/Auth/useAuth'
|
|
||||||
import { useClusterRef } from '#components/Map/hooks/useClusterRef'
|
import { useClusterRef } from '#components/Map/hooks/useClusterRef'
|
||||||
import { useItems, useRemoveItem, useUpdateItem } from '#components/Map/hooks/useItems'
|
import { useItems, useRemoveItem, useUpdateItem } from '#components/Map/hooks/useItems'
|
||||||
import { useLayers } from '#components/Map/hooks/useLayers'
|
import { useLayers } from '#components/Map/hooks/useLayers'
|
||||||
@ -52,7 +51,6 @@ export function ProfileView({ attestationApi }: { attestationApi?: ItemsApi<any>
|
|||||||
const map = useMap()
|
const map = useMap()
|
||||||
const selectPosition = useSelectPosition()
|
const selectPosition = useSelectPosition()
|
||||||
const removeItem = useRemoveItem()
|
const removeItem = useRemoveItem()
|
||||||
const { user } = useAuth()
|
|
||||||
const tags = useTags()
|
const tags = useTags()
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const hasUserPermission = useHasUserPermission()
|
const hasUserPermission = useHasUserPermission()
|
||||||
@ -210,8 +208,8 @@ export function ProfileView({ attestationApi }: { attestationApi?: ItemsApi<any>
|
|||||||
needs={needs}
|
needs={needs}
|
||||||
relations={relations}
|
relations={relations}
|
||||||
updatePermission={updatePermission}
|
updatePermission={updatePermission}
|
||||||
linkItem={(id) => linkItem(id, item, updateItem, user)}
|
linkItem={(id) => linkItem(id, item, updateItem)}
|
||||||
unlinkItem={(id) => unlinkItem(id, item, updateItem, user)}
|
unlinkItem={(id) => unlinkItem(id, item, updateItem)}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
|
|||||||
@ -116,7 +116,7 @@ export const submitNewItem = async (
|
|||||||
setAddItemPopupType('')
|
setAddItemPopupType('')
|
||||||
}
|
}
|
||||||
|
|
||||||
export const linkItem = async (id: string, item: Item, updateItem, user) => {
|
export const linkItem = async (id: string, item: Item, updateItem) => {
|
||||||
const newRelations = item.relations ?? []
|
const newRelations = item.relations ?? []
|
||||||
newRelations?.push({ items_id: item.id, related_items_id: id })
|
newRelations?.push({ items_id: item.id, related_items_id: id })
|
||||||
const updatedItem = { id: item.id, relations: newRelations }
|
const updatedItem = { id: item.id, relations: newRelations }
|
||||||
@ -139,13 +139,13 @@ export const linkItem = async (id: string, item: Item, updateItem, user) => {
|
|||||||
...result.data,
|
...result.data,
|
||||||
layer,
|
layer,
|
||||||
relations: newRelations,
|
relations: newRelations,
|
||||||
user_created: user ?? undefined,
|
user_created: item.user_created,
|
||||||
}
|
}
|
||||||
updateItem(itemWithLayer)
|
updateItem(itemWithLayer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const unlinkItem = async (id: string, item: Item, updateItem, user) => {
|
export const unlinkItem = async (id: string, item: Item, updateItem) => {
|
||||||
const newRelations = item.relations?.filter((r) => r.related_items_id !== id)
|
const newRelations = item.relations?.filter((r) => r.related_items_id !== id)
|
||||||
const updatedItem = { id: item.id, relations: newRelations }
|
const updatedItem = { id: item.id, relations: newRelations }
|
||||||
|
|
||||||
@ -163,7 +163,7 @@ export const unlinkItem = async (id: string, item: Item, updateItem, user) => {
|
|||||||
if (result.success && result.data) {
|
if (result.success && result.data) {
|
||||||
// Find the layer object by ID from server response or use existing layer
|
// Find the layer object by ID from server response or use existing layer
|
||||||
const layer = item.layer
|
const layer = item.layer
|
||||||
const itemWithLayer = { ...result.data, layer, user_created: user ?? undefined }
|
const itemWithLayer = { ...result.data, layer, user_created: item.user_created }
|
||||||
updateItem(itemWithLayer)
|
updateItem(itemWithLayer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -308,7 +308,7 @@ export const onUpdateItem = async (
|
|||||||
layer: item.layer,
|
layer: item.layer,
|
||||||
markerIcon: state.marker_icon,
|
markerIcon: state.marker_icon,
|
||||||
gallery: state.gallery,
|
gallery: state.gallery,
|
||||||
user_created: user ?? undefined,
|
user_created: item.user_created,
|
||||||
}
|
}
|
||||||
updateItem(itemWithLayer)
|
updateItem(itemWithLayer)
|
||||||
navigate(`/item/${item.id}${params && '?' + params}`)
|
navigate(`/item/${item.id}${params && '?' + params}`)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user