2022-03-19 12:19:01 +01:00
|
|
|
import {
|
|
|
|
|
Modal,
|
|
|
|
|
ModalOverlay,
|
|
|
|
|
ModalContent,
|
|
|
|
|
ModalFooter,
|
|
|
|
|
ModalHeader,
|
|
|
|
|
ModalBody,
|
|
|
|
|
Box,
|
|
|
|
|
Input,
|
2022-05-02 23:14:27 +02:00
|
|
|
InputGroup,
|
|
|
|
|
InputRightElement,
|
2022-03-19 12:19:01 +01:00
|
|
|
Text,
|
|
|
|
|
Button,
|
2022-05-02 13:51:01 +02:00
|
|
|
Checkbox,
|
2022-05-02 20:06:27 +02:00
|
|
|
Select,
|
2022-03-19 12:19:01 +01:00
|
|
|
} from "@chakra-ui/react";
|
|
|
|
|
import React from "react";
|
2022-05-02 23:14:27 +02:00
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
2022-04-01 21:41:53 +02:00
|
|
|
import { useNuiEvent } from "../../hooks/useNuiEvent";
|
2022-05-02 14:45:06 +02:00
|
|
|
import { useLocales } from "../../providers/LocaleProvider";
|
2022-05-02 13:51:01 +02:00
|
|
|
import { debugData } from "../../utils/debugData";
|
2022-04-01 21:41:53 +02:00
|
|
|
import { fetchNui } from "../../utils/fetchNui";
|
2022-03-19 12:19:01 +01:00
|
|
|
|
2022-05-02 20:06:27 +02:00
|
|
|
type RowType = "input" | "checkbox" | "select";
|
|
|
|
|
|
2022-03-19 12:19:01 +01:00
|
|
|
interface Props {
|
|
|
|
|
heading: string;
|
2022-05-02 13:51:01 +02:00
|
|
|
rows: {
|
2022-05-02 20:06:27 +02:00
|
|
|
type: RowType;
|
2022-05-02 13:51:01 +02:00
|
|
|
label: string;
|
2022-05-02 20:06:27 +02:00
|
|
|
options?: { value: string; label: string }[];
|
2022-05-02 23:14:27 +02:00
|
|
|
password?: boolean;
|
2022-05-02 13:51:01 +02:00
|
|
|
}[];
|
2022-03-19 12:19:01 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-02 13:51:01 +02:00
|
|
|
debugData<Props>([
|
|
|
|
|
{
|
|
|
|
|
action: "openDialog",
|
|
|
|
|
data: {
|
|
|
|
|
heading: "Police locker",
|
|
|
|
|
rows: [
|
|
|
|
|
{ type: "input", label: "Locker number" },
|
|
|
|
|
{ type: "checkbox", label: "Some checkbox" },
|
2022-05-02 23:14:27 +02:00
|
|
|
{ type: "input", label: "Locker PIN", password: true },
|
2022-05-02 13:51:01 +02:00
|
|
|
{ type: "checkbox", label: "Some other checkbox" },
|
2022-05-02 20:06:27 +02:00
|
|
|
{
|
|
|
|
|
type: "select",
|
|
|
|
|
label: "Locker type",
|
|
|
|
|
options: [
|
|
|
|
|
{ value: "option1", label: "Option 1" },
|
|
|
|
|
{ value: "option2", label: "Option 2" },
|
|
|
|
|
{ value: "option3", label: "Option 3" },
|
|
|
|
|
],
|
|
|
|
|
},
|
2022-05-02 13:51:01 +02:00
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
]);
|
2022-03-19 12:19:01 +01:00
|
|
|
|
|
|
|
|
const InputDialog: React.FC = () => {
|
2022-05-02 13:51:01 +02:00
|
|
|
const [fields, setFields] = React.useState<Props>({
|
2022-03-19 12:19:01 +01:00
|
|
|
heading: "",
|
2022-05-02 13:51:01 +02:00
|
|
|
rows: [{ type: "input", label: "" }],
|
2022-03-19 12:19:01 +01:00
|
|
|
});
|
2022-05-02 13:51:01 +02:00
|
|
|
const [inputData, setInputData] = React.useState<Array<string | 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 00:19:02 +02:00
|
|
|
const [passwordStates, setShowPassword] = React.useState<Array<boolean>>([]);
|
|
|
|
|
const handleShowPassword = (index: number) => {
|
|
|
|
|
setShowPassword({
|
|
|
|
|
...passwordStates, [index]: !passwordStates[index]
|
|
|
|
|
})
|
|
|
|
|
}
|
2022-05-02 23:14:27 +02:00
|
|
|
|
2022-03-19 12:19:01 +01:00
|
|
|
useNuiEvent<Props>("openDialog", (data) => {
|
2022-05-03 00:19:02 +02:00
|
|
|
setShowPassword([]);
|
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);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const handleClose = () => {
|
|
|
|
|
setVisible(false);
|
|
|
|
|
fetchNui("inputData");
|
|
|
|
|
};
|
|
|
|
|
|
2022-05-02 20:06:27 +02:00
|
|
|
const handleChange = (value: string | 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);
|
|
|
|
|
fetchNui("inputData", inputData);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<Modal
|
|
|
|
|
isOpen={visible}
|
|
|
|
|
onClose={handleClose}
|
|
|
|
|
isCentered
|
|
|
|
|
closeOnEsc
|
|
|
|
|
closeOnOverlayClick={false}
|
|
|
|
|
size="xs"
|
|
|
|
|
>
|
|
|
|
|
<ModalOverlay />
|
|
|
|
|
<ModalContent>
|
2022-05-02 13:51:01 +02:00
|
|
|
<ModalHeader textAlign="center">{fields.heading}</ModalHeader>
|
|
|
|
|
<ModalBody fontFamily="Poppins" textAlign="left">
|
|
|
|
|
{fields.rows.map((row, index) => (
|
|
|
|
|
<React.Fragment key={`row-${index}`}>
|
|
|
|
|
{row.type === "input" && (
|
|
|
|
|
<Box mb={3} key={`input-${index}`} textAlign="left">
|
|
|
|
|
<Text>{row.label}</Text>
|
2022-05-02 23:14:27 +02:00
|
|
|
<InputGroup>
|
|
|
|
|
<Input
|
|
|
|
|
onChange={(e) => handleChange(e.target.value, index)}
|
2022-05-03 00:19:02 +02:00
|
|
|
type={!row.password || passwordStates[index] ? 'text' : 'password'}
|
2022-05-02 23:14:27 +02:00
|
|
|
/>
|
|
|
|
|
{row.password && (
|
|
|
|
|
<InputRightElement
|
|
|
|
|
cursor="pointer"
|
2022-05-03 00:19:02 +02:00
|
|
|
onClick={() => handleShowPassword(index)}
|
2022-05-02 23:14:27 +02:00
|
|
|
children={
|
|
|
|
|
<FontAwesomeIcon
|
|
|
|
|
fixedWidth
|
2022-05-03 00:19:02 +02:00
|
|
|
icon={passwordStates[index] ? "eye" : "eye-slash"}
|
2022-05-02 23:14:27 +02:00
|
|
|
fontSize="1em"
|
|
|
|
|
style={{ paddingRight: 8 }}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</InputGroup>
|
2022-05-02 13:51:01 +02:00
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
{row.type === "checkbox" && (
|
|
|
|
|
<Box mb={3} key={`checkbox-${index}`}>
|
|
|
|
|
<Checkbox
|
2022-05-02 20:06:27 +02:00
|
|
|
onChange={(e) => handleChange(e.target.checked, index)}
|
2022-05-02 13:51:01 +02:00
|
|
|
>
|
|
|
|
|
{row.label}
|
|
|
|
|
</Checkbox>
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
2022-05-02 20:06:27 +02:00
|
|
|
{row.type === "select" && (
|
|
|
|
|
<Box mb={3} key={`select-${index}`}>
|
|
|
|
|
<Select
|
|
|
|
|
onChange={(e) => handleChange(e.target.value, index)}
|
|
|
|
|
>
|
|
|
|
|
{/* Hacky workaround for selectable placeholder issue */}
|
|
|
|
|
<option value="" selected hidden disabled>
|
|
|
|
|
{row.label}
|
|
|
|
|
</option>
|
|
|
|
|
{row.options?.map((option, index) => (
|
|
|
|
|
<option key={`option-${index}`} value={option.value}>
|
|
|
|
|
{option.label}
|
|
|
|
|
</option>
|
|
|
|
|
))}
|
|
|
|
|
</Select>
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
2022-05-02 13:51:01 +02:00
|
|
|
</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;
|