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 | */ |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 18 | import { SearchRounded } from '@mui/icons-material'; |
| 19 | import { InputAdornment, TextField } from '@mui/material'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 20 | import { useEffect, useState } from 'react'; |
| 21 | import usePromise from 'react-fetch-hook/usePromise'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 22 | |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 23 | import { apiUrl } from '../utils/constants.js'; |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 24 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 25 | const isInputValid = (input) => input && input.length > 2; |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 26 | |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 27 | export default function UsernameChooser({ setName, ...props }) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 28 | const [query, setQuery] = useState(''); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 29 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 30 | const { isLoading, data, error } = usePromise( |
| 31 | () => |
| 32 | isInputValid(query) |
simon | 5da8ca6 | 2022-11-09 15:21:25 -0500 | [diff] [blame] | 33 | ? fetch(new URL(`/ns/username/${query}`, apiUrl)).then((res) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 34 | if (res.status === 200) return res.json(); |
| 35 | else throw res.status; |
| 36 | }) |
| 37 | : new Promise((res, rej) => rej(400)), |
| 38 | [query] |
| 39 | ); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 40 | |
| 41 | useEffect(() => { |
| 42 | if (!isLoading) { |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 43 | if (error === 404) setName(query); |
| 44 | else setName(''); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 45 | } |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 46 | }, [setName, query, isLoading, data, error]); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 47 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 48 | const handleChange = (event) => setQuery(event.target.value); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 49 | |
| 50 | return ( |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 51 | <TextField |
| 52 | className="main-search-input" |
| 53 | type="search" |
| 54 | placeholder="Register a unique name" |
| 55 | error={!error} |
| 56 | label={ |
| 57 | isLoading |
| 58 | ? 'Searching...' |
| 59 | : error && error !== 400 |
| 60 | ? 'This name is available' |
| 61 | : data && data.address |
| 62 | ? 'This name is not available' |
| 63 | : '' |
| 64 | } |
| 65 | value={query} |
| 66 | disabled={props.disabled} |
| 67 | onChange={handleChange} |
| 68 | InputProps={{ |
| 69 | startAdornment: ( |
| 70 | <InputAdornment position="start"> |
| 71 | <SearchRounded /> |
| 72 | </InputAdornment> |
| 73 | ), |
| 74 | }} |
| 75 | /> |
| 76 | ); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 77 | } |