simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 18 | import { GppMaybe, Warning } from '@mui/icons-material'; |
| 19 | import { |
| 20 | IconButtonProps, |
| 21 | List, |
| 22 | ListItem, |
| 23 | ListItemIcon, |
| 24 | Stack, |
| 25 | TextField, |
| 26 | TextFieldProps, |
| 27 | Typography, |
| 28 | } from '@mui/material'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 29 | import { styled } from '@mui/material/styles'; |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 30 | import { ChangeEvent, ReactElement, useCallback, useEffect, useState } from 'react'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 31 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 32 | import { InfoButton, ToggleVisibilityButton } from './Button'; |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 33 | import RulesDialog from './RulesDialog'; |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 34 | import { CheckedIcon, LockIcon, PenIcon, PersonIcon, RoundSaltireIcon } from './SvgIcon'; |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 35 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 36 | const iconsHeight = '16px'; |
| 37 | const StyledCheckedIconSuccess = styled(CheckedIcon)(({ theme }) => ({ |
| 38 | height: iconsHeight, |
| 39 | color: theme.palette.success.main, |
| 40 | })); |
| 41 | const StyledRoundSaltireIconError = styled(RoundSaltireIcon)(({ theme }) => ({ |
| 42 | height: iconsHeight, |
| 43 | color: theme.palette.error.main, |
| 44 | })); |
| 45 | const StyledPenIconLight = styled(PenIcon)({ height: iconsHeight, color: '#03B9E9' }); |
| 46 | const StyledPenIconDark = styled(PenIcon)(({ theme }) => ({ height: iconsHeight, color: theme.palette.primary.dark })); |
| 47 | const StyledPersonIconLight = styled(PersonIcon)({ height: iconsHeight, color: '#03B9E9' }); |
| 48 | const StyledLockIcon = styled(LockIcon)({ height: iconsHeight, color: '#03B9E9' }); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 49 | |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 50 | export type InputProps = TextFieldProps & { |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 51 | infoButtonProps?: IconButtonProps; |
| 52 | success?: boolean; |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 53 | tooltipTitle: string; |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 54 | }; |
| 55 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 56 | export const UsernameInput = ({ |
| 57 | infoButtonProps, |
| 58 | onChange: _onChange, |
| 59 | success, |
| 60 | tooltipTitle, |
| 61 | ...props |
| 62 | }: InputProps) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 63 | const [isSelected, setIsSelected] = useState(false); |
| 64 | const [input, setInput] = useState(props.defaultValue); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 65 | const [startAdornment, setStartAdornment] = useState<ReactElement | undefined>(); |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 66 | const [isDialogOpened, setIsDialogOpened] = useState<boolean>(false); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 67 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 68 | const onChange = useCallback( |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 69 | (event: ChangeEvent<HTMLInputElement>) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 70 | setInput(event.target.value); |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 71 | _onChange?.(event); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 72 | }, |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 73 | [_onChange] |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 74 | ); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 75 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 76 | useEffect(() => { |
| 77 | /* Handle startAdornment */ |
| 78 | let Icon = StyledPersonIconLight; |
| 79 | let visibility = 'visible'; |
| 80 | if (props.error) { |
| 81 | Icon = StyledRoundSaltireIconError; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 82 | } else if (success) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 83 | Icon = StyledCheckedIconSuccess; |
| 84 | } else if (!isSelected && !input) { |
| 85 | visibility = 'hidden'; // keep icon's space so text does not move |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 86 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 87 | setStartAdornment(<Icon sx={{ visibility }} />); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 88 | }, [props.error, success, isSelected, input]); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 89 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 90 | return ( |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 91 | <> |
| 92 | <RulesDialog openDialog={isDialogOpened} title="Username rules :" closeDialog={() => setIsDialogOpened(false)}> |
| 93 | <UsernameRules /> |
| 94 | </RulesDialog> |
| 95 | <TextField |
| 96 | {...props} |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 97 | color={inputColor(props.error, success)} |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 98 | label={'Choose an identifier'} |
| 99 | variant="standard" |
| 100 | InputLabelProps={{ shrink: !!(isSelected || input) }} |
| 101 | onChange={onChange} |
| 102 | InputProps={{ |
| 103 | startAdornment, |
| 104 | endAdornment: ( |
| 105 | <InfoButton tooltipTitle={tooltipTitle} {...infoButtonProps} onClick={() => setIsDialogOpened(true)} /> |
| 106 | ), |
| 107 | }} |
| 108 | onFocus={() => setIsSelected(true)} |
| 109 | onBlur={() => setIsSelected(false)} |
| 110 | /> |
| 111 | </> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 112 | ); |
| 113 | }; |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 114 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 115 | export const PasswordInput = ({ |
| 116 | infoButtonProps, |
| 117 | onChange: _onChange, |
| 118 | success, |
| 119 | tooltipTitle, |
| 120 | ...props |
| 121 | }: InputProps) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 122 | const [showPassword, setShowPassword] = useState(false); |
| 123 | const [isSelected, setIsSelected] = useState(false); |
| 124 | const [input, setInput] = useState(props.defaultValue); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 125 | const [startAdornment, setStartAdornment] = useState<ReactElement | undefined>(); |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 126 | const [isDialogOpened, setIsDialogOpened] = useState<boolean>(false); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 127 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 128 | const toggleShowPassword = () => { |
| 129 | setShowPassword((showPassword) => !showPassword); |
| 130 | }; |
| 131 | |
| 132 | const onChange = useCallback( |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 133 | (event: ChangeEvent<HTMLInputElement>) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 134 | setInput(event.target.value); |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 135 | _onChange?.(event); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 136 | }, |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 137 | [_onChange] |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 138 | ); |
| 139 | |
| 140 | useEffect(() => { |
| 141 | /* Handle startAdornment */ |
| 142 | let Icon = StyledLockIcon; |
| 143 | let visibility = 'visible'; |
| 144 | if (props.error) { |
| 145 | Icon = StyledRoundSaltireIconError; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 146 | } else if (success) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 147 | Icon = StyledCheckedIconSuccess; |
| 148 | } else if (!isSelected && !input) { |
| 149 | visibility = 'hidden'; // keep icon's space so text does not move |
| 150 | } |
| 151 | setStartAdornment(<Icon sx={{ visibility }} />); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 152 | }, [props.error, success, isSelected, input]); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 153 | |
| 154 | return ( |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 155 | <> |
| 156 | <RulesDialog openDialog={isDialogOpened} title="Password rules :" closeDialog={() => setIsDialogOpened(false)}> |
| 157 | <PasswordRules /> |
| 158 | </RulesDialog> |
| 159 | <TextField |
| 160 | {...props} |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 161 | color={inputColor(props.error, success)} |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 162 | label="Password" |
| 163 | type={showPassword ? 'text' : 'password'} |
| 164 | variant="standard" |
| 165 | autoComplete="current-password" |
| 166 | InputLabelProps={{ shrink: !!(isSelected || input) }} |
| 167 | onChange={onChange} |
| 168 | InputProps={{ |
| 169 | startAdornment, |
| 170 | endAdornment: ( |
| 171 | <Stack direction="row" spacing="14px" alignItems="center"> |
| 172 | <InfoButton tooltipTitle={tooltipTitle} {...infoButtonProps} onClick={() => setIsDialogOpened(true)} /> |
| 173 | <ToggleVisibilityButton visible={showPassword} onClick={toggleShowPassword} /> |
| 174 | </Stack> |
| 175 | ), |
| 176 | }} |
| 177 | onFocus={() => setIsSelected(true)} |
| 178 | onBlur={() => setIsSelected(false)} |
| 179 | /> |
| 180 | </> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 181 | ); |
| 182 | }; |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 183 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 184 | export const NickNameInput = ({ onChange: _onChange, ...props }: TextFieldProps) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 185 | const [isSelected, setIsSelected] = useState(false); |
| 186 | const [input, setInput] = useState(props.defaultValue); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 187 | const [startAdornmentVisibility, setStartAdornmentVisibility] = useState<'visible' | 'hidden'>('hidden'); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 188 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 189 | const onChange = useCallback( |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 190 | (event: ChangeEvent<HTMLInputElement>) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 191 | setInput(event.target.value); |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 192 | _onChange?.(event); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 193 | }, |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 194 | [_onChange] |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 195 | ); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 196 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 197 | useEffect(() => { |
| 198 | setStartAdornmentVisibility(isSelected || input ? 'visible' : 'hidden'); |
| 199 | }, [isSelected, input]); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 200 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 201 | return ( |
| 202 | <TextField |
| 203 | {...props} |
| 204 | label="Nickname, surname..." |
| 205 | variant="standard" |
| 206 | InputLabelProps={{ shrink: !!(isSelected || input) }} |
| 207 | onChange={onChange} |
| 208 | InputProps={{ |
| 209 | startAdornment: <StyledPenIconLight sx={{ visibility: startAdornmentVisibility }} />, |
| 210 | }} |
| 211 | onFocus={() => setIsSelected(true)} |
| 212 | onBlur={() => setIsSelected(false)} |
| 213 | /> |
| 214 | ); |
| 215 | }; |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 216 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 217 | export const RegularInput = ({ onChange: _onChange, ...props }: TextFieldProps) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 218 | const [isSelected, setIsSelected] = useState(false); |
| 219 | const [input, setInput] = useState(props.defaultValue); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 220 | const [startAdornmentVisibility, setStartAdornmentVisibility] = useState<'visible' | 'hidden'>('hidden'); |
| 221 | const [endAdornmentVisibility, setEndAdornmentVisibility] = useState<'visible' | 'hidden'>('visible'); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 222 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 223 | const onChange = useCallback( |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 224 | (event: ChangeEvent<HTMLInputElement>) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 225 | setInput(event.target.value); |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 226 | _onChange?.(event); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 227 | }, |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 228 | [_onChange] |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 229 | ); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 230 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 231 | useEffect(() => { |
| 232 | setStartAdornmentVisibility(isSelected || input ? 'visible' : 'hidden'); |
| 233 | setEndAdornmentVisibility(isSelected || input ? 'hidden' : 'visible'); |
| 234 | }, [isSelected, input]); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 235 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 236 | return ( |
| 237 | <TextField |
| 238 | {...props} |
| 239 | variant="standard" |
| 240 | InputLabelProps={{ shrink: !!(isSelected || input) }} |
| 241 | onChange={onChange} |
| 242 | InputProps={{ |
| 243 | startAdornment: <StyledPenIconLight sx={{ visibility: startAdornmentVisibility }} />, |
| 244 | endAdornment: <StyledPenIconDark sx={{ visibility: endAdornmentVisibility }} />, |
| 245 | }} |
| 246 | onFocus={() => setIsSelected(true)} |
| 247 | onBlur={() => setIsSelected(false)} |
| 248 | /> |
| 249 | ); |
| 250 | }; |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 251 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 252 | function inputColor( |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 253 | error?: boolean, |
| 254 | success?: boolean |
| 255 | ): 'success' | 'error' | 'primary' | 'secondary' | 'info' | 'warning' | undefined { |
| 256 | return error ? 'error' : success ? 'success' : 'primary'; |
| 257 | } |
| 258 | |
| 259 | const PasswordRules = () => { |
| 260 | return ( |
| 261 | <Typography variant="body1"> |
| 262 | <List> |
| 263 | <ListItem> |
| 264 | <ListItemIcon> |
| 265 | <GppMaybe /> |
| 266 | </ListItemIcon> |
| 267 | The password must contain at least 1 lowercase alphabetical character. |
| 268 | </ListItem> |
| 269 | <ListItem> |
| 270 | <ListItemIcon> |
| 271 | <GppMaybe /> |
| 272 | </ListItemIcon> |
| 273 | The password must contain at least 1 uppercase alphabetical character. |
| 274 | </ListItem> |
| 275 | <ListItem> |
| 276 | <ListItemIcon> |
| 277 | <GppMaybe /> |
| 278 | </ListItemIcon> |
| 279 | The password must contain at least 1 numeric character. |
| 280 | </ListItem> |
| 281 | <ListItem> |
| 282 | <ListItemIcon> |
| 283 | <GppMaybe /> |
| 284 | </ListItemIcon> |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 285 | The password must contain at least 1 special character. |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 286 | </ListItem> |
| 287 | <ListItem> |
| 288 | <ListItemIcon> |
| 289 | <GppMaybe /> |
| 290 | </ListItemIcon> |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 291 | The password must be 10 characters or longer to be considered strong. |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 292 | </ListItem> |
| 293 | </List> |
| 294 | </Typography> |
| 295 | ); |
| 296 | }; |
| 297 | |
| 298 | const UsernameRules = () => { |
| 299 | return ( |
| 300 | <Typography variant="body1"> |
| 301 | <List> |
| 302 | <ListItem> |
| 303 | <ListItemIcon> |
| 304 | <Warning /> |
| 305 | </ListItemIcon> |
| 306 | The username must be 3 to 32 characters long. |
| 307 | </ListItem> |
| 308 | <ListItem> |
| 309 | <ListItemIcon> |
| 310 | <Warning /> |
| 311 | </ListItemIcon> |
| 312 | The username may contain lowercase and uppercase alphabetical characters. |
| 313 | </ListItem> |
| 314 | <ListItem> |
| 315 | <ListItemIcon> |
| 316 | <Warning /> |
| 317 | </ListItemIcon> |
| 318 | The username may contain hyphens {'(-)'}. |
| 319 | </ListItem> |
| 320 | <ListItem> |
| 321 | <ListItemIcon> |
| 322 | <Warning /> |
| 323 | </ListItemIcon> |
| 324 | The username may contain underscores {'(_)'}. |
| 325 | </ListItem> |
| 326 | </List> |
| 327 | </Typography> |
| 328 | ); |
| 329 | }; |