Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate characters for player search #900

Merged
merged 3 commits into from
Apr 1, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/pages/players/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,19 @@ function Players() {
const [searched, setSearched] = useState(false);
const [turnstileToken, setTurnstileToken] = useState();

const searchTextValid = useMemo(() => {
const charactersValid = !!nameFilter.match(/^[a-zA-Z0-9-_]*$/);
const lengthValid = nameFilter.length >= 3 && nameFilter.length <= 15;
setButtonDisabled(!charactersValid || !lengthValid);
setNameResultsError(false);
if (!charactersValid) {
setNameResultsError(`Names can only contain letters, numbers, dashes (-), and underscores (_)`);
}
return charactersValid && lengthValid;
}, [nameFilter, setButtonDisabled, setNameResultsError]);

const searchForName = useCallback(async () => {
if (nameFilter.length < 3 || nameFilter.length > 15) {
if (!searchTextValid) {
return;
}
try {
Expand All @@ -48,7 +59,7 @@ function Players() {
setNameResultsError(error.message);
}
turnstileRef.current.reset();
}, [nameFilter, setNameResults, setNameResultsError, turnstileToken, turnstileRef]);
}, [nameFilter, searchTextValid, setNameResults, setNameResultsError, turnstileToken, turnstileRef]);

const searchResults = useMemo(() => {
if (!searched) {
Expand Down Expand Up @@ -116,7 +127,6 @@ function Players() {
onChange={(event) => {
let newNameFilter = event.target.value;
setNameFilter(newNameFilter);
setButtonDisabled(newNameFilter.length < 3 || newNameFilter.length > 15);
}}
/>
<button className="search-button" onClick={searchForName} disabled={isButtonDisabled || turnstileToken === undefined}>{t('Search')}</button>
Expand Down