2022-08-31 04:45:55 +00:00
|
|
|
import clsx from 'clsx'
|
2022-10-05 07:23:23 +00:00
|
|
|
import { DragDropContext, Droppable, Draggable } from '@hello-pangea/dnd'
|
2022-08-31 04:45:55 +00:00
|
|
|
import { MenuIcon } from '@heroicons/react/solid'
|
2022-09-17 23:30:27 +00:00
|
|
|
import { toast } from 'react-hot-toast'
|
2022-08-31 04:45:55 +00:00
|
|
|
|
|
|
|
import { Col } from 'web/components/layout/col'
|
|
|
|
import { Row } from 'web/components/layout/row'
|
|
|
|
import { Subtitle } from 'web/components/subtitle'
|
2022-09-16 21:12:24 +00:00
|
|
|
import { keyBy } from 'lodash'
|
2022-09-17 23:30:27 +00:00
|
|
|
import { XCircleIcon } from '@heroicons/react/outline'
|
|
|
|
import { Button } from './button'
|
|
|
|
import { updateUser } from 'web/lib/firebase/users'
|
|
|
|
import { leaveGroup } from 'web/lib/firebase/groups'
|
|
|
|
import { User } from 'common/user'
|
|
|
|
import { useUser } from 'web/hooks/use-user'
|
|
|
|
import { Group } from 'common/group'
|
2022-08-31 04:45:55 +00:00
|
|
|
|
|
|
|
export function ArrangeHome(props: {
|
2022-09-17 23:30:27 +00:00
|
|
|
sections: { label: string; id: string; group?: Group }[]
|
2022-09-16 21:12:24 +00:00
|
|
|
setSectionIds: (sections: string[]) => void
|
2022-08-31 04:45:55 +00:00
|
|
|
}) {
|
2022-09-16 21:12:24 +00:00
|
|
|
const { sections, setSectionIds } = props
|
2022-08-31 04:45:55 +00:00
|
|
|
|
2022-09-16 21:12:24 +00:00
|
|
|
const sectionsById = keyBy(sections, 'id')
|
2022-08-31 04:45:55 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<DragDropContext
|
|
|
|
onDragEnd={(e) => {
|
|
|
|
const { destination, source, draggableId } = e
|
|
|
|
if (!destination) return
|
|
|
|
|
2022-09-16 21:12:24 +00:00
|
|
|
const section = sectionsById[draggableId]
|
2022-08-31 04:45:55 +00:00
|
|
|
|
2022-09-16 21:12:24 +00:00
|
|
|
const newSectionIds = sections.map((section) => section.id)
|
2022-08-31 04:45:55 +00:00
|
|
|
|
2022-09-16 21:12:24 +00:00
|
|
|
newSectionIds.splice(source.index, 1)
|
|
|
|
newSectionIds.splice(destination.index, 0, section.id)
|
2022-08-31 04:45:55 +00:00
|
|
|
|
2022-09-16 21:12:24 +00:00
|
|
|
setSectionIds(newSectionIds)
|
2022-08-31 04:45:55 +00:00
|
|
|
}}
|
|
|
|
>
|
2022-09-12 05:39:04 +00:00
|
|
|
<Row className="relative max-w-md gap-4">
|
|
|
|
<DraggableList items={sections} title="Sections" />
|
2022-08-31 04:45:55 +00:00
|
|
|
</Row>
|
|
|
|
</DragDropContext>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function DraggableList(props: {
|
|
|
|
title: string
|
2022-09-17 23:30:27 +00:00
|
|
|
items: { id: string; label: string; group?: Group }[]
|
2022-08-31 04:45:55 +00:00
|
|
|
}) {
|
2022-09-17 23:30:27 +00:00
|
|
|
const user = useUser()
|
2022-08-31 04:45:55 +00:00
|
|
|
const { title, items } = props
|
|
|
|
return (
|
|
|
|
<Droppable droppableId={title.toLowerCase()}>
|
2022-09-12 05:39:04 +00:00
|
|
|
{(provided) => (
|
2022-08-31 04:45:55 +00:00
|
|
|
<Col
|
|
|
|
{...provided.droppableProps}
|
|
|
|
ref={provided.innerRef}
|
2022-09-12 05:39:04 +00:00
|
|
|
className={clsx('flex-1 items-stretch gap-1 rounded bg-gray-100 p-4')}
|
2022-08-31 04:45:55 +00:00
|
|
|
>
|
2022-09-12 05:39:04 +00:00
|
|
|
<Subtitle text={title} className="mx-2 !mt-0 !mb-4" />
|
2022-08-31 04:45:55 +00:00
|
|
|
{items.map((item, index) => (
|
|
|
|
<Draggable key={item.id} draggableId={item.id} index={index}>
|
|
|
|
{(provided, snapshot) => (
|
|
|
|
<div
|
|
|
|
ref={provided.innerRef}
|
|
|
|
{...provided.draggableProps}
|
|
|
|
{...provided.dragHandleProps}
|
|
|
|
style={provided.draggableProps.style}
|
|
|
|
>
|
2022-09-12 05:39:04 +00:00
|
|
|
<SectionItem
|
|
|
|
className={clsx(
|
|
|
|
snapshot.isDragging && 'z-[9000] bg-gray-200'
|
|
|
|
)}
|
|
|
|
item={item}
|
2022-09-17 23:30:27 +00:00
|
|
|
user={user}
|
2022-09-12 05:39:04 +00:00
|
|
|
/>
|
2022-08-31 04:45:55 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Draggable>
|
|
|
|
))}
|
|
|
|
{provided.placeholder}
|
|
|
|
</Col>
|
|
|
|
)}
|
|
|
|
</Droppable>
|
|
|
|
)
|
|
|
|
}
|
2022-08-31 05:30:31 +00:00
|
|
|
|
2022-09-12 05:39:04 +00:00
|
|
|
const SectionItem = (props: {
|
2022-09-17 23:30:27 +00:00
|
|
|
item: { id: string; label: string; group?: Group }
|
|
|
|
user: User | null | undefined
|
2022-09-12 05:39:04 +00:00
|
|
|
className?: string
|
|
|
|
}) => {
|
2022-09-17 23:30:27 +00:00
|
|
|
const { item, user, className } = props
|
|
|
|
const { group } = item
|
2022-09-12 05:39:04 +00:00
|
|
|
|
|
|
|
return (
|
2022-09-17 23:30:27 +00:00
|
|
|
<Row
|
2022-09-12 05:39:04 +00:00
|
|
|
className={clsx(
|
|
|
|
className,
|
2022-09-17 23:30:27 +00:00
|
|
|
'items-center justify-between gap-4 rounded bg-gray-50 p-2'
|
2022-09-12 05:39:04 +00:00
|
|
|
)}
|
|
|
|
>
|
2022-09-17 23:30:27 +00:00
|
|
|
<Row className="items-center gap-4">
|
|
|
|
<MenuIcon
|
|
|
|
className="h-5 w-5 flex-shrink-0 text-gray-500"
|
|
|
|
aria-hidden="true"
|
|
|
|
/>{' '}
|
|
|
|
{item.label}
|
|
|
|
</Row>
|
|
|
|
|
|
|
|
{group && (
|
|
|
|
<Button
|
2022-09-17 23:52:13 +00:00
|
|
|
className="pt-1 pb-1"
|
2022-09-17 23:30:27 +00:00
|
|
|
color="gray-white"
|
|
|
|
onClick={() => {
|
|
|
|
if (user) {
|
|
|
|
const homeSections = (user.homeSections ?? []).filter(
|
|
|
|
(id) => id !== group.id
|
|
|
|
)
|
|
|
|
updateUser(user.id, { homeSections })
|
|
|
|
|
|
|
|
toast.promise(leaveGroup(group, user.id), {
|
|
|
|
loading: 'Unfollowing group...',
|
|
|
|
success: `Unfollowed ${group.name}`,
|
|
|
|
error: "Couldn't unfollow group, try again?",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<XCircleIcon
|
|
|
|
className={clsx('h-5 w-5 flex-shrink-0')}
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</Row>
|
2022-09-12 05:39:04 +00:00
|
|
|
)
|
|
|
|
}
|