1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-20 03:59:36 +02:00
flame/client/src/components/Bookmarks/BookmarkForm/BookmarkForm.tsx

184 lines
5.2 KiB
TypeScript
Raw Normal View History

2021-05-24 11:51:05 +02:00
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';
2021-05-26 13:13:56 +02:00
import { Category, GlobalState, NewBookmark, NewCategory, NewNotification } from '../../../interfaces';
2021-05-25 11:44:11 +02:00
import { ContentType } from '../Bookmarks';
2021-05-26 13:13:56 +02:00
import { getCategories, addCategory, addBookmark, updateCategory, createNotification } from '../../../store/actions';
import Button from '../../UI/Buttons/Button/Button';
2021-05-24 11:51:05 +02:00
interface ComponentProps {
modalHandler: () => void;
2021-05-25 11:44:11 +02:00
contentType: ContentType;
2021-05-24 11:51:05 +02:00
categories: Category[];
2021-05-26 13:13:56 +02:00
category?: Category;
addCategory: (formData: NewCategory) => void;
addBookmark: (formData: NewBookmark) => void;
2021-05-26 13:13:56 +02:00
updateCategory: (id: number, formData: NewCategory) => void;
createNotification: (notification: NewNotification) => void;
2021-05-24 11:51:05 +02:00
}
const BookmarkForm = (props: ComponentProps): JSX.Element => {
const [categoryName, setCategoryName] = useState<NewCategory>({
name: ''
})
const [formData, setFormData] = useState<NewBookmark>({
name: '',
url: '',
categoryId: -1
})
2021-05-26 13:13:56 +02:00
useEffect(() => {
if (props.category) {
setCategoryName({ name: props.category.name });
} else {
setCategoryName({ name: '' });
}
}, [props.category])
2021-05-24 11:51:05 +02:00
const formSubmitHandler = (e: SyntheticEvent<HTMLFormElement>): void => {
e.preventDefault();
2021-05-26 13:13:56 +02:00
if (!props.category) {
// Add new
if (props.contentType === ContentType.category) {
props.addCategory(categoryName);
setCategoryName({ name: '' });
} else if (props.contentType === ContentType.bookmark) {
if (formData.categoryId === -1) {
props.createNotification({
title: 'Error',
message: 'Please select category'
})
return;
}
props.addBookmark(formData);
setFormData({
name: '',
url: '',
categoryId: formData.categoryId
})
}
} else {
// Update
if (props.contentType === ContentType.category) {
props.updateCategory(props.category.id, categoryName);
setCategoryName({ name: '' });
}
2021-05-26 13:13:56 +02:00
props.modalHandler();
2021-05-24 11:51:05 +02:00
}
}
const inputChangeHandler = (e: ChangeEvent<HTMLInputElement>): void => {
setFormData({
...formData,
[e.target.name]: e.target.value
})
}
const selectChangeHandler = (e: ChangeEvent<HTMLSelectElement>): void => {
setFormData({
...formData,
categoryId: parseInt(e.target.value)
})
}
return (
<ModalForm
modalHandler={props.modalHandler}
formHandler={formSubmitHandler}
>
2021-05-25 11:44:11 +02:00
{props.contentType === ContentType.category
2021-05-24 11:51:05 +02:00
? (
<Fragment>
<InputGroup>
<label htmlFor='categoryName'>Category Name</label>
<input
type='text'
name='categoryName'
id='categoryName'
placeholder='Social Media'
required
value={categoryName.name}
onChange={(e) => setCategoryName({ name: e.target.value })}
/>
</InputGroup>
</Fragment>
)
: (
<Fragment>
<InputGroup>
<label htmlFor='name'>Bookmark Name</label>
<input
type='text'
name='name'
id='name'
placeholder='Reddit'
required
value={formData.name}
onChange={(e) => inputChangeHandler(e)}
/>
</InputGroup>
<InputGroup>
<label htmlFor='url'>Bookmark URL</label>
<input
type='text'
name='url'
id='url'
placeholder='reddit.com'
required
value={formData.url}
onChange={(e) => inputChangeHandler(e)}
/>
</InputGroup>
<InputGroup>
<label htmlFor='categoryId'>Bookmark Category</label>
<select
name='categoryId'
id='categoryId'
required
onChange={(e) => selectChangeHandler(e)}
>
<option value={-1}>Select category</option>
{props.categories.map((category: Category): JSX.Element => {
return (
<option
key={category.id}
value={category.id}
>
{category.name}
</option>
)
})}
</select>
</InputGroup>
</Fragment>
)
}
2021-05-26 13:13:56 +02:00
{!props.category
? <Button>Add new category</Button>
: <Button>Update category</Button>
}
2021-05-24 11:51:05 +02:00
</ModalForm>
)
}
const mapStateToProps = (state: GlobalState) => {
return {
categories: state.bookmark.categories
}
}
2021-05-26 13:13:56 +02:00
const dispatchMap = {
getCategories,
addCategory,
addBookmark,
updateCategory,
createNotification
}
export default connect(mapStateToProps, dispatchMap)(BookmarkForm);