2022-06-22 16:35:50 +00:00
|
|
|
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 { User } from 'common/user'
|
|
|
|
import { MAX_GROUP_NAME_LENGTH } from 'common/group'
|
2022-07-10 22:03:15 +00:00
|
|
|
import { createGroup } from 'web/lib/firebase/api'
|
2022-10-10 02:37:24 +00:00
|
|
|
import { Input } from '../input'
|
2022-06-22 16:35:50 +00:00
|
|
|
|
|
|
|
export function CreateGroupButton(props: {
|
|
|
|
user: User
|
|
|
|
className?: string
|
|
|
|
label?: string
|
|
|
|
onOpenStateChange?: (isOpen: boolean) => void
|
|
|
|
goToGroupOnSubmit?: boolean
|
2022-09-18 19:24:29 +00:00
|
|
|
addGroupIdParamOnSubmit?: boolean
|
2022-06-22 16:35:50 +00:00
|
|
|
icon?: JSX.Element
|
|
|
|
}) {
|
2022-09-18 19:24:29 +00:00
|
|
|
const {
|
|
|
|
user,
|
|
|
|
className,
|
|
|
|
label,
|
|
|
|
onOpenStateChange,
|
|
|
|
goToGroupOnSubmit,
|
|
|
|
addGroupIdParamOnSubmit,
|
|
|
|
icon,
|
|
|
|
} = props
|
2022-09-18 18:49:24 +00:00
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
const [name, setName] = useState('')
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
const [errorText, setErrorText] = useState('')
|
|
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
|
|
|
|
const onSubmit = async () => {
|
|
|
|
setIsSubmitting(true)
|
|
|
|
const newGroup = {
|
2022-09-18 18:49:24 +00:00
|
|
|
name,
|
|
|
|
memberIds: [],
|
2022-07-28 18:37:26 +00:00
|
|
|
anyoneCanJoin: true,
|
2022-06-22 16:35:50 +00:00
|
|
|
}
|
|
|
|
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) {
|
|
|
|
if (goToGroupOnSubmit)
|
|
|
|
router.push(groupPath(result.group.slug)).catch((e) => {
|
|
|
|
console.log(e)
|
|
|
|
setErrorText(e.message)
|
|
|
|
})
|
2022-09-18 19:24:29 +00:00
|
|
|
else if (addGroupIdParamOnSubmit) {
|
|
|
|
router.replace({
|
|
|
|
pathname: router.pathname,
|
|
|
|
query: { ...router.query, groupId: result.group.id },
|
|
|
|
})
|
|
|
|
}
|
2022-06-22 16:35:50 +00:00
|
|
|
setIsSubmitting(false)
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
setIsSubmitting(false)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-28 05:23:25 +00:00
|
|
|
if (user.isBannedFromPosting) return <></>
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
return (
|
|
|
|
<ConfirmationButton
|
|
|
|
openModalBtn={{
|
|
|
|
label: label ? label : 'Create Group',
|
|
|
|
icon: icon,
|
2022-09-30 05:41:22 +00:00
|
|
|
className: className,
|
|
|
|
disabled: isSubmitting,
|
2022-06-22 16:35:50 +00:00
|
|
|
}}
|
|
|
|
submitBtn={{
|
|
|
|
label: 'Create',
|
2022-10-04 14:02:20 +00:00
|
|
|
color: 'green',
|
|
|
|
isSubmitting,
|
2022-06-22 16:35:50 +00:00
|
|
|
}}
|
|
|
|
onSubmitWithSuccess={onSubmit}
|
|
|
|
onOpenChanged={(isOpen) => {
|
|
|
|
onOpenStateChange?.(isOpen)
|
|
|
|
setName('')
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Title className="!my-0" text="Create a group" />
|
|
|
|
|
|
|
|
<Col className="gap-1 text-gray-500">
|
2022-09-18 18:49:24 +00:00
|
|
|
<div>You can add markets to your group after creation.</div>
|
2022-06-22 16:35:50 +00:00
|
|
|
</Col>
|
2022-09-18 18:49:24 +00:00
|
|
|
{errorText && <div className={'text-error'}>{errorText}</div>}
|
2022-06-22 16:35:50 +00:00
|
|
|
|
2022-10-13 20:20:04 +00:00
|
|
|
<div className="flex w-full flex-col">
|
2022-09-18 18:49:24 +00:00
|
|
|
<label className="mb-2 ml-1 mt-0">Group name</label>
|
2022-10-10 02:37:24 +00:00
|
|
|
<Input
|
2022-09-18 18:49:24 +00:00
|
|
|
placeholder={'Your group name'}
|
|
|
|
disabled={isSubmitting}
|
|
|
|
value={name}
|
|
|
|
maxLength={MAX_GROUP_NAME_LENGTH}
|
|
|
|
onChange={(e) => setName(e.target.value || '')}
|
|
|
|
/>
|
2022-06-22 16:35:50 +00:00
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
</div>
|
|
|
|
</ConfirmationButton>
|
|
|
|
)
|
|
|
|
}
|