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

364 lines
9.6 KiB
TypeScript
Raw Normal View History

2021-10-05 17:08:37 +02:00
// React
2021-08-06 15:15:54 +02:00
import {
useState,
SyntheticEvent,
Fragment,
ChangeEvent,
2021-10-05 17:08:37 +02:00
useEffect,
2021-08-06 15:15:54 +02:00
} from 'react';
2021-10-05 17:08:37 +02:00
// Redux
2021-05-24 11:51:05 +02:00
import { connect } from 'react-redux';
2021-10-05 17:08:37 +02:00
import {
getCategories,
addCategory,
addBookmark,
updateCategory,
updateBookmark,
createNotification,
} from '../../../store/actions';
2021-05-24 11:51:05 +02:00
2021-10-05 17:08:37 +02:00
// Typescript
2021-08-06 15:15:54 +02:00
import {
Bookmark,
Category,
GlobalState,
NewBookmark,
NewCategory,
2021-10-05 17:08:37 +02:00
NewNotification,
2021-08-06 15:15:54 +02:00
} from '../../../interfaces';
2021-05-25 11:44:11 +02:00
import { ContentType } from '../Bookmarks';
2021-10-05 17:08:37 +02:00
// UI
import ModalForm from '../../UI/Forms/ModalForm/ModalForm';
import InputGroup from '../../UI/Forms/InputGroup/InputGroup';
2021-05-26 13:13:56 +02:00
import Button from '../../UI/Buttons/Button/Button';
2021-10-05 17:08:37 +02:00
// CSS
2021-07-28 12:36:03 +02:00
import classes from './BookmarkForm.module.css';
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;
2021-07-28 12:36:03 +02:00
addBookmark: (formData: NewBookmark | FormData) => void;
2021-05-26 13:13:56 +02:00
updateCategory: (id: number, formData: NewCategory) => void;
2021-07-28 12:36:03 +02:00
updateBookmark: (
id: number,
formData: NewBookmark | FormData,
category: {
2021-08-06 15:15:54 +02:00
prev: number;
curr: number;
2021-07-28 12:36:03 +02:00
}
) => 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 => {
2021-07-28 12:36:03 +02:00
const [useCustomIcon, toggleUseCustomIcon] = useState<boolean>(false);
const [customIcon, setCustomIcon] = useState<File | null>(null);
2021-05-24 11:51:05 +02:00
const [categoryName, setCategoryName] = useState<NewCategory>({
2021-10-05 17:08:37 +02:00
name: '',
2021-08-06 15:15:54 +02:00
});
2021-05-24 11:51:05 +02:00
const [formData, setFormData] = useState<NewBookmark>({
name: '',
url: '',
categoryId: -1,
2021-10-05 17:08:37 +02:00
icon: '',
2021-08-06 15:15:54 +02:00
});
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: '' });
}
2021-08-06 15:15:54 +02:00
}, [props.category]);
2021-05-26 13:13:56 +02:00
// 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,
2021-10-05 17:08:37 +02:00
icon: props.bookmark.icon,
2021-08-06 15:15:54 +02:00
});
2021-05-28 13:41:27 +02:00
} else {
setFormData({
name: '',
url: '',
categoryId: -1,
2021-10-05 17:08:37 +02:00
icon: '',
2021-08-06 15:15:54 +02:00
});
2021-05-28 13:41:27 +02:00
}
2021-08-06 15:15:54 +02:00
}, [props.bookmark]);
2021-05-28 13:41:27 +02:00
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-07-28 12:36:03 +02:00
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;
2021-08-06 15:15:54 +02:00
};
2021-07-28 12:36:03 +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',
2021-10-05 17:08:37 +02:00
message: 'Please select category',
2021-08-06 15:15:54 +02:00
});
2021-05-26 13:13:56 +02:00
return;
}
2021-07-28 12:36:03 +02:00
if (customIcon) {
const data = createFormData();
props.addBookmark(data);
} else {
props.addBookmark(formData);
}
2021-08-06 15:15:54 +02:00
2021-05-26 13:13:56 +02:00
setFormData({
name: '',
url: '',
categoryId: formData.categoryId,
2021-10-05 17:08:37 +02:00
icon: '',
2021-08-06 15:15:54 +02:00
});
2021-07-28 12:36:03 +02:00
2021-10-05 17:08:37 +02:00
// setCustomIcon(null);
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-07-28 12:36:03 +02:00
if (customIcon) {
const data = createFormData();
2021-08-06 15:15:54 +02:00
props.updateBookmark(props.bookmark.id, data, {
prev: props.bookmark.categoryId,
2021-10-05 17:08:37 +02:00
curr: formData.categoryId,
2021-08-06 15:15:54 +02:00
});
2021-07-28 12:36:03 +02:00
} else {
2021-08-06 15:15:54 +02:00
props.updateBookmark(props.bookmark.id, formData, {
prev: props.bookmark.categoryId,
2021-10-05 17:08:37 +02:00
curr: formData.categoryId,
2021-08-06 15:15:54 +02:00
});
2021-07-28 12:36:03 +02:00
}
2021-05-28 13:41:27 +02:00
setFormData({
name: '',
url: '',
categoryId: -1,
2021-10-05 17:08:37 +02:00
icon: '',
2021-08-06 15:15:54 +02:00
});
2021-07-28 12:36:03 +02:00
2021-08-06 15:15:54 +02:00
setCustomIcon(null);
}
2021-05-26 13:13:56 +02:00
props.modalHandler();
2021-05-24 11:51:05 +02:00
}
2021-08-06 15:15:54 +02:00
};
2021-05-24 11:51:05 +02:00
const inputChangeHandler = (e: ChangeEvent<HTMLInputElement>): void => {
setFormData({
...formData,
2021-10-05 17:08:37 +02:00
[e.target.name]: e.target.value,
2021-08-06 15:15:54 +02:00
});
};
2021-05-24 11:51:05 +02:00
const selectChangeHandler = (e: ChangeEvent<HTMLSelectElement>): void => {
setFormData({
...formData,
2021-10-05 17:08:37 +02:00
categoryId: parseInt(e.target.value),
2021-08-06 15:15:54 +02:00
});
};
2021-05-24 11:51:05 +02:00
2021-07-28 12:36:03 +02:00
const fileChangeHandler = (e: ChangeEvent<HTMLInputElement>): void => {
if (e.target.files) {
setCustomIcon(e.target.files[0]);
}
2021-08-06 15:15:54 +02:00
};
2021-07-28 12:36:03 +02:00
2021-08-06 15:15:54 +02:00
let button = <Button>Submit</Button>;
2021-05-28 13:41:27 +02:00
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) {
2021-08-06 15:15:54 +02:00
button = <Button>Update category</Button>;
2021-05-28 13:41:27 +02:00
} else if (props.bookmark) {
2021-08-06 15:15:54 +02:00
button = <Button>Update bookmark</Button>;
2021-05-28 13:41:27 +02:00
}
2021-05-24 11:51:05 +02:00
return (
<ModalForm
modalHandler={props.modalHandler}
formHandler={formSubmitHandler}
>
2021-08-06 15:15:54 +02:00
{props.contentType === ContentType.category ? (
<Fragment>
<InputGroup>
2021-10-05 17:08:37 +02:00
<label htmlFor="categoryName">Category Name</label>
2021-08-06 15:15:54 +02:00
<input
2021-10-05 17:08:37 +02:00
type="text"
name="categoryName"
id="categoryName"
placeholder="Social Media"
2021-08-06 15:15:54 +02:00
required
value={categoryName.name}
2021-10-05 17:08:37 +02:00
onChange={(e) => setCategoryName({ name: e.target.value })}
2021-08-06 15:15:54 +02:00
/>
</InputGroup>
</Fragment>
) : (
<Fragment>
<InputGroup>
2021-10-05 17:08:37 +02:00
<label htmlFor="name">Bookmark Name</label>
2021-08-06 15:15:54 +02:00
<input
2021-10-05 17:08:37 +02:00
type="text"
name="name"
id="name"
placeholder="Reddit"
2021-08-06 15:15:54 +02:00
required
value={formData.name}
2021-10-05 17:08:37 +02:00
onChange={(e) => inputChangeHandler(e)}
2021-08-06 15:15:54 +02:00
/>
</InputGroup>
<InputGroup>
2021-10-05 17:08:37 +02:00
<label htmlFor="url">Bookmark URL</label>
2021-08-06 15:15:54 +02:00
<input
2021-10-05 17:08:37 +02:00
type="text"
name="url"
id="url"
placeholder="reddit.com"
2021-08-06 15:15:54 +02:00
required
value={formData.url}
2021-10-05 17:08:37 +02:00
onChange={(e) => inputChangeHandler(e)}
2021-08-06 15:15:54 +02:00
/>
<span>
<a
2021-10-05 17:08:37 +02:00
href="https://github.com/pawelmalak/flame#supported-url-formats-for-applications-and-bookmarks"
target="_blank"
rel="noreferrer"
2021-08-06 15:15:54 +02:00
>
{' '}
Check supported URL formats
</a>
</span>
</InputGroup>
<InputGroup>
2021-10-05 17:08:37 +02:00
<label htmlFor="categoryId">Bookmark Category</label>
2021-08-06 15:15:54 +02:00
<select
2021-10-05 17:08:37 +02:00
name="categoryId"
id="categoryId"
2021-08-06 15:15:54 +02:00
required
2021-10-05 17:08:37 +02:00
onChange={(e) => selectChangeHandler(e)}
2021-08-06 15:15:54 +02:00
value={formData.categoryId}
>
<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>
{!useCustomIcon ? (
// mdi
2021-05-24 11:51:05 +02:00
<InputGroup>
2021-10-05 17:08:37 +02:00
<label htmlFor="icon">Bookmark Icon (optional)</label>
2021-05-24 11:51:05 +02:00
<input
2021-10-05 17:08:37 +02:00
type="text"
name="icon"
id="icon"
placeholder="book-open-outline"
2021-08-06 15:15:54 +02:00
value={formData.icon}
2021-10-05 17:08:37 +02:00
onChange={(e) => inputChangeHandler(e)}
2021-05-24 11:51:05 +02:00
/>
<span>
2021-08-06 15:15:54 +02:00
Use icon name from MDI.
2021-10-05 17:08:37 +02:00
<a href="https://materialdesignicons.com/" target="blank">
2021-08-06 15:15:54 +02:00
{' '}
Click here for reference
</a>
</span>
2021-08-06 15:15:54 +02:00
<span
onClick={() => toggleUseCustomIcon(!useCustomIcon)}
className={classes.Switch}
>
Switch to custom icon upload
</span>
2021-05-24 11:51:05 +02:00
</InputGroup>
2021-08-06 15:15:54 +02:00
) : (
// custom
2021-05-24 11:51:05 +02:00
<InputGroup>
2021-10-05 17:08:37 +02:00
<label htmlFor="icon">Bookmark Icon (optional)</label>
2021-08-06 15:15:54 +02:00
<input
2021-10-05 17:08:37 +02:00
type="file"
name="icon"
id="icon"
onChange={(e) => fileChangeHandler(e)}
accept=".jpg,.jpeg,.png,.svg"
2021-08-06 15:15:54 +02:00
/>
<span
2021-10-05 17:08:37 +02:00
onClick={() => {
setCustomIcon(null);
toggleUseCustomIcon(!useCustomIcon);
}}
2021-08-06 15:15:54 +02:00
className={classes.Switch}
2021-05-24 11:51:05 +02:00
>
2021-08-06 15:15:54 +02:00
Switch to MDI
</span>
2021-05-24 11:51:05 +02:00
</InputGroup>
2021-08-06 15:15:54 +02:00
)}
</Fragment>
)}
2021-05-28 13:41:27 +02:00
{button}
2021-05-24 11:51:05 +02:00
</ModalForm>
2021-08-06 15:15:54 +02:00
);
};
2021-05-24 11:51:05 +02:00
const mapStateToProps = (state: GlobalState) => {
return {
2021-10-05 17:08:37 +02:00
categories: state.bookmark.categories,
2021-08-06 15:15:54 +02:00
};
};
2021-05-24 11:51:05 +02:00
2021-05-26 13:13:56 +02:00
const dispatchMap = {
getCategories,
addCategory,
addBookmark,
updateCategory,
2021-05-28 13:41:27 +02:00
updateBookmark,
2021-10-05 17:08:37 +02:00
createNotification,
2021-08-06 15:15:54 +02:00
};
2021-05-26 13:13:56 +02:00
2021-08-06 15:15:54 +02:00
export default connect(mapStateToProps, dispatchMap)(BookmarkForm);