2021-07-28 12:36:03 +02:00
|
|
|
import { useState, useEffect, ChangeEvent, SyntheticEvent } from 'react';
|
2021-05-13 17:21:52 +02:00
|
|
|
import { connect } from 'react-redux';
|
2021-05-22 17:03:32 +02:00
|
|
|
import { addApp, updateApp } from '../../../store/actions';
|
2021-05-24 14:54:46 +02:00
|
|
|
import { App, NewApp } from '../../../interfaces';
|
2021-05-13 17:21:52 +02:00
|
|
|
|
2021-06-23 15:27:46 +02:00
|
|
|
import classes from './AppForm.module.css';
|
|
|
|
|
2021-05-24 11:00:42 +02:00
|
|
|
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-05-13 17:21:52 +02:00
|
|
|
|
|
|
|
interface ComponentProps {
|
2021-05-24 11:00:42 +02:00
|
|
|
modalHandler: () => void;
|
2021-06-23 15:27:46 +02:00
|
|
|
addApp: (formData: NewApp | FormData) => any;
|
2021-07-28 11:36:48 +02:00
|
|
|
updateApp: (id: number, formData: NewApp | FormData) => any;
|
2021-05-22 17:03:32 +02:00
|
|
|
app?: App;
|
2021-05-13 17:21:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const AppForm = (props: ComponentProps): JSX.Element => {
|
2021-06-24 10:54:48 +02:00
|
|
|
const [useCustomIcon, toggleUseCustomIcon] = useState<boolean>(false);
|
2021-06-23 15:27:46 +02:00
|
|
|
const [customIcon, setCustomIcon] = useState<File | null>(null);
|
2021-05-13 17:21:52 +02:00
|
|
|
const [formData, setFormData] = useState<NewApp>({
|
|
|
|
name: '',
|
|
|
|
url: '',
|
|
|
|
icon: ''
|
|
|
|
});
|
|
|
|
|
2021-05-22 17:03:32 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (props.app) {
|
|
|
|
setFormData({
|
|
|
|
name: props.app.name,
|
|
|
|
url: props.app.url,
|
|
|
|
icon: props.app.icon
|
2021-08-06 15:15:54 +02:00
|
|
|
});
|
2021-05-26 13:13:56 +02:00
|
|
|
} else {
|
|
|
|
setFormData({
|
|
|
|
name: '',
|
|
|
|
url: '',
|
|
|
|
icon: ''
|
2021-08-06 15:15:54 +02:00
|
|
|
});
|
2021-05-22 17:03:32 +02:00
|
|
|
}
|
2021-08-06 15:15:54 +02:00
|
|
|
}, [props.app]);
|
2021-05-22 17:03:32 +02:00
|
|
|
|
2021-05-13 17:21:52 +02:00
|
|
|
const inputChangeHandler = (e: ChangeEvent<HTMLInputElement>): void => {
|
|
|
|
setFormData({
|
|
|
|
...formData,
|
|
|
|
[e.target.name]: e.target.value
|
2021-08-06 15:15:54 +02:00
|
|
|
});
|
|
|
|
};
|
2021-05-13 17:21:52 +02:00
|
|
|
|
2021-06-23 15:27:46 +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-06-23 15:27:46 +02:00
|
|
|
|
2021-05-24 11:00:42 +02:00
|
|
|
const formSubmitHandler = (e: SyntheticEvent<HTMLFormElement>): void => {
|
2021-05-13 17:21:52 +02:00
|
|
|
e.preventDefault();
|
2021-05-22 17:03:32 +02:00
|
|
|
|
2021-07-28 11:36:48 +02:00
|
|
|
const createFormData = (): FormData => {
|
|
|
|
const data = new FormData();
|
2021-06-23 15:27:46 +02:00
|
|
|
if (customIcon) {
|
|
|
|
data.append('icon', customIcon);
|
2021-07-28 11:36:48 +02:00
|
|
|
}
|
|
|
|
data.append('name', formData.name);
|
|
|
|
data.append('url', formData.url);
|
2021-06-23 15:27:46 +02:00
|
|
|
|
2021-07-28 11:36:48 +02:00
|
|
|
return data;
|
2021-08-06 15:15:54 +02:00
|
|
|
};
|
2021-06-23 15:27:46 +02:00
|
|
|
|
2021-07-28 11:36:48 +02:00
|
|
|
if (!props.app) {
|
|
|
|
if (customIcon) {
|
|
|
|
const data = createFormData();
|
2021-06-23 15:27:46 +02:00
|
|
|
props.addApp(data);
|
|
|
|
} else {
|
|
|
|
props.addApp(formData);
|
|
|
|
}
|
2021-05-22 17:03:32 +02:00
|
|
|
} else {
|
2021-07-28 11:36:48 +02:00
|
|
|
if (customIcon) {
|
|
|
|
const data = createFormData();
|
|
|
|
props.updateApp(props.app.id, data);
|
|
|
|
} else {
|
|
|
|
props.updateApp(props.app.id, formData);
|
|
|
|
props.modalHandler();
|
|
|
|
}
|
2021-05-22 17:03:32 +02:00
|
|
|
}
|
|
|
|
|
2021-05-13 17:21:52 +02:00
|
|
|
setFormData({
|
|
|
|
name: '',
|
|
|
|
url: '',
|
|
|
|
icon: ''
|
2021-08-06 15:15:54 +02:00
|
|
|
});
|
2021-07-28 12:36:03 +02:00
|
|
|
|
|
|
|
setCustomIcon(null);
|
2021-08-06 15:15:54 +02:00
|
|
|
};
|
2021-05-13 17:21:52 +02:00
|
|
|
|
|
|
|
return (
|
2021-05-24 11:00:42 +02:00
|
|
|
<ModalForm
|
|
|
|
modalHandler={props.modalHandler}
|
|
|
|
formHandler={formSubmitHandler}
|
|
|
|
>
|
|
|
|
<InputGroup>
|
|
|
|
<label htmlFor='name'>App Name</label>
|
|
|
|
<input
|
|
|
|
type='text'
|
|
|
|
name='name'
|
|
|
|
id='name'
|
|
|
|
placeholder='Bookstack'
|
|
|
|
required
|
|
|
|
value={formData.name}
|
2021-08-06 15:15:54 +02:00
|
|
|
onChange={e => inputChangeHandler(e)}
|
2021-05-24 11:00:42 +02:00
|
|
|
/>
|
|
|
|
</InputGroup>
|
|
|
|
<InputGroup>
|
|
|
|
<label htmlFor='url'>App URL</label>
|
|
|
|
<input
|
|
|
|
type='text'
|
|
|
|
name='url'
|
|
|
|
id='url'
|
|
|
|
placeholder='bookstack.example.com'
|
|
|
|
required
|
|
|
|
value={formData.url}
|
2021-08-06 15:15:54 +02:00
|
|
|
onChange={e => inputChangeHandler(e)}
|
2021-05-24 11:00:42 +02:00
|
|
|
/>
|
2021-06-13 01:06:42 +02:00
|
|
|
<span>
|
|
|
|
<a
|
2021-06-13 23:21:35 +02:00
|
|
|
href='https://github.com/pawelmalak/flame#supported-url-formats-for-applications-and-bookmarks'
|
2021-06-13 01:06:42 +02:00
|
|
|
target='_blank'
|
|
|
|
rel='noreferrer'
|
2021-06-13 23:21:35 +02:00
|
|
|
>
|
2021-08-06 15:15:54 +02:00
|
|
|
{' '}
|
|
|
|
Check supported URL formats
|
2021-06-13 01:06:42 +02:00
|
|
|
</a>
|
|
|
|
</span>
|
2021-05-24 11:00:42 +02:00
|
|
|
</InputGroup>
|
2021-08-06 15:15:54 +02:00
|
|
|
{!useCustomIcon ? (
|
2021-06-23 15:27:46 +02:00
|
|
|
// use mdi icon
|
2021-08-06 15:15:54 +02:00
|
|
|
<InputGroup>
|
|
|
|
<label htmlFor='icon'>App Icon</label>
|
|
|
|
<input
|
|
|
|
type='text'
|
|
|
|
name='icon'
|
|
|
|
id='icon'
|
|
|
|
placeholder='book-open-outline'
|
|
|
|
required
|
|
|
|
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>
|
|
|
|
<span
|
|
|
|
onClick={() => toggleUseCustomIcon(!useCustomIcon)}
|
|
|
|
className={classes.Switch}
|
|
|
|
>
|
|
|
|
Switch to custom icon upload
|
|
|
|
</span>
|
|
|
|
</InputGroup>
|
|
|
|
) : (
|
2021-06-23 15:27:46 +02:00
|
|
|
// upload custom icon
|
2021-08-06 15:15:54 +02:00
|
|
|
<InputGroup>
|
|
|
|
<label htmlFor='icon'>App Icon</label>
|
|
|
|
<input
|
|
|
|
type='file'
|
|
|
|
name='icon'
|
|
|
|
id='icon'
|
|
|
|
required
|
|
|
|
onChange={e => fileChangeHandler(e)}
|
|
|
|
accept='.jpg,.jpeg,.png,.svg'
|
|
|
|
/>
|
|
|
|
<span
|
|
|
|
onClick={() => toggleUseCustomIcon(!useCustomIcon)}
|
|
|
|
className={classes.Switch}
|
|
|
|
>
|
|
|
|
Switch to MDI
|
|
|
|
</span>
|
|
|
|
</InputGroup>
|
|
|
|
)}
|
|
|
|
{!props.app ? (
|
|
|
|
<Button>Add new application</Button>
|
|
|
|
) : (
|
|
|
|
<Button>Update application</Button>
|
|
|
|
)}
|
2021-05-24 11:00:42 +02:00
|
|
|
</ModalForm>
|
2021-08-06 15:15:54 +02:00
|
|
|
);
|
|
|
|
};
|
2021-05-13 17:21:52 +02:00
|
|
|
|
2021-08-06 15:15:54 +02:00
|
|
|
export default connect(null, { addApp, updateApp })(AppForm);
|