Simplify create group dialog

This commit is contained in:
James Grugett 2022-09-18 13:49:24 -05:00
parent 39119a3419
commit c9e782faa7

View File

@ -6,7 +6,6 @@ import { ConfirmationButton } from '../confirmation-button'
import { Col } from '../layout/col' import { Col } from '../layout/col'
import { Spacer } from '../layout/spacer' import { Spacer } from '../layout/spacer'
import { Title } from '../title' import { Title } from '../title'
import { FilterSelectUsers } from 'web/components/filter-select-users'
import { User } from 'common/user' import { User } from 'common/user'
import { MAX_GROUP_NAME_LENGTH } from 'common/group' import { MAX_GROUP_NAME_LENGTH } from 'common/group'
import { createGroup } from 'web/lib/firebase/api' import { createGroup } from 'web/lib/firebase/api'
@ -21,31 +20,18 @@ export function CreateGroupButton(props: {
}) { }) {
const { user, className, label, onOpenStateChange, goToGroupOnSubmit, icon } = const { user, className, label, onOpenStateChange, goToGroupOnSubmit, icon } =
props props
const [defaultName, setDefaultName] = useState(`${user.name}'s group`)
const [name, setName] = useState('') const [name, setName] = useState('')
const [memberUsers, setMemberUsers] = useState<User[]>([])
const [isSubmitting, setIsSubmitting] = useState(false) const [isSubmitting, setIsSubmitting] = useState(false)
const [errorText, setErrorText] = useState('') const [errorText, setErrorText] = useState('')
const router = useRouter() const router = useRouter()
function updateMemberUsers(users: User[]) {
const usersFirstNames = users.map((user) => user.name.split(' ')[0])
const postFix =
usersFirstNames.length > 3 ? ` & ${usersFirstNames.length - 3} more` : ''
const newName = `${user.name.split(' ')[0]}${
users.length > 0 ? ', ' + usersFirstNames.slice(0, 3).join(', ') : ''
}${postFix}'s group`
setDefaultName(newName)
setMemberUsers(users)
}
const onSubmit = async () => { const onSubmit = async () => {
setIsSubmitting(true) setIsSubmitting(true)
const groupName = name !== '' ? name : defaultName
const newGroup = { const newGroup = {
name: groupName, name,
memberIds: memberUsers.map((user) => user.id), memberIds: [],
anyoneCanJoin: true, anyoneCanJoin: true,
} }
const result = await createGroup(newGroup).catch((e) => { const result = await createGroup(newGroup).catch((e) => {
@ -62,7 +48,6 @@ export function CreateGroupButton(props: {
console.log(result.details) console.log(result.details)
if (result.group) { if (result.group) {
updateMemberUsers([])
if (goToGroupOnSubmit) if (goToGroupOnSubmit)
router.push(groupPath(result.group.slug)).catch((e) => { router.push(groupPath(result.group.slug)).catch((e) => {
console.log(e) console.log(e)
@ -99,41 +84,26 @@ export function CreateGroupButton(props: {
onSubmitWithSuccess={onSubmit} onSubmitWithSuccess={onSubmit}
onOpenChanged={(isOpen) => { onOpenChanged={(isOpen) => {
onOpenStateChange?.(isOpen) onOpenStateChange?.(isOpen)
updateMemberUsers([])
setName('') setName('')
}} }}
> >
<Title className="!my-0" text="Create a group" /> <Title className="!my-0" text="Create a group" />
<Col className="gap-1 text-gray-500"> <Col className="gap-1 text-gray-500">
<div>You can add markets and members to your group after creation.</div> <div>You can add markets to your group after creation.</div>
</Col> </Col>
<div className={'text-error'}>{errorText}</div> {errorText && <div className={'text-error'}>{errorText}</div>}
<div>
<div className="form-control w-full"> <div className="form-control w-full">
<label className="label"> <label className="mb-2 ml-1 mt-0">Group name</label>
<span className="mb-0">Add members (optional)</span>
</label>
<FilterSelectUsers
setSelectedUsers={updateMemberUsers}
selectedUsers={memberUsers}
ignoreUserIds={[user.id]}
/>
</div>
<div className="form-control w-full">
<label className="label">
<span className="mt-1">Group name (optional)</span>
</label>
<input <input
placeholder={defaultName} placeholder={'Your group name'}
className="input input-bordered resize-none" className="input input-bordered resize-none"
disabled={isSubmitting} disabled={isSubmitting}
value={name} value={name}
maxLength={MAX_GROUP_NAME_LENGTH} maxLength={MAX_GROUP_NAME_LENGTH}
onChange={(e) => setName(e.target.value || '')} onChange={(e) => setName(e.target.value || '')}
/> />
</div>
<Spacer h={4} /> <Spacer h={4} />
</div> </div>