2022-08-27 15:58:36 +02:00
|
|
|
import { Modal, ModalOverlay, ModalContent, ModalFooter, ModalHeader, ModalBody, Button } from '@chakra-ui/react';
|
|
|
|
|
import React from 'react';
|
|
|
|
|
import { useNuiEvent } from '../../hooks/useNuiEvent';
|
|
|
|
|
import { useLocales } from '../../providers/LocaleProvider';
|
|
|
|
|
import { fetchNui } from '../../utils/fetchNui';
|
|
|
|
|
import { IInput, ICheckbox, ISelect, INumber, ISlider } from '../../interfaces/dialog';
|
2022-10-27 14:22:15 +02:00
|
|
|
import InputField from './components/fields/input';
|
|
|
|
|
import CheckboxField from './components/fields/checkbox';
|
|
|
|
|
import SelectField from './components/fields/select';
|
|
|
|
|
import NumberField from './components/fields/number';
|
|
|
|
|
import SliderField from './components/fields/slider';
|
2022-05-02 20:06:27 +02:00
|
|
|
|
2022-08-27 15:40:41 +02:00
|
|
|
export interface InputProps {
|
2022-03-19 12:19:01 +01:00
|
|
|
heading: string;
|
2022-08-10 21:05:27 +02:00
|
|
|
rows: Array<IInput | ICheckbox | ISelect | INumber | ISlider>;
|
2022-03-19 12:19:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const InputDialog: React.FC = () => {
|
2022-08-27 15:40:41 +02:00
|
|
|
const [fields, setFields] = React.useState<InputProps>({
|
2022-08-27 15:58:36 +02:00
|
|
|
heading: '',
|
|
|
|
|
rows: [{ type: 'input', label: '' }],
|
2022-03-19 12:19:01 +01:00
|
|
|
});
|
2022-08-27 15:58:36 +02:00
|
|
|
const [inputData, setInputData] = React.useState<Array<string | number | boolean>>([]);
|
2022-05-03 14:36:33 +02:00
|
|
|
const [passwordStates, setPasswordStates] = React.useState<boolean[]>([]);
|
2022-03-19 12:19:01 +01:00
|
|
|
const [visible, setVisible] = React.useState(false);
|
2022-05-02 14:45:06 +02:00
|
|
|
const { locale } = useLocales();
|
2022-03-19 12:19:01 +01:00
|
|
|
|
2022-05-03 01:03:02 +02:00
|
|
|
const handlePasswordStates = (index: number) => {
|
|
|
|
|
setPasswordStates({
|
2022-05-03 14:36:33 +02:00
|
|
|
...passwordStates,
|
|
|
|
|
[index]: !passwordStates[index],
|
|
|
|
|
});
|
|
|
|
|
};
|
2022-05-02 23:14:27 +02:00
|
|
|
|
2022-08-27 15:58:36 +02:00
|
|
|
useNuiEvent<InputProps>('openDialog', (data) => {
|
2022-05-03 01:03:02 +02:00
|
|
|
setPasswordStates([]);
|
2022-05-02 13:51:01 +02:00
|
|
|
setFields(data);
|
2022-04-19 10:44:11 +02:00
|
|
|
setInputData([]);
|
2022-03-19 12:19:01 +01:00
|
|
|
setVisible(true);
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-29 11:39:30 +02:00
|
|
|
useNuiEvent('closeInputDialog', () => {
|
|
|
|
|
setVisible(false);
|
|
|
|
|
});
|
|
|
|
|
|
2022-03-19 12:19:01 +01:00
|
|
|
const handleClose = () => {
|
|
|
|
|
setVisible(false);
|
2022-08-27 15:58:36 +02:00
|
|
|
fetchNui('inputData');
|
2022-03-19 12:19:01 +01:00
|
|
|
};
|
|
|
|
|
|
2022-06-19 14:57:03 +02:00
|
|
|
const handleChange = (value: string | number | boolean, index: number) => {
|
2022-03-19 12:19:01 +01:00
|
|
|
setInputData((previousData) => {
|
2022-05-02 20:06:27 +02:00
|
|
|
previousData[index] = value;
|
2022-03-19 12:19:01 +01:00
|
|
|
return previousData;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleConfirm = () => {
|
|
|
|
|
setVisible(false);
|
2022-08-27 15:58:36 +02:00
|
|
|
fetchNui('inputData', inputData);
|
2022-03-19 12:19:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2022-08-27 15:58:36 +02:00
|
|
|
<Modal isOpen={visible} onClose={handleClose} isCentered closeOnEsc closeOnOverlayClick={false} size="xs">
|
2022-03-19 12:19:01 +01:00
|
|
|
<ModalOverlay />
|
2022-08-26 18:49:49 +02:00
|
|
|
<ModalContent
|
|
|
|
|
onKeyDown={(e) => {
|
2022-08-27 15:58:36 +02:00
|
|
|
if (e.key === 'Enter' && visible) return handleConfirm();
|
2022-08-26 18:49:49 +02:00
|
|
|
}}
|
|
|
|
|
>
|
2022-05-02 13:51:01 +02:00
|
|
|
<ModalHeader textAlign="center">{fields.heading}</ModalHeader>
|
|
|
|
|
<ModalBody fontFamily="Poppins" textAlign="left">
|
2022-08-27 15:58:36 +02:00
|
|
|
{fields.rows.map((row: IInput | ICheckbox | ISelect | INumber | ISlider, index) => (
|
2022-12-16 09:40:28 +01:00
|
|
|
<React.Fragment key={`row-${index}-${row.type}-${row.label}`}>
|
2022-08-27 15:58:36 +02:00
|
|
|
{row.type === 'input' && (
|
|
|
|
|
<InputField
|
|
|
|
|
row={row}
|
|
|
|
|
index={index}
|
|
|
|
|
handleChange={handleChange}
|
|
|
|
|
passwordStates={passwordStates}
|
|
|
|
|
handlePasswordStates={handlePasswordStates}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{row.type === 'checkbox' && <CheckboxField row={row} index={index} handleChange={handleChange} />}
|
|
|
|
|
{row.type === 'select' && <SelectField row={row} index={index} handleChange={handleChange} />}
|
|
|
|
|
{row.type === 'number' && <NumberField row={row} index={index} handleChange={handleChange} />}
|
|
|
|
|
{row.type === 'slider' && <SliderField row={row} index={index} handleChange={handleChange} />}
|
|
|
|
|
</React.Fragment>
|
|
|
|
|
))}
|
2022-03-19 12:19:01 +01:00
|
|
|
</ModalBody>
|
|
|
|
|
<ModalFooter>
|
|
|
|
|
<Button mr={3} onClick={handleClose}>
|
2022-05-02 14:45:06 +02:00
|
|
|
{locale.ui.close}
|
2022-03-19 12:19:01 +01:00
|
|
|
</Button>
|
|
|
|
|
<Button colorScheme="blue" onClick={handleConfirm}>
|
2022-05-02 14:45:06 +02:00
|
|
|
{locale.ui.confirm}
|
2022-03-19 12:19:01 +01:00
|
|
|
</Button>
|
|
|
|
|
</ModalFooter>
|
|
|
|
|
</ModalContent>
|
|
|
|
|
</Modal>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default InputDialog;
|