2022-01-25 20:47:25 +00:00
|
|
|
import { useState } from 'react'
|
|
|
|
import clsx from 'clsx'
|
|
|
|
import { PencilIcon } from '@heroicons/react/outline'
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
import { Group } from 'common/group'
|
2022-10-05 13:02:17 +00:00
|
|
|
import { deleteGroup, joinGroup, updateGroup } from 'web/lib/firebase/groups'
|
2022-03-17 07:56:25 +00:00
|
|
|
import { Spacer } from '../layout/spacer'
|
2022-01-26 20:56:15 +00:00
|
|
|
import { useRouter } from 'next/router'
|
2022-06-22 16:35:50 +00:00
|
|
|
import { Modal } from 'web/components/layout/modal'
|
|
|
|
import { FilterSelectUsers } from 'web/components/filter-select-users'
|
|
|
|
import { User } from 'common/user'
|
2022-09-03 00:06:48 +00:00
|
|
|
import { useMemberIds } from 'web/hooks/use-group'
|
2022-10-10 02:37:24 +00:00
|
|
|
import { Input } from '../input'
|
2022-10-12 19:27:17 +00:00
|
|
|
import { Button } from '../button'
|
2022-01-25 20:47:25 +00:00
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
export function EditGroupButton(props: { group: Group; className?: string }) {
|
|
|
|
const { group, className } = props
|
2022-01-26 20:56:15 +00:00
|
|
|
const router = useRouter()
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
const [name, setName] = useState(group.name)
|
|
|
|
const [open, setOpen] = useState(false)
|
2022-01-25 20:47:25 +00:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
2022-06-22 16:35:50 +00:00
|
|
|
const [addMemberUsers, setAddMemberUsers] = useState<User[]>([])
|
2022-09-03 00:06:48 +00:00
|
|
|
const memberIds = useMemberIds(group.id)
|
2022-06-22 16:35:50 +00:00
|
|
|
function updateOpen(newOpen: boolean) {
|
|
|
|
setAddMemberUsers([])
|
|
|
|
setOpen(newOpen)
|
|
|
|
}
|
2022-01-25 20:47:25 +00:00
|
|
|
|
2022-09-20 13:42:41 +00:00
|
|
|
const saveDisabled = name === group.name && addMemberUsers.length === 0
|
2022-01-25 20:47:25 +00:00
|
|
|
|
|
|
|
const onSubmit = async () => {
|
|
|
|
setIsSubmitting(true)
|
|
|
|
|
2022-09-03 00:06:48 +00:00
|
|
|
await Promise.all(addMemberUsers.map((user) => joinGroup(group, user.id)))
|
2022-10-05 13:02:17 +00:00
|
|
|
await updateGroup(group, { name })
|
2022-01-25 20:47:25 +00:00
|
|
|
|
|
|
|
setIsSubmitting(false)
|
2022-06-22 16:35:50 +00:00
|
|
|
updateOpen(false)
|
2022-01-25 20:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-06-22 16:35:50 +00:00
|
|
|
<div className={clsx('flex p-1', className)}>
|
2022-10-12 19:27:17 +00:00
|
|
|
<Button
|
|
|
|
size="sm"
|
|
|
|
color="gray-white"
|
|
|
|
className="whitespace-nowrap"
|
2022-06-22 16:35:50 +00:00
|
|
|
onClick={() => updateOpen(!open)}
|
2022-01-25 20:47:25 +00:00
|
|
|
>
|
2022-02-11 18:40:22 +00:00
|
|
|
<PencilIcon className="inline h-4 w-4" /> Edit
|
2022-10-12 19:27:17 +00:00
|
|
|
</Button>
|
2022-06-22 16:35:50 +00:00
|
|
|
<Modal open={open} setOpen={updateOpen}>
|
|
|
|
<div className="h-full rounded-md bg-white p-8">
|
2022-10-13 20:20:04 +00:00
|
|
|
<div className="flex w-full flex-col">
|
2022-10-13 18:23:42 +00:00
|
|
|
<label className="px-1 py-2">
|
2022-06-22 16:35:50 +00:00
|
|
|
<span className="mb-1">Group name</span>
|
2022-01-25 20:47:25 +00:00
|
|
|
</label>
|
|
|
|
|
2022-10-10 02:37:24 +00:00
|
|
|
<Input
|
2022-06-22 16:35:50 +00:00
|
|
|
placeholder="Your group name"
|
2022-01-25 20:47:25 +00:00
|
|
|
disabled={isSubmitting}
|
|
|
|
value={name}
|
|
|
|
onChange={(e) => setName(e.target.value || '')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
2022-10-13 20:20:04 +00:00
|
|
|
<div className="flex w-full flex-col">
|
2022-10-13 18:23:42 +00:00
|
|
|
<label className="px-1 py-2">
|
2022-06-22 16:35:50 +00:00
|
|
|
<span className="mb-0">Add members</span>
|
2022-01-25 20:47:25 +00:00
|
|
|
</label>
|
2022-06-22 16:35:50 +00:00
|
|
|
<FilterSelectUsers
|
|
|
|
setSelectedUsers={setAddMemberUsers}
|
|
|
|
selectedUsers={addMemberUsers}
|
|
|
|
ignoreUserIds={memberIds}
|
2022-01-25 20:47:25 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2022-10-13 18:23:42 +00:00
|
|
|
<div className="flex">
|
2022-10-12 19:27:17 +00:00
|
|
|
<Button
|
|
|
|
color="red"
|
|
|
|
size="xs"
|
2022-01-26 20:56:15 +00:00
|
|
|
onClick={() => {
|
2022-06-22 16:35:50 +00:00
|
|
|
if (confirm('Are you sure you want to delete this group?')) {
|
|
|
|
deleteGroup(group)
|
|
|
|
updateOpen(false)
|
|
|
|
router.replace('/groups')
|
2022-01-26 20:56:15 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Delete
|
2022-10-12 19:27:17 +00:00
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
color="gray-white"
|
|
|
|
size="xs"
|
2022-06-22 16:35:50 +00:00
|
|
|
onClick={() => updateOpen(false)}
|
|
|
|
>
|
2022-01-25 20:47:25 +00:00
|
|
|
Cancel
|
2022-10-12 19:27:17 +00:00
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
color="green"
|
|
|
|
disabled={saveDisabled}
|
|
|
|
loading={isSubmitting}
|
2022-01-25 20:47:25 +00:00
|
|
|
onClick={onSubmit}
|
|
|
|
>
|
|
|
|
Save
|
2022-10-12 19:27:17 +00:00
|
|
|
</Button>
|
2022-01-25 20:47:25 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-06-22 16:35:50 +00:00
|
|
|
</Modal>
|
2022-01-25 20:47:25 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|