From 5927ba8c1686cd85d3c6b09b7d971db946cbebc3 Mon Sep 17 00:00:00 2001 From: Anton Tranelis <31516529+antontranelis@users.noreply.github.com> Date: Tue, 10 Jun 2025 10:27:05 +0200 Subject: [PATCH] fix(source): fixed edit item form init process (#234) * fixed edit item form init process * fix linting --- src/Components/AppShell/UserControl.tsx | 2 +- src/Components/Profile/ProfileForm.tsx | 149 ++++++++++++------------ 2 files changed, 75 insertions(+), 76 deletions(-) diff --git a/src/Components/AppShell/UserControl.tsx b/src/Components/AppShell/UserControl.tsx index 03c1669b..2599708c 100644 --- a/src/Components/AppShell/UserControl.tsx +++ b/src/Components/AppShell/UserControl.tsx @@ -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 () => { diff --git a/src/Components/Profile/ProfileForm.tsx b/src/Components/Profile/ProfileForm.tsx index c68f7dcc..06019cec 100644 --- a/src/Components/Profile/ProfileForm.tsx +++ b/src/Components/Profile/ProfileForm.tsx @@ -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(false) const [loading, setLoading] = useState(false) - const [item, setItem] = useState({} as Item) + const [item, setItem] = useState() const { user } = useAuth() const updateItem = useUpdateItem() const addItem = useAddItem() @@ -72,30 +71,23 @@ 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 { - const layer = layers.find((l) => l.userProfileLayer) - setItem({ - id: crypto.randomUUID(), - name: user?.first_name ?? '', - text: '', - layer, - new: true, - }) - } + if (itemId === 'new') { + const layer = layers.find((l) => l.userProfileLayer) + setItem({ + id: crypto.randomUUID(), + name: user?.first_name ?? '', + text: '', + 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('') useEffect(() => { + if (!item) return if (item.layer?.itemType.template) setTemplate(item.layer?.itemType.template) else { const userLayer = layers.find((l) => l.userProfileLayer === true) @@ -160,65 +153,71 @@ 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' > -
{ - e.preventDefault() - void onUpdateItem( - state, - item, - tags, - addTag, - setLoading, - navigate, - updateItem, - addItem, - user, - urlParams, - ) - }} - > -
- + {item ? ( + { + e.preventDefault() + void onUpdateItem( + state, + item, + tags, + addTag, + setLoading, + navigate, + updateItem, + addItem, + user, + urlParams, + ) + }} + > +
+ - {template === 'onepager' && ( - - )} + {template === 'onepager' && ( + + )} - {template === 'simple' && } + {template === 'simple' && } - {template === 'flex' && ( - - )} + {template === 'flex' && ( + + )} - {template === 'tabs' && ( - linkItem(id, item, updateItem)} - unlinkItem={(id) => unlinkItem(id, item, updateItem)} - setUrlParams={setUrlParams} - > - )} + {template === 'tabs' && ( + linkItem(id, item, updateItem)} + unlinkItem={(id) => unlinkItem(id, item, updateItem)} + setUrlParams={setUrlParams} + > + )} -
- +
+ +
+ + ) : ( +
+
- + )} )