Skip to content

Commit 2d3b25c

Browse files
authored
fix(new-dashboard): team member invite & creation (#4487)
1 parent 20b0c73 commit 2d3b25c

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

packages/app/src/app/components/UserSearchInput/UserSearchInput.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useEffect } from 'react';
22
import Downshift, { DownshiftProps } from 'downshift';
33
import css from '@styled-system/css';
44
import { Input, List, ListAction } from '@codesandbox/components';
5+
import { useOvermind } from 'app/overmind';
56

67
type User = {
78
id: string;
@@ -11,17 +12,25 @@ type User = {
1112

1213
interface IUserAutoComplete {
1314
inputValue: string;
15+
allowSelf?: boolean;
1416
children: (answer: {
1517
users: User[];
1618
loading: boolean;
1719
error: Error | null;
1820
}) => JSX.Element;
1921
}
2022

21-
const UserAutoComplete = ({ inputValue, children }: IUserAutoComplete) => {
23+
const UserAutoComplete = ({
24+
inputValue,
25+
children,
26+
allowSelf = false,
27+
}: IUserAutoComplete) => {
2228
const [users, setUsers] = React.useState<User[]>([]);
2329
const [loading, setLoading] = React.useState<boolean>(true);
2430
const [error, setError] = React.useState<Error | null>(null);
31+
const {
32+
state: { user },
33+
} = useOvermind();
2534
useEffect(() => {
2635
setLoading(true);
2736
setError(null);
@@ -34,7 +43,10 @@ const UserAutoComplete = ({ inputValue, children }: IUserAutoComplete) => {
3443
fetch(`/api/v1/users/search?username=${inputValue}`)
3544
.then(x => x.json())
3645
.then(x => {
37-
setUsers(x);
46+
const fetchedUsers = allowSelf
47+
? x
48+
: x.filter(member => member.username !== user?.username);
49+
setUsers(fetchedUsers);
3850
setLoading(false);
3951
})
4052
.catch(e => {
@@ -50,14 +62,15 @@ const UserAutoComplete = ({ inputValue, children }: IUserAutoComplete) => {
5062
clearTimeout(timeoutId);
5163
}
5264
};
53-
}, [inputValue]);
65+
}, [allowSelf, inputValue, user]);
5466

5567
return children({ users, loading, error });
5668
};
5769

5870
interface IUserSearchInputProps {
5971
onInputValueChange: DownshiftProps<string>['onInputValueChange'];
6072
inputValue: string;
73+
allowSelf?: boolean;
6174
[key: string]: any;
6275
}
6376

@@ -67,6 +80,7 @@ const InputWithoutTypes = Input as any;
6780
export const UserSearchInput = ({
6881
onInputValueChange,
6982
inputValue,
83+
allowSelf = false,
7084
...props
7185
}: IUserSearchInputProps) => (
7286
<Downshift
@@ -98,7 +112,7 @@ export const UserSearchInput = ({
98112
/>
99113
</div>
100114

101-
<UserAutoComplete inputValue={currentInputValue}>
115+
<UserAutoComplete allowSelf={allowSelf} inputValue={currentInputValue}>
102116
{({ users, error }) =>
103117
users.length > 0 && !error && isOpen ? (
104118
<List

packages/app/src/app/pages/NewDashboard/Content/routes/Settings/NewTeam.tsx

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,26 @@ export const NewTeam = () => {
1717
const onSubmit = async event => {
1818
event.preventDefault();
1919
const teamName = event.target.name.value;
20-
console.warn(teamName, 'created by', user.username);
21-
setLoading(true);
22-
try {
23-
await dashboard.createTeam({ teamName });
24-
setLoading(false);
25-
history.push(dashboardUrls.teamInvite(activeTeam));
26-
} catch {
27-
setLoading(false);
20+
if (teamName && teamName.trim()) {
21+
console.warn(teamName, 'created by', user.username);
22+
event.target.name.setCustomValidity('');
23+
setLoading(true);
24+
try {
25+
await dashboard.createTeam({ teamName });
26+
setLoading(false);
27+
history.push(dashboardUrls.teamInvite(activeTeam));
28+
} catch {
29+
setLoading(false);
30+
}
31+
}
32+
};
33+
34+
const handleInput = e => {
35+
const { value } = e.target;
36+
if (value && value.trim()) {
37+
e.target.setCustomValidity('');
38+
} else {
39+
e.target.setCustomValidity('Workspace name is required.');
2840
}
2941
};
3042

@@ -66,6 +78,8 @@ export const NewTeam = () => {
6678
type="text"
6779
placeholder="Workspace name"
6880
autoFocus
81+
required
82+
onChange={handleInput}
6983
css={css({ height: 8 })}
7084
/>
7185
<Button

packages/app/src/app/pages/NewDashboard/Content/routes/Settings/TeamSettings.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ export const TeamSettings = () => {
195195
>
196196
<UserSearchInput
197197
inputValue={inviteValue}
198+
allowSelf={false}
198199
onInputValueChange={val => {
199200
setInviteValue(val);
200201
}}

0 commit comments

Comments
 (0)