2021-07-28 12:36:03 +02:00
|
|
|
import { useState, useEffect, ChangeEvent, SyntheticEvent } from 'react';
|
2021-11-09 14:33:51 +01:00
|
|
|
import { useDispatch } from 'react-redux';
|
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-11-09 14:33:51 +01:00
|
|
|
import { ModalForm, InputGroup, Button } from '../../UI';
|
2021-11-08 23:40:30 +01:00
|
|
|
import { inputHandler, newAppTemplate } from '../../../utility';
|
2021-11-09 14:33:51 +01:00
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { actionCreators } from '../../../store';
|
2021-05-13 17:21:52 +02:00
|
|
|
|
2021-11-09 14:33:51 +01:00
|
|
|
interface Props {
|
2021-05-24 11:00:42 +02:00
|
|
|
modalHandler: () => void;
|
2021-05-22 17:03:32 +02:00
|
|
|
app?: App;
|
2021-05-13 17:21:52 +02:00
|
|
|
}
|
|
|
|
|
2021-11-09 23:40:58 +01:00
|
|
|
export const AppForm = ({ app, modalHandler }: Props): JSX.Element => {
|
2021-11-09 14:33:51 +01:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
const { addApp, updateApp } = bindActionCreators(actionCreators, dispatch);
|
|
|
|
|
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-11-08 23:40:30 +01:00
|
|
|
const [formData, setFormData] = useState<NewApp>(newAppTemplate);
|
2021-05-13 17:21:52 +02:00
|
|
|
|
2021-05-22 17:03:32 +02:00
|
|
|
useEffect(() => {
|
2021-11-09 23:40:58 +01:00
|
|
|
if (app) {
|
2021-05-22 17:03:32 +02:00
|
|
|
setFormData({
|
2021-11-09 23:40:58 +01:00
|
|
|
...app,
|
2021-08-06 15:15:54 +02:00
|
|
|
});
|
2021-05-26 13:13:56 +02:00
|
|
|
} else {
|
2021-11-08 23:40:30 +01:00
|
|
|
setFormData(newAppTemplate);
|
2021-05-22 17:03:32 +02:00
|
|
|
}
|
2021-11-09 23:40:58 +01:00
|
|
|
}, [app]);
|
2021-05-22 17:03:32 +02:00
|
|
|
|
2021-11-08 23:40:30 +01:00
|
|
|
const inputChangeHandler = (
|
|
|
|
e: ChangeEvent<HTMLInputElement | HTMLSelectElement>,
|
|
|
|
options?: { isNumber?: boolean; isBool?: boolean }
|
|
|
|
) => {
|
|
|
|
inputHandler<NewApp>({
|
|
|
|
e,
|
|
|
|
options,
|
|
|
|
setStateHandler: setFormData,
|
|
|
|
state: formData,
|
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-10-05 17:08:37 +02:00
|
|
|
|
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-11-19 14:06:38 +01:00
|
|
|
data.append('isPublic', `${formData.isPublic ? 1 : 0}`);
|
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-11-09 23:40:58 +01:00
|
|
|
if (!app) {
|
2021-07-28 11:36:48 +02:00
|
|
|
if (customIcon) {
|
|
|
|
const data = createFormData();
|
2021-11-09 14:33:51 +01:00
|
|
|
addApp(data);
|
2021-06-23 15:27:46 +02:00
|
|
|
} else {
|
2021-11-09 14:33:51 +01:00
|
|
|
addApp(formData);
|
2021-06-23 15:27:46 +02:00
|
|
|
}
|
2021-05-22 17:03:32 +02:00
|
|
|
} else {
|
2021-07-28 11:36:48 +02:00
|
|
|
if (customIcon) {
|
|
|
|
const data = createFormData();
|
2021-11-09 23:40:58 +01:00
|
|
|
updateApp(app.id, data);
|
2021-07-28 11:36:48 +02:00
|
|
|
} else {
|
2021-11-09 23:40:58 +01:00
|
|
|
updateApp(app.id, formData);
|
|
|
|
modalHandler();
|
2021-07-28 11:36:48 +02:00
|
|
|
}
|
2021-05-22 17:03:32 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 23:40:30 +01:00
|
|
|
setFormData(newAppTemplate);
|
2021-08-06 15:15:54 +02:00
|
|
|
};
|
2021-05-13 17:21:52 +02:00
|
|
|
|
|
|
|
return (
|
2021-11-09 23:40:58 +01:00
|
|
|
<ModalForm modalHandler={modalHandler} formHandler={formSubmitHandler}>
|
2021-11-08 23:40:30 +01:00
|
|
|
{/* NAME */}
|
2021-05-24 11:00:42 +02:00
|
|
|
<InputGroup>
|
2021-10-05 17:08:37 +02:00
|
|
|
<label htmlFor="name">App Name</label>
|
2021-05-24 11:00:42 +02:00
|
|
|
<input
|
2021-10-05 17:08:37 +02:00
|
|
|
type="text"
|
|
|
|
name="name"
|
|
|
|
id="name"
|
|
|
|
placeholder="Bookstack"
|
2021-05-24 11:00:42 +02:00
|
|
|
required
|
|
|
|
value={formData.name}
|
2021-10-05 17:08:37 +02:00
|
|
|
onChange={(e) => inputChangeHandler(e)}
|
2021-05-24 11:00:42 +02:00
|
|
|
/>
|
|
|
|
</InputGroup>
|
2021-11-08 23:40:30 +01:00
|
|
|
|
|
|
|
{/* URL */}
|
2021-05-24 11:00:42 +02:00
|
|
|
<InputGroup>
|
2021-10-05 17:08:37 +02:00
|
|
|
<label htmlFor="url">App URL</label>
|
2021-05-24 11:00:42 +02:00
|
|
|
<input
|
2021-10-05 17:08:37 +02:00
|
|
|
type="text"
|
|
|
|
name="url"
|
|
|
|
id="url"
|
|
|
|
placeholder="bookstack.example.com"
|
2021-05-24 11:00:42 +02:00
|
|
|
required
|
|
|
|
value={formData.url}
|
2021-10-05 17:08:37 +02:00
|
|
|
onChange={(e) => inputChangeHandler(e)}
|
2021-05-24 11:00:42 +02:00
|
|
|
/>
|
|
|
|
</InputGroup>
|
2021-11-08 23:40:30 +01:00
|
|
|
|
|
|
|
{/* ICON */}
|
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>
|
2021-10-05 17:08:37 +02:00
|
|
|
<label htmlFor="icon">App Icon</label>
|
2021-08-06 15:15:54 +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
|
|
|
required
|
|
|
|
value={formData.icon}
|
2021-10-05 17:08:37 +02:00
|
|
|
onChange={(e) => inputChangeHandler(e)}
|
2021-08-06 15:15:54 +02:00
|
|
|
/>
|
|
|
|
<span>
|
2021-11-10 13:53:28 +01:00
|
|
|
Use icon name from MDI or pass a valid URL.
|
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>
|
|
|
|
<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>
|
2021-10-05 17:08:37 +02:00
|
|
|
<label htmlFor="icon">App Icon</label>
|
2021-08-06 15:15:54 +02:00
|
|
|
<input
|
2021-10-05 17:08:37 +02:00
|
|
|
type="file"
|
|
|
|
name="icon"
|
|
|
|
id="icon"
|
2021-08-06 15:15:54 +02:00
|
|
|
required
|
2021-10-05 17:08:37 +02:00
|
|
|
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}
|
|
|
|
>
|
|
|
|
Switch to MDI
|
|
|
|
</span>
|
|
|
|
</InputGroup>
|
|
|
|
)}
|
2021-11-08 23:40:30 +01:00
|
|
|
|
|
|
|
{/* VISIBILITY */}
|
|
|
|
<InputGroup>
|
|
|
|
<label htmlFor="isPublic">App visibility</label>
|
|
|
|
<select
|
|
|
|
id="isPublic"
|
|
|
|
name="isPublic"
|
|
|
|
value={formData.isPublic ? 1 : 0}
|
|
|
|
onChange={(e) => inputChangeHandler(e, { isBool: true })}
|
|
|
|
>
|
|
|
|
<option value={1}>Visible (anyone can access it)</option>
|
|
|
|
<option value={0}>Hidden (authentication required)</option>
|
|
|
|
</select>
|
|
|
|
</InputGroup>
|
|
|
|
|
2021-11-09 23:40:58 +01:00
|
|
|
{!app ? (
|
2021-08-06 15:15:54 +02:00
|
|
|
<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
|
|
|
);
|
|
|
|
};
|