blob: 77c1d738c6407a5686dd40632efb9ae41d3dbea0 [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);
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éraud88a52442021-04-26 12:11:41 -040052
53 useEffect(() => {
54 if (!isLoading) {
simon80b7b3b2022-09-28 17:50:10 -040055 if (error === 404) setName(query);
56 else setName('');
Adrien Béraud88a52442021-04-26 12:11:41 -040057 }
simon80b7b3b2022-09-28 17:50:10 -040058 }, [setName, query, isLoading, data, error]);
Adrien Béraud88a52442021-04-26 12:11:41 -040059
simond47ef9e2022-09-28 22:24:28 -040060 const handleChange = (event) => setQuery(event.target.value);
Adrien Béraud88a52442021-04-26 12:11:41 -040061
62 return (
simond47ef9e2022-09-28 22:24:28 -040063 <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éraud88a52442021-04-26 12:11:41 -040089}