From 24c717327f0d00f3a873e144ba442ab32d528c9d Mon Sep 17 00:00:00 2001 From: Bojan Mojsilovic Date: Fri, 6 Oct 2023 16:33:31 +0200 Subject: [PATCH] Edit profile changes regarding username --- src/constants.ts | 1 + src/pages/EditProfile.module.scss | 9 ++++++ src/pages/EditProfile.tsx | 49 +++++++++++++++++++------------ src/translations.ts | 14 +++++++-- 4 files changed, 53 insertions(+), 20 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index a1a55f7..07a4b88 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -230,6 +230,7 @@ export const notificationTypeNoteProps: Record = { } +export const usernameRegex = /^[a-zA-Z0-9\-\_]+$/; // export const odyseeRegex = /odysee\.com\/([a-zA-Z0-9]+)/; // export const magnetRegex = /(magnet:[\S]+)/i; // export const tweetUrlRegex = /https?:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(?:es)?\/(\d+)/; diff --git a/src/pages/EditProfile.module.scss b/src/pages/EditProfile.module.scss index 6f755d8..7db6f62 100644 --- a/src/pages/EditProfile.module.scss +++ b/src/pages/EditProfile.module.scss @@ -246,6 +246,15 @@ form { border-radius: 0px 8px 8px 0px; } } + + .inputError { + color: var(--warning-color); + font-size: 14px; + font-weight: 400; + line-height: 14px; + margin-top: calc(var(--spacing) * (-1)); + margin-bottom: 6px; + } } @media only screen and (max-width: 720px) { diff --git a/src/pages/EditProfile.tsx b/src/pages/EditProfile.tsx index 3747ecb..658c8f8 100644 --- a/src/pages/EditProfile.tsx +++ b/src/pages/EditProfile.tsx @@ -17,7 +17,7 @@ import { sendProfile } from '../lib/profile'; import { useToastContext } from '../components/Toaster/Toaster'; import { APP_ID } from '../App'; import { subscribeTo as uploadSub } from "../uploadSocket"; -import { Kind } from '../constants'; +import { Kind, usernameRegex } from '../constants'; import { uploadMedia } from '../lib/media'; import Loader from '../components/Loader/Loader'; import { useNavigate } from '@solidjs/router'; @@ -40,6 +40,7 @@ const EditProfile: Component = () => { let textArea: HTMLTextAreaElement | undefined; let fileUploadAvatar: HTMLInputElement | undefined; let fileUploadBanner: HTMLInputElement | undefined; + let nameInput: HTMLInputElement | undefined; const [isBannerCached, setIsBannerCached] = createSignal(false); const [isMoreVisible, setIsMoreVisible] = createSignal(false); @@ -49,6 +50,8 @@ const EditProfile: Component = () => { const [avatarPreview, setAvatarPreview] = createSignal(); const [bannerPreview, setBannerPreview] = createSignal(); + const [isNameValid, setIsNameValid] = createSignal(true); + const flagBannerForWarning = () => { const dev = localStorage.getItem('devMode') === 'true'; @@ -140,6 +143,12 @@ const EditProfile: Component = () => { } }); + const onNameInput = () => { + const value = nameInput?.value || ''; + + setIsNameValid(usernameRegex.test(value)) + }; + const onUpload = (target: 'picture' | 'banner', fileUpload: HTMLInputElement | undefined) => { if (!fileUpload) { @@ -224,11 +233,10 @@ const EditProfile: Component = () => { const data = new FormData(e.target as HTMLFormElement); - const displayName = data.get('displayName'); - const name = data.get('name'); + const name = data.get('name')?.toString() || ''; - if (displayName == null || name == null || displayName.toString().length == 0 || name.toString().length === 0) { - toast?.sendWarning('Required fields are not filled'); + if (!usernameRegex.test(name)) { + toast?.sendWarning(intl.formatMessage(tSettings.profile.name.formError)); return false; } @@ -355,19 +363,6 @@ const EditProfile: Component = () => {
-
- - - * - {intl.formatMessage(tSettings.profile.required)} - -
-
@@ -383,11 +378,28 @@ const EditProfile: Component = () => {
+ +
+ {intl.formatMessage(tSettings.profile.name.error)} +
+
+ +
+ +
+
@@ -474,6 +486,7 @@ const EditProfile: Component = () => { diff --git a/src/translations.ts b/src/translations.ts index 5024b84..1891f1b 100644 --- a/src/translations.ts +++ b/src/translations.ts @@ -1252,14 +1252,24 @@ export const settings = { name: { label: { id: 'pages.settings.profile.name.label', - defaultMessage: 'Handle', + defaultMessage: 'Username', description: 'Label for name input on edit profile page', }, placeholder : { id: 'pages.settings.profile.name.placeholder', - defaultMessage: 'Enter handle', + defaultMessage: 'Enter username', description: 'Placeholder for name input on edit profile page', }, + error: { + id: 'pages.settings.profile.name.error', + defaultMessage: 'Spaces and special characters are not allowed in the username', + description: 'Error label for name input on edit profile page', + }, + formError: { + id: 'pages.settings.profile.name.formError', + defaultMessage: 'Username is not valid', + description: 'Error label for invalid form on edit profile page', + }, }, website: { label: {