mirror of
https://github.com/utopia-os/utopia-ui.git
synced 2025-12-13 07:46:10 +00:00
fix(source): fixed edit item form init process (#234)
* fixed edit item form init process * fix linting
This commit is contained in:
parent
483c3f0857
commit
5927ba8c16
@ -22,7 +22,7 @@ export const UserControl = () => {
|
||||
user && items.find((i) => i.user_created?.id === user.id && i.layer?.userProfileLayer)
|
||||
profile
|
||||
? setUserProfile(profile)
|
||||
: setUserProfile({ id: crypto.randomUUID(), name: user?.first_name ?? '', text: '' })
|
||||
: setUserProfile({ id: 'new', name: user?.first_name ?? '', text: '' })
|
||||
}, [user, items])
|
||||
|
||||
const onLogout = async () => {
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useLocation, useNavigate } from 'react-router-dom'
|
||||
import { toast } from 'react-toastify'
|
||||
|
||||
import { useAuth } from '#components/Auth/useAuth'
|
||||
import { useItems, useUpdateItem, useAddItem } from '#components/Map/hooks/useItems'
|
||||
@ -51,7 +50,7 @@ export function ProfileForm() {
|
||||
|
||||
const [updatePermission, setUpdatePermission] = useState<boolean>(false)
|
||||
const [loading, setLoading] = useState<boolean>(false)
|
||||
const [item, setItem] = useState<Item>({} as Item)
|
||||
const [item, setItem] = useState<Item>()
|
||||
const { user } = useAuth()
|
||||
const updateItem = useUpdateItem()
|
||||
const addItem = useAddItem()
|
||||
@ -72,15 +71,7 @@ export function ProfileForm() {
|
||||
|
||||
useEffect(() => {
|
||||
const itemId = location.pathname.split('/')[2]
|
||||
|
||||
const item = items.find((i) => i.id === itemId)
|
||||
item && setItem(item)
|
||||
|
||||
if (!item) {
|
||||
if (items.some((i) => i.user_created?.id === user?.id && i.layer?.userProfileLayer)) {
|
||||
navigate('/')
|
||||
toast.error('Item does not exist')
|
||||
} else {
|
||||
if (itemId === 'new') {
|
||||
const layer = layers.find((l) => l.userProfileLayer)
|
||||
setItem({
|
||||
id: crypto.randomUUID(),
|
||||
@ -89,13 +80,14 @@ export function ProfileForm() {
|
||||
layer,
|
||||
new: true,
|
||||
})
|
||||
} else {
|
||||
const item = items.find((i) => i.id === itemId)
|
||||
if (item) setItem(item)
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [items])
|
||||
}, [items, location.pathname, layers, user?.first_name])
|
||||
|
||||
useEffect(() => {
|
||||
if (!item) return
|
||||
const newColor =
|
||||
item.color ??
|
||||
(getItemTags(item) && getItemTags(item)[0]?.color
|
||||
@ -147,6 +139,7 @@ export function ProfileForm() {
|
||||
const [template, setTemplate] = useState<string>('')
|
||||
|
||||
useEffect(() => {
|
||||
if (!item) return
|
||||
if (item.layer?.itemType.template) setTemplate(item.layer?.itemType.template)
|
||||
else {
|
||||
const userLayer = layers.find((l) => l.userProfileLayer === true)
|
||||
@ -160,6 +153,7 @@ export function ProfileForm() {
|
||||
backdrop
|
||||
className='tw:mx-4 tw:mt-4 tw:mb-4 tw:overflow-x-hidden tw:w-[calc(100%-32px)] tw:md:w-[calc(50%-32px)] tw:max-w-3xl tw:left-auto! tw:top-0 tw:bottom-0'
|
||||
>
|
||||
{item ? (
|
||||
<form
|
||||
className='tw:h-full'
|
||||
onSubmit={(e) => {
|
||||
@ -219,6 +213,11 @@ export function ProfileForm() {
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
) : (
|
||||
<div className='tw:h-full tw:flex tw:flex-col tw:items-center tw:justify-center'>
|
||||
<div className='tw:loading tw:loading-spinner'></div>
|
||||
</div>
|
||||
)}
|
||||
</MapOverlayPage>
|
||||
</>
|
||||
)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user