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

261 lines
7.5 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-28 13:41:27 +02:00
import { Bookmark, Category, GlobalState, NewBookmark, NewCategory, NewNotification } from '../../../interfaces';
2021-05-25 11:44:11 +02:00
import { ContentType } from '../Bookmarks';
2021-05-28 13:41:27 +02:00
import { getCategories, addCategory, addBookmark, updateCategory, updateBookmark, createNotification } from '../../../store/actions';
2021-05-26 13:13:56 +02:00
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;
2021-05-28 13:41:27 +02:00
bookmark?: Bookmark;
addCategory: (formData: NewCategory) => void;
addBookmark: (formData: NewBookmark) => void;
2021-05-26 13:13:56 +02:00
updateCategory: (id: number, formData: NewCategory) => void;
2021-06-05 15:48:43 +02:00
updateBookmark: (id: number, formData: NewBookmark, previousCategoryId: number) => void;
2021-05-26 13:13:56 +02:00
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,
icon: ''
2021-05-24 11:51:05 +02:00
})
// Load category data if provided for editing
2021-05-26 13:13:56 +02:00
useEffect(() => {
if (props.category) {
setCategoryName({ name: props.category.name });
} else {
setCategoryName({ name: '' });
}
}, [props.category])
// Load bookmark data if provided for editing
2021-05-28 13:41:27 +02:00
useEffect(() => {
if (props.bookmark) {
setFormData({
name: props.bookmark.name,
url: props.bookmark.url,
categoryId: props.bookmark.categoryId,
icon: props.bookmark.icon
2021-05-28 13:41:27 +02:00
})
} else {
setFormData({
name: '',
url: '',
categoryId: -1,
icon: ''
2021-05-28 13:41:27 +02:00
})
}
}, [props.bookmark])
2021-05-24 11:51:05 +02:00
const formSubmitHandler = (e: SyntheticEvent<HTMLFormElement>): void => {
e.preventDefault();
2021-05-26 13:13:56 +02:00
2021-05-28 13:41:27 +02:00
if (!props.category && !props.bookmark) {
2021-05-26 13:13:56 +02:00
// Add new
if (props.contentType === ContentType.category) {
2021-05-28 13:41:27 +02:00
// Add category
2021-05-26 13:13:56 +02:00
props.addCategory(categoryName);
setCategoryName({ name: '' });
} else if (props.contentType === ContentType.bookmark) {
2021-05-28 13:41:27 +02:00
// Add bookmark
2021-05-26 13:13:56 +02:00
if (formData.categoryId === -1) {
props.createNotification({
title: 'Error',
message: 'Please select category'
})
return;
}
props.addBookmark(formData);
setFormData({
name: '',
url: '',
categoryId: formData.categoryId,
icon: ''
2021-05-26 13:13:56 +02:00
})
}
} else {
// Update
2021-05-28 13:41:27 +02:00
if (props.contentType === ContentType.category && props.category) {
// Update category
2021-05-26 13:13:56 +02:00
props.updateCategory(props.category.id, categoryName);
setCategoryName({ name: '' });
2021-05-28 13:41:27 +02:00
} else if (props.contentType === ContentType.bookmark && props.bookmark) {
// Update bookmark
2021-06-05 15:48:43 +02:00
props.updateBookmark(props.bookmark.id, formData, props.bookmark.categoryId);
2021-05-28 13:41:27 +02:00
setFormData({
name: '',
url: '',
categoryId: -1,
icon: ''
2021-05-28 13:41:27 +02:00
})
}
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)
})
}
2021-05-28 13:41:27 +02:00
let button = <Button>Submit</Button>
if (!props.category && !props.bookmark) {
if (props.contentType === ContentType.category) {
button = <Button>Add new category</Button>;
} else {
button = <Button>Add new bookmark</Button>;
}
} else if (props.category) {
button = <Button>Update category</Button>
} else if (props.bookmark) {
button = <Button>Update bookmark</Button>
}
2021-05-24 11:51:05 +02:00
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)}
/>
<span>
<a
href='https://github.com/pawelmalak/flame#supported-url-formats-for-applications-and-bookmarks'
target='_blank'
rel='noreferrer'
>
{' '}Check supported URL formats
</a>
</span>
2021-05-24 11:51:05 +02:00
</InputGroup>
<InputGroup>
<label htmlFor='categoryId'>Bookmark Category</label>
<select
name='categoryId'
id='categoryId'
required
onChange={(e) => selectChangeHandler(e)}
2021-05-28 13:41:27 +02:00
value={formData.categoryId}
2021-05-24 11:51:05 +02:00
>
<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>
<InputGroup>
<label htmlFor='icon'>Bookmark Icon (optional)</label>
<input
type='text'
name='icon'
id='icon'
placeholder='book-open-outline'
value={formData.icon}
onChange={(e) => inputChangeHandler(e)}
/>
<span>
Use icon name from MDI.
<a
href='https://materialdesignicons.com/'
target='blank'>
{' '}Click here for reference
</a>
</span>
</InputGroup>
2021-05-24 11:51:05 +02:00
</Fragment>
)
}
2021-05-28 13:41:27 +02:00
{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,
2021-05-28 13:41:27 +02:00
updateBookmark,
2021-05-26 13:13:56 +02:00
createNotification
}
export default connect(mapStateToProps, dispatchMap)(BookmarkForm);