adding new tag bugfixing

This commit is contained in:
Anton 2023-08-31 22:37:01 +02:00
parent 8460e87a9b
commit d6ada13e19

View File

@ -6,10 +6,11 @@ import { useAddItem, useUpdateItem } from '../hooks/useItems'
import { Geometry, LayerProps, Item, ItemsApi } from '../../../types' import { Geometry, LayerProps, Item, ItemsApi } from '../../../types'
import { TextAreaInput } from '../../Input/TextAreaInput' import { TextAreaInput } from '../../Input/TextAreaInput'
import { TextInput } from '../../Input/TextInput' import { TextInput } from '../../Input/TextInput'
import { hashTagRegex } from '../../../Utils/HashTagRegex'
import { useAddTag } from '../hooks/useTags'
import { randomColor } from '../../../Utils/RandomColor'
import { toast } from 'react-toastify' import { toast } from 'react-toastify'
import { useResetFilterTags } from '../hooks/useFilter'
import { hashTagRegex } from '../../../Utils/HashTagRegex'
import { randomColor } from '../../../Utils/RandomColor'
import { useAddTag, useTags } from '../hooks/useTags'
export interface ItemFormPopupProps { export interface ItemFormPopupProps {
position: LatLng, position: LatLng,
@ -30,6 +31,11 @@ export function ItemFormPopup(props: ItemFormPopupProps) {
const addItem = useAddItem(); const addItem = useAddItem();
const updateItem = useUpdateItem(); const updateItem = useUpdateItem();
const tags = useTags();
const addTag = useAddTag();
const resetFilterTags = useResetFilterTags();
const handleSubmit = async (evt: any) => { const handleSubmit = async (evt: any) => {
const formItem: Item = {} as Item; const formItem: Item = {} as Item;
@ -42,6 +48,15 @@ export function ItemFormPopup(props: ItemFormPopupProps) {
evt.preventDefault(); evt.preventDefault();
setSpinner(true); setSpinner(true);
formItem.text.toLocaleLowerCase().match(hashTagRegex)?.map(tag=> {
if (!tags.find((t) => t.id === tag.slice(1))) {
console.log(tag);
addTag({id: tag.slice(1), color: randomColor()})
}
});
if(props.item) { if(props.item) {
let success = false; let success = false;
@ -49,9 +64,12 @@ export function ItemFormPopup(props: ItemFormPopupProps) {
await props.layer.api?.updateItem!({...formItem, id: props.item.id}); await props.layer.api?.updateItem!({...formItem, id: props.item.id});
success = true; success = true;
} catch (error) { } catch (error) {
console.log(); toast.error(error.toString);
}
if(success) {
updateItem({...props.item, ...formItem});
toast.success("Item updated");
} }
success&&updateItem({...props.item, ...formItem});
setSpinner(false); setSpinner(false);
map.closePopup(); map.closePopup();
} }
@ -64,7 +82,11 @@ export function ItemFormPopup(props: ItemFormPopupProps) {
} catch (error) { } catch (error) {
toast.error(error.toString); toast.error(error.toString);
} }
success&&addItem({...formItem, id: crypto.randomUUID(), layer: props.layer}); if(success) {
addItem({...formItem, id: crypto.randomUUID(), layer: props.layer});
toast.success("New item created");
resetFilterTags();
}
setSpinner(false); setSpinner(false);
map.closePopup(); map.closePopup();
} }