3b3717d307
* Folds=>groups * Show groups on user profile * Allow group creation from /create * Refactoring to groups * Convert folds to groups * Add new add to group notification * Fix user profile tab bug * Add groups nav and tab for my groups * Remove bad profile pages * remove comments * Add group list dropdown to sidebar * remove unused * group cards ui * Messages=>Comments, v2, groupDetails * Discussion time * Cleaning up some code * Remove follow count * Fix pool scoring for cpmm * Fix imports * Simplify rules, add GroupUser collection * Fix group cards * Refactor * Refactor * Small fixes * Remove string * Add api error detail handling * Clear name field * Componentize * Spacing * Undo userpage memo * Member groups are already in my tab * Remove active contracts reference for now * Remove unused * Refactoring * Allow adding old questions to a group * Rename * Wording * Throw standard v2 APIError * Hide input for non-members, add about under title * Multiple names to & # more * Move comments firestore rules to appropriate subpaths * Group membership, pool=>volume * Cleanup, useEvent * Raise state to parent * Eliminate unused * Cleaning up * Clean code * Revert tags input deletion * Cleaning code * Stylling * Limit members to display * Array cleanup * Add categories back in * Private=>closed * Unused vars
141 lines
4.2 KiB
TypeScript
141 lines
4.2 KiB
TypeScript
import clsx from 'clsx'
|
|
import { useRouter } from 'next/router'
|
|
import { useState } from 'react'
|
|
import { groupPath } from 'web/lib/firebase/groups'
|
|
import { ConfirmationButton } from '../confirmation-button'
|
|
import { Col } from '../layout/col'
|
|
import { Spacer } from '../layout/spacer'
|
|
import { Title } from '../title'
|
|
import { FilterSelectUsers } from 'web/components/filter-select-users'
|
|
import { User } from 'common/user'
|
|
import { MAX_GROUP_NAME_LENGTH } from 'common/group'
|
|
import { createGroup } from 'web/lib/firebase/api-call'
|
|
|
|
export function CreateGroupButton(props: {
|
|
user: User
|
|
className?: string
|
|
label?: string
|
|
onOpenStateChange?: (isOpen: boolean) => void
|
|
goToGroupOnSubmit?: boolean
|
|
icon?: JSX.Element
|
|
}) {
|
|
const { user, className, label, onOpenStateChange, goToGroupOnSubmit, icon } =
|
|
props
|
|
const [defaultName, setDefaultName] = useState(`${user.name}'s group`)
|
|
const [name, setName] = useState('')
|
|
const [memberUsers, setMemberUsers] = useState<User[]>([])
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
const [errorText, setErrorText] = useState('')
|
|
|
|
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 () => {
|
|
setIsSubmitting(true)
|
|
const groupName = name !== '' ? name : defaultName
|
|
const newGroup = {
|
|
name: groupName,
|
|
memberIds: memberUsers.map((user) => user.id),
|
|
anyoneCanJoin: false,
|
|
}
|
|
const result = await createGroup(newGroup).catch((e) => {
|
|
const errorDetails = e.details[0]
|
|
if (errorDetails)
|
|
setErrorText(
|
|
`Error with ${errorDetails.field} field - ${errorDetails.error} `
|
|
)
|
|
else setErrorText(e.message)
|
|
setIsSubmitting(false)
|
|
console.error(e)
|
|
return e
|
|
})
|
|
console.log(result.details)
|
|
|
|
if (result.group) {
|
|
updateMemberUsers([])
|
|
if (goToGroupOnSubmit)
|
|
router.push(groupPath(result.group.slug)).catch((e) => {
|
|
console.log(e)
|
|
setErrorText(e.message)
|
|
})
|
|
setIsSubmitting(false)
|
|
return true
|
|
} else {
|
|
setIsSubmitting(false)
|
|
return false
|
|
}
|
|
}
|
|
|
|
return (
|
|
<ConfirmationButton
|
|
openModalBtn={{
|
|
label: label ? label : 'Create Group',
|
|
icon: icon,
|
|
className: clsx(
|
|
isSubmitting ? 'loading btn-disabled' : 'btn-primary',
|
|
'btn-sm, normal-case',
|
|
className
|
|
),
|
|
}}
|
|
submitBtn={{
|
|
label: 'Create',
|
|
className: clsx(
|
|
'normal-case',
|
|
isSubmitting ? 'loading btn-disabled' : ' btn-primary'
|
|
),
|
|
}}
|
|
onSubmitWithSuccess={onSubmit}
|
|
onOpenChanged={(isOpen) => {
|
|
onOpenStateChange?.(isOpen)
|
|
updateMemberUsers([])
|
|
setName('')
|
|
}}
|
|
>
|
|
<Title className="!my-0" text="Create a group" />
|
|
|
|
<Col className="gap-1 text-gray-500">
|
|
<div>You can add markets and members to your group after creation.</div>
|
|
</Col>
|
|
<div className={'text-error'}>{errorText}</div>
|
|
|
|
<div>
|
|
<div className="form-control w-full">
|
|
<label className="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
|
|
placeholder={defaultName}
|
|
className="input input-bordered resize-none"
|
|
disabled={isSubmitting}
|
|
value={name}
|
|
maxLength={MAX_GROUP_NAME_LENGTH}
|
|
onChange={(e) => setName(e.target.value || '')}
|
|
/>
|
|
</div>
|
|
|
|
<Spacer h={4} />
|
|
</div>
|
|
</ConfirmationButton>
|
|
)
|
|
}
|