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'
|
|
|
|
import { deleteGroup, 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-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
|
|
|
|
const { memberIds } = group
|
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 [about, setAbout] = useState(group.about ?? '')
|
|
|
|
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-01-25 20:47:25 +00:00
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
function updateOpen(newOpen: boolean) {
|
|
|
|
setAddMemberUsers([])
|
|
|
|
setOpen(newOpen)
|
|
|
|
}
|
2022-01-25 20:47:25 +00:00
|
|
|
|
|
|
|
const saveDisabled =
|
2022-06-22 16:35:50 +00:00
|
|
|
name === group.name && about === group.about && addMemberUsers.length === 0
|
2022-01-25 20:47:25 +00:00
|
|
|
|
|
|
|
const onSubmit = async () => {
|
|
|
|
setIsSubmitting(true)
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
await updateGroup(group, {
|
2022-01-25 20:47:25 +00:00
|
|
|
name,
|
|
|
|
about,
|
2022-06-22 16:35:50 +00:00
|
|
|
memberIds: [...memberIds, ...addMemberUsers.map((user) => user.id)],
|
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)}>
|
|
|
|
<div
|
2022-01-25 20:47:25 +00:00
|
|
|
className={clsx(
|
2022-06-22 16:35:50 +00:00
|
|
|
'btn-ghost cursor-pointer whitespace-nowrap rounded-full text-sm text-white'
|
2022-01-25 20:47:25 +00:00
|
|
|
)}
|
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-06-22 16:35:50 +00:00
|
|
|
</div>
|
|
|
|
<Modal open={open} setOpen={updateOpen}>
|
|
|
|
<div className="h-full rounded-md bg-white p-8">
|
2022-01-25 20:47:25 +00:00
|
|
|
<div className="form-control w-full">
|
|
|
|
<label className="label">
|
2022-06-22 16:35:50 +00:00
|
|
|
<span className="mb-1">Group name</span>
|
2022-01-25 20:47:25 +00:00
|
|
|
</label>
|
|
|
|
|
|
|
|
<input
|
2022-06-22 16:35:50 +00:00
|
|
|
placeholder="Your group name"
|
2022-01-25 20:47:25 +00:00
|
|
|
className="input input-bordered resize-none"
|
|
|
|
disabled={isSubmitting}
|
|
|
|
value={name}
|
|
|
|
onChange={(e) => setName(e.target.value || '')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<div className="form-control w-full">
|
|
|
|
<label className="label">
|
|
|
|
<span className="mb-1">About</span>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<input
|
|
|
|
placeholder="Short description (140 characters max)"
|
|
|
|
className="input input-bordered resize-none"
|
|
|
|
disabled={isSubmitting}
|
|
|
|
value={about}
|
|
|
|
maxLength={140}
|
|
|
|
onChange={(e) => setAbout(e.target.value || '')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<div className="form-control w-full">
|
|
|
|
<label className="label">
|
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>
|
|
|
|
|
|
|
|
<div className="modal-action">
|
2022-01-26 20:56:15 +00:00
|
|
|
<label
|
|
|
|
htmlFor="edit"
|
|
|
|
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
|
|
|
}
|
|
|
|
}}
|
|
|
|
className={clsx(
|
2022-02-11 18:40:22 +00:00
|
|
|
'btn btn-sm btn-outline mr-auto self-center hover:border-red-500 hover:bg-red-500'
|
2022-01-26 20:56:15 +00:00
|
|
|
)}
|
|
|
|
>
|
|
|
|
Delete
|
|
|
|
</label>
|
2022-06-22 16:35:50 +00:00
|
|
|
<label
|
|
|
|
htmlFor="edit"
|
|
|
|
className={'btn'}
|
|
|
|
onClick={() => updateOpen(false)}
|
|
|
|
>
|
2022-01-25 20:47:25 +00:00
|
|
|
Cancel
|
|
|
|
</label>
|
|
|
|
<label
|
|
|
|
className={clsx(
|
|
|
|
'btn',
|
|
|
|
saveDisabled ? 'btn-disabled' : 'btn-primary',
|
|
|
|
isSubmitting && 'loading'
|
|
|
|
)}
|
|
|
|
htmlFor="edit"
|
|
|
|
onClick={onSubmit}
|
|
|
|
>
|
|
|
|
Save
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-06-22 16:35:50 +00:00
|
|
|
</Modal>
|
2022-01-25 20:47:25 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|