import { useState, SyntheticEvent, Fragment, ChangeEvent, useEffect } from 'react'; import { connect } from 'react-redux'; import ModalForm from '../../UI/Forms/ModalForm/ModalForm'; import InputGroup from '../../UI/Forms/InputGroup/InputGroup'; import { Bookmark, Category, GlobalState, NewBookmark, NewCategory, NewNotification } from '../../../interfaces'; import { ContentType } from '../Bookmarks'; import { getCategories, addCategory, addBookmark, updateCategory, updateBookmark, createNotification } from '../../../store/actions'; import Button from '../../UI/Buttons/Button/Button'; import classes from './BookmarkForm.module.css'; interface ComponentProps { modalHandler: () => void; contentType: ContentType; categories: Category[]; category?: Category; bookmark?: Bookmark; addCategory: (formData: NewCategory) => void; addBookmark: (formData: NewBookmark | FormData) => void; updateCategory: (id: number, formData: NewCategory) => void; updateBookmark: ( id: number, formData: NewBookmark | FormData, category: { prev: number; curr: number; } ) => void; createNotification: (notification: NewNotification) => void; } const BookmarkForm = (props: ComponentProps): JSX.Element => { const [useCustomIcon, toggleUseCustomIcon] = useState(false); const [customIcon, setCustomIcon] = useState(null); const [categoryName, setCategoryName] = useState({ name: '' }); const [formData, setFormData] = useState({ name: '', url: '', categoryId: -1, icon: '' }); // Load category data if provided for editing useEffect(() => { if (props.category) { setCategoryName({ name: props.category.name }); } else { setCategoryName({ name: '' }); } }, [props.category]); // Load bookmark data if provided for editing useEffect(() => { if (props.bookmark) { setFormData({ name: props.bookmark.name, url: props.bookmark.url, categoryId: props.bookmark.categoryId, icon: props.bookmark.icon }); } else { setFormData({ name: '', url: '', categoryId: -1, icon: '' }); } }, [props.bookmark]); const formSubmitHandler = (e: SyntheticEvent): void => { e.preventDefault(); const createFormData = (): FormData => { const data = new FormData(); if (customIcon) { data.append('icon', customIcon); } data.append('name', formData.name); data.append('url', formData.url); data.append('categoryId', `${formData.categoryId}`); return data; }; if (!props.category && !props.bookmark) { // Add new if (props.contentType === ContentType.category) { // Add category props.addCategory(categoryName); setCategoryName({ name: '' }); } else if (props.contentType === ContentType.bookmark) { // Add bookmark if (formData.categoryId === -1) { props.createNotification({ title: 'Error', message: 'Please select category' }); return; } if (customIcon) { const data = createFormData(); props.addBookmark(data); } else { props.addBookmark(formData); } setFormData({ name: '', url: '', categoryId: formData.categoryId, icon: '' }); setCustomIcon(null); } } else { // Update if (props.contentType === ContentType.category && props.category) { // Update category props.updateCategory(props.category.id, categoryName); setCategoryName({ name: '' }); } else if (props.contentType === ContentType.bookmark && props.bookmark) { // Update bookmark if (customIcon) { const data = createFormData(); props.updateBookmark(props.bookmark.id, data, { prev: props.bookmark.categoryId, curr: formData.categoryId }); } else { props.updateBookmark(props.bookmark.id, formData, { prev: props.bookmark.categoryId, curr: formData.categoryId }); } setFormData({ name: '', url: '', categoryId: -1, icon: '' }); setCustomIcon(null); } props.modalHandler(); } }; const inputChangeHandler = (e: ChangeEvent): void => { setFormData({ ...formData, [e.target.name]: e.target.value }); }; const selectChangeHandler = (e: ChangeEvent): void => { setFormData({ ...formData, categoryId: parseInt(e.target.value) }); }; const fileChangeHandler = (e: ChangeEvent): void => { if (e.target.files) { setCustomIcon(e.target.files[0]); } }; let button = ; if (!props.category && !props.bookmark) { if (props.contentType === ContentType.category) { button = ; } else { button = ; } } else if (props.category) { button = ; } else if (props.bookmark) { button = ; } return ( {props.contentType === ContentType.category ? ( setCategoryName({ name: e.target.value })} /> ) : ( inputChangeHandler(e)} /> inputChangeHandler(e)} /> {' '} Check supported URL formats {!useCustomIcon ? ( // mdi inputChangeHandler(e)} /> Use icon name from MDI. {' '} Click here for reference toggleUseCustomIcon(!useCustomIcon)} className={classes.Switch} > Switch to custom icon upload ) : ( // custom fileChangeHandler(e)} accept='.jpg,.jpeg,.png,.svg' /> toggleUseCustomIcon(!useCustomIcon)} className={classes.Switch} > Switch to MDI )} )} {button} ); }; const mapStateToProps = (state: GlobalState) => { return { categories: state.bookmark.categories }; }; const dispatchMap = { getCategories, addCategory, addBookmark, updateCategory, updateBookmark, createNotification }; export default connect(mapStateToProps, dispatchMap)(BookmarkForm);