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 | */ |
Ziwei Wang | a41e166 | 2023-01-27 14:56:32 -0500 | [diff] [blame] | 18 | import SearchRounded from '@mui/icons-material/SearchRounded'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 19 | import { InputAdornment, TextField } from '@mui/material'; |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 20 | import axios from 'axios'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 21 | import { useEffect, useState } from 'react'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 22 | |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 23 | import { apiUrl } from '../utils/constants'; |
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(''); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 29 | const [isLoading, setIsLoading] = useState(true); |
| 30 | const [error, setError] = useState(); |
| 31 | const [data, setData] = useState(); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 32 | |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 33 | useEffect(() => { |
| 34 | if (isInputValid(query)) { |
| 35 | setIsLoading(true); |
Misha Krieger-Raynauld | 46e9d24 | 2022-11-12 18:02:43 -0500 | [diff] [blame] | 36 | axios.get(`/ns/username/${query}`, { baseURL: apiUrl }).then((res) => { |
| 37 | setIsLoading(false); |
| 38 | if (res.status === 200) { |
| 39 | setData(res.data); |
| 40 | } else { |
| 41 | throw res.status; |
| 42 | } |
| 43 | }); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 44 | } else { |
| 45 | setError(400); |
| 46 | } |
| 47 | }, [query]); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 48 | |
| 49 | useEffect(() => { |
| 50 | if (!isLoading) { |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 51 | if (error === 404) setName(query); |
| 52 | else setName(''); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 53 | } |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 54 | }, [setName, query, isLoading, data, error]); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 55 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 56 | const handleChange = (event) => setQuery(event.target.value); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 57 | |
| 58 | return ( |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 59 | <TextField |
| 60 | className="main-search-input" |
| 61 | type="search" |
| 62 | placeholder="Register a unique name" |
| 63 | error={!error} |
| 64 | label={ |
| 65 | isLoading |
| 66 | ? 'Searching...' |
| 67 | : error && error !== 400 |
| 68 | ? 'This name is available' |
| 69 | : data && data.address |
| 70 | ? 'This name is not available' |
| 71 | : '' |
| 72 | } |
| 73 | value={query} |
| 74 | disabled={props.disabled} |
| 75 | onChange={handleChange} |
| 76 | InputProps={{ |
| 77 | startAdornment: ( |
| 78 | <InputAdornment position="start"> |
| 79 | <SearchRounded /> |
| 80 | </InputAdornment> |
| 81 | ), |
| 82 | }} |
| 83 | /> |
| 84 | ); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 85 | } |