manifold/web/components/groups/edit-group-button.tsx

115 lines
3.4 KiB
TypeScript
Raw Normal View History

import { useState } from 'react'
import clsx from 'clsx'
import { PencilIcon } from '@heroicons/react/outline'
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'
import { Modal } from 'web/components/layout/modal'
import { FilterSelectUsers } from 'web/components/filter-select-users'
import { User } from 'common/user'
import { useMemberIds } from 'web/hooks/use-group'
import { Input } from '../input'
import { Button } from '../button'
export function EditGroupButton(props: { group: Group; className?: string }) {
const { group, className } = props
2022-01-26 20:56:15 +00:00
const router = useRouter()
const [name, setName] = useState(group.name)
const [open, setOpen] = useState(false)
const [isSubmitting, setIsSubmitting] = useState(false)
const [addMemberUsers, setAddMemberUsers] = useState<User[]>([])
const memberIds = useMemberIds(group.id)
function updateOpen(newOpen: boolean) {
setAddMemberUsers([])
setOpen(newOpen)
}
2022-09-20 13:42:41 +00:00
const saveDisabled = name === group.name && addMemberUsers.length === 0
const onSubmit = async () => {
setIsSubmitting(true)
await Promise.all(addMemberUsers.map((user) => joinGroup(group, user.id)))
2022-10-05 13:02:17 +00:00
await updateGroup(group, { name })
setIsSubmitting(false)
updateOpen(false)
}
return (
<div className={clsx('flex p-1', className)}>
<Button
size="sm"
color="gray-white"
className="whitespace-nowrap"
onClick={() => updateOpen(!open)}
>
<PencilIcon className="inline h-4 w-4" /> Edit
</Button>
<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">
<label className="px-1 py-2">
<span className="mb-1">Group name</span>
</label>
<Input
placeholder="Your group name"
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">
<label className="px-1 py-2">
<span className="mb-0">Add members</span>
</label>
<FilterSelectUsers
setSelectedUsers={setAddMemberUsers}
selectedUsers={addMemberUsers}
ignoreUserIds={memberIds}
/>
</div>
<div className="flex">
<Button
color="red"
size="xs"
2022-01-26 20:56:15 +00:00
onClick={() => {
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
</Button>
<Button
color="gray-white"
size="xs"
onClick={() => updateOpen(false)}
>
Cancel
</Button>
<Button
color="green"
disabled={saveDisabled}
loading={isSubmitting}
onClick={onSubmit}
>
Save
</Button>
</div>
</div>
</Modal>
</div>
)
}