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 | 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 | |
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(''); |
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); |
| 36 | axios |
| 37 | .get(`/ns/username/${query}`, { |
| 38 | baseURL: apiUrl, |
| 39 | }) |
| 40 | .then((res) => { |
| 41 | setIsLoading(false); |
| 42 | if (res.status === 200) { |
| 43 | setData(res.data); |
| 44 | } else { |
| 45 | throw res.status; |
| 46 | } |
| 47 | }); |
| 48 | } else { |
| 49 | setError(400); |
| 50 | } |
| 51 | }, [query]); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 52 | |
| 53 | useEffect(() => { |
| 54 | if (!isLoading) { |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 55 | if (error === 404) setName(query); |
| 56 | else setName(''); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 57 | } |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 58 | }, [setName, query, isLoading, data, error]); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 59 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 60 | const handleChange = (event) => setQuery(event.target.value); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 61 | |
| 62 | return ( |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 63 | <TextField |
| 64 | className="main-search-input" |
| 65 | type="search" |
| 66 | placeholder="Register a unique name" |
| 67 | error={!error} |
| 68 | label={ |
| 69 | isLoading |
| 70 | ? 'Searching...' |
| 71 | : error && error !== 400 |
| 72 | ? 'This name is available' |
| 73 | : data && data.address |
| 74 | ? 'This name is not available' |
| 75 | : '' |
| 76 | } |
| 77 | value={query} |
| 78 | disabled={props.disabled} |
| 79 | onChange={handleChange} |
| 80 | InputProps={{ |
| 81 | startAdornment: ( |
| 82 | <InputAdornment position="start"> |
| 83 | <SearchRounded /> |
| 84 | </InputAdornment> |
| 85 | ), |
| 86 | }} |
| 87 | /> |
| 88 | ); |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 89 | } |