Skip to content

Commit ab3391d

Browse files
authored
feat(onboarding-ui): add function to prefill Workspace URL based on W… (#694)
* feat(onboarding-ui): add function to prefill Workspace URL based on Workspace Name * add Divider * refactor: isValidDomanName method * refactor: isValidDomanName method function * refactor: change variable name
1 parent 35e9b17 commit ab3391d

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

packages/onboarding-ui/src/forms/CreateCloudWorkspaceForm/CreateCloudWorkspaceForm.stories.tsx

+41-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,46 @@ import WorkspaceUrlInput from './WorkspaceUrlInput';
66

77
type Args = ComponentProps<typeof CreateCloudWorkspaceForm>;
88

9+
const isValidLength = (domainName: string) => {
10+
if (domainName.length < 3) {
11+
return 'Workspace URL should have at least 3 characters';
12+
}
13+
return true;
14+
};
15+
16+
const isValidFirstSubstring = (domainName: string) => {
17+
const firstSubstring = domainName.slice(0, 3);
18+
if (firstSubstring === 'rc-') {
19+
return 'Workspace URL address unavailable';
20+
}
21+
return true;
22+
};
23+
24+
const isValidCharacter = (domainName: string) => {
25+
const regex = RegExp('^([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]))$');
26+
if (!regex.test(domainName)) {
27+
return 'Invalid character for Workspace URL';
28+
}
29+
return true;
30+
};
31+
32+
const isValidDomainName = async (domainName: string) => {
33+
const validationList = [
34+
isValidLength,
35+
isValidFirstSubstring,
36+
isValidCharacter,
37+
];
38+
39+
for (const validate of validationList) {
40+
const validateMessage = validate(domainName);
41+
if (typeof validateMessage === 'string') {
42+
return validateMessage;
43+
}
44+
}
45+
46+
return true;
47+
};
48+
949
export default {
1050
title: 'forms/CreateCloudWorkspaceForm',
1151
component: CreateCloudWorkspaceForm,
@@ -23,7 +63,7 @@ export default {
2363
['pt', 'Português'],
2464
],
2565
domain: 'rocket.chat',
26-
validateUrl: async (url) => (url === 'rocket' ? 'invalid url' : true),
66+
validateUrl: isValidDomainName,
2767
validateEmail: async (email) =>
2868
email === 'rocket@rocket.chat' ? 'invalid email' : true,
2969
},

packages/onboarding-ui/src/forms/CreateCloudWorkspaceForm/CreateCloudWorkspaceForm.tsx

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import {
55
ButtonGroup,
66
Button,
77
Box,
8+
Divider,
89
EmailInput,
910
TextInput,
1011
Select,
1112
CheckBox,
1213
Grid,
1314
} from '@rocket.chat/fuselage';
14-
import type { ReactElement } from 'react';
15+
import type { ReactElement, FocusEvent } from 'react';
1516
import type { SubmitHandler, Validate } from 'react-hook-form';
1617
import { useForm, Controller } from 'react-hook-form';
1718
import { useTranslation, Trans } from 'react-i18next';
@@ -57,9 +58,16 @@ const CreateCloudWorkspaceForm = ({
5758
register,
5859
control,
5960
handleSubmit,
61+
setValue,
6062
formState: { isValid, isValidating, isSubmitting, errors },
6163
} = useForm<CreateCloudWorkspaceFormPayload>({ mode: 'onChange' });
6264

65+
const onNameBlur = (e: FocusEvent<HTMLInputElement>) => {
66+
const fieldValue = e.target.value;
67+
const workspaceURL = fieldValue.replace(/[^a-zA-Z0-9-]/g, '').toLowerCase();
68+
setValue('workspaceURL', workspaceURL, { shouldValidate: true });
69+
};
70+
6371
return (
6472
<Form onSubmit={handleSubmit(onSubmit)}>
6573
<Form.Title>{t('form.createCloudWorkspace.title')}</Form.Title>
@@ -95,6 +103,7 @@ const CreateCloudWorkspaceForm = ({
95103
{...register('workspaceName', { required: true })}
96104
defaultValue={defaultValues?.workspaceName}
97105
error={errors?.workspaceName?.type || undefined}
106+
onBlur={onNameBlur}
98107
/>
99108
</Field.Row>
100109
{errors.workspaceName && (
@@ -187,6 +196,8 @@ const CreateCloudWorkspaceForm = ({
187196
</Grid.Item>
188197
</Grid>
189198

199+
<Divider mb='x0' />
200+
190201
<Field>
191202
<Field.Row justifyContent='flex-start'>
192203
<CheckBox {...register('agreement', { required: true })} mie='x8' />

0 commit comments

Comments
 (0)