blob: 486d9476694709f3d2caa1c9cf7ef6bd298bbcf7 [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';
simond47ef9e2022-09-28 22:24:28 -040020import { useEffect, useState } from 'react';
21import usePromise from 'react-fetch-hook/usePromise';
simon07b4eb02022-09-29 17:50:26 -040022
simond47ef9e2022-09-28 22:24:28 -040023import authManager from '../AuthManager';
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('');
Adrien Béraud88a52442021-04-26 12:11:41 -040029
simond47ef9e2022-09-28 22:24:28 -040030 const { isLoading, data, error } = usePromise(
31 () =>
32 isInputValid(query)
33 ? authManager.fetch(`/api/ns/name/${query}`).then((res) => {
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éraud88a52442021-04-26 12:11:41 -040040
41 useEffect(() => {
42 if (!isLoading) {
simon80b7b3b2022-09-28 17:50:10 -040043 if (error === 404) setName(query);
44 else setName('');
Adrien Béraud88a52442021-04-26 12:11:41 -040045 }
simon80b7b3b2022-09-28 17:50:10 -040046 }, [setName, query, isLoading, data, error]);
Adrien Béraud88a52442021-04-26 12:11:41 -040047
simond47ef9e2022-09-28 22:24:28 -040048 const handleChange = (event) => setQuery(event.target.value);
Adrien Béraud88a52442021-04-26 12:11:41 -040049
50 return (
simond47ef9e2022-09-28 22:24:28 -040051 <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éraud88a52442021-04-26 12:11:41 -040077}