|
| 1 | +import { useState, useEffect } from "react"; |
| 2 | +import { Typography, Box, Button, TextField, Chip, Stack } from "@mui/material"; |
| 3 | +import { Add } from "@mui/icons-material"; |
| 4 | +import { useWatch } from "react-hook-form"; |
| 5 | + |
| 6 | +const CippAliasDialog = ({ formHook }) => { |
| 7 | + const [newAlias, setNewAlias] = useState(""); |
| 8 | + |
| 9 | + // Initialize the form field if it doesn't exist |
| 10 | + useEffect(() => { |
| 11 | + // Set default empty array if AddedAliases doesn't exist in the form |
| 12 | + if (!formHook.getValues("AddedAliases")) { |
| 13 | + formHook.setValue("AddedAliases", []); |
| 14 | + } |
| 15 | + }, [formHook]); |
| 16 | + |
| 17 | + // Use useWatch to subscribe to form field changes |
| 18 | + const aliasList = useWatch({ |
| 19 | + control: formHook.control, |
| 20 | + name: "AddedAliases", |
| 21 | + defaultValue: [], |
| 22 | + }); |
| 23 | + |
| 24 | + const isPending = formHook.formState.isSubmitting; |
| 25 | + |
| 26 | + const handleAddAlias = () => { |
| 27 | + if (newAlias.trim()) { |
| 28 | + const currentAliases = formHook.getValues("AddedAliases") || []; |
| 29 | + const newList = [...currentAliases, newAlias.trim()]; |
| 30 | + formHook.setValue("AddedAliases", newList, { shouldValidate: true }); |
| 31 | + setNewAlias(""); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + const handleDeleteAlias = (aliasToDelete) => { |
| 36 | + const currentAliases = formHook.getValues("AddedAliases") || []; |
| 37 | + const updatedList = currentAliases.filter((alias) => alias !== aliasToDelete); |
| 38 | + formHook.setValue("AddedAliases", updatedList, { shouldValidate: true }); |
| 39 | + }; |
| 40 | + |
| 41 | + const handleKeyPress = (event) => { |
| 42 | + if (event.key === "Enter") { |
| 43 | + event.preventDefault(); |
| 44 | + handleAddAlias(); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + return ( |
| 49 | + <> |
| 50 | + <Stack spacing={3} sx={{ mt: 1 }}> |
| 51 | + <Typography variant="body2" color="text.secondary"> |
| 52 | + Add proxy addresses (aliases) for this user. Enter each alias and click Add or press |
| 53 | + Enter. |
| 54 | + </Typography> |
| 55 | + <Box sx={{ display: "flex", gap: 1 }}> |
| 56 | + <TextField |
| 57 | + fullWidth |
| 58 | + value={newAlias} |
| 59 | + onChange={(e) => setNewAlias(e.target.value)} |
| 60 | + onKeyPress={handleKeyPress} |
| 61 | + placeholder="Enter an alias" |
| 62 | + variant="outlined" |
| 63 | + disabled={isPending} |
| 64 | + size="small" |
| 65 | + sx={{ |
| 66 | + "& .MuiOutlinedInput-root": { |
| 67 | + fontFamily: "monospace", |
| 68 | + "& .MuiOutlinedInput-input": { |
| 69 | + px: 2, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }} |
| 73 | + /> |
| 74 | + <Button |
| 75 | + onClick={handleAddAlias} |
| 76 | + variant="contained" |
| 77 | + disabled={!newAlias.trim() || isPending} |
| 78 | + startIcon={<Add />} |
| 79 | + size="small" |
| 80 | + > |
| 81 | + Add |
| 82 | + </Button> |
| 83 | + </Box> |
| 84 | + <Box |
| 85 | + sx={{ |
| 86 | + display: "flex", |
| 87 | + flexWrap: "wrap", |
| 88 | + gap: 1, |
| 89 | + minHeight: "40px", |
| 90 | + p: 1, |
| 91 | + border: "1px dashed", |
| 92 | + borderColor: "divider", |
| 93 | + borderRadius: 1, |
| 94 | + alignItems: "center", |
| 95 | + justifyContent: "center", |
| 96 | + }} |
| 97 | + > |
| 98 | + {aliasList.length === 0 ? ( |
| 99 | + <Typography |
| 100 | + variant="body2" |
| 101 | + color="text.secondary" |
| 102 | + sx={{ |
| 103 | + px: 2, |
| 104 | + py: 1, |
| 105 | + textAlign: "center", |
| 106 | + width: "100%", |
| 107 | + }} |
| 108 | + > |
| 109 | + No aliases added yet |
| 110 | + </Typography> |
| 111 | + ) : ( |
| 112 | + aliasList.map((alias) => ( |
| 113 | + <Chip |
| 114 | + key={alias} |
| 115 | + label={alias} |
| 116 | + onDelete={() => handleDeleteAlias(alias)} |
| 117 | + color="primary" |
| 118 | + variant="outlined" |
| 119 | + /> |
| 120 | + )) |
| 121 | + )} |
| 122 | + </Box> |
| 123 | + </Stack> |
| 124 | + </> |
| 125 | + ); |
| 126 | +}; |
| 127 | + |
| 128 | +export default CippAliasDialog; |
0 commit comments