blob: 19b8380ade2d43eabf7ca40d0f7cfa86ee35b78c [file] [log] [blame]
simon26e79f72022-10-05 22:16:08 -04001/*
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 */
simon07b4eb02022-09-29 17:50:26 -040018import { SearchRounded } from '@mui/icons-material';
19import { InputAdornment, TextField } from '@mui/material';
simon94fe53e2022-11-10 12:51:58 -050020import axios from 'axios';
simond47ef9e2022-09-28 22:24:28 -040021import { useEffect, useState } from 'react';
simon07b4eb02022-09-29 17:50:26 -040022
simon5da8ca62022-11-09 15:21:25 -050023import { apiUrl } from '../utils/constants.js';
Adrien Béraud88a52442021-04-26 12:11:41 -040024
simond47ef9e2022-09-28 22:24:28 -040025const isInputValid = (input) => input && input.length > 2;
Adrien Béraud88a52442021-04-26 12:11:41 -040026
simon80b7b3b2022-09-28 17:50:10 -040027export default function UsernameChooser({ setName, ...props }) {
simond47ef9e2022-09-28 22:24:28 -040028 const [query, setQuery] = useState('');
simon94fe53e2022-11-10 12:51:58 -050029 const [isLoading, setIsLoading] = useState(true);
30 const [error, setError] = useState();
31 const [data, setData] = useState();
Adrien Béraud88a52442021-04-26 12:11:41 -040032
simon94fe53e2022-11-10 12:51:58 -050033 useEffect(() => {
34 if (isInputValid(query)) {
35 setIsLoading(true);
Misha Krieger-Raynauld46e9d242022-11-12 18:02:43 -050036 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 });
simon94fe53e2022-11-10 12:51:58 -050044 } else {
45 setError(400);
46 }
47 }, [query]);
Adrien Béraud88a52442021-04-26 12:11:41 -040048
49 useEffect(() => {
50 if (!isLoading) {
simon80b7b3b2022-09-28 17:50:10 -040051 if (error === 404) setName(query);
52 else setName('');
Adrien Béraud88a52442021-04-26 12:11:41 -040053 }
simon80b7b3b2022-09-28 17:50:10 -040054 }, [setName, query, isLoading, data, error]);
Adrien Béraud88a52442021-04-26 12:11:41 -040055
simond47ef9e2022-09-28 22:24:28 -040056 const handleChange = (event) => setQuery(event.target.value);
Adrien Béraud88a52442021-04-26 12:11:41 -040057
58 return (
simond47ef9e2022-09-28 22:24:28 -040059 <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éraud88a52442021-04-26 12:11:41 -040085}