Don't allow hiding of home sections

This commit is contained in:
James Grugett 2022-09-10 01:38:34 -05:00
parent 88c098c64d
commit 85d4108fd0
4 changed files with 24 additions and 60 deletions

View File

@ -34,7 +34,7 @@ export type User = {
followerCountCached: number followerCountCached: number
followedCategories?: string[] followedCategories?: string[]
homeSections?: { visible: string[]; hidden: string[] } homeSections?: string[]
referredByUserId?: string referredByUserId?: string
referredByContractId?: string referredByContractId?: string

View File

@ -13,19 +13,13 @@ import { Group } from 'common/group'
export function ArrangeHome(props: { export function ArrangeHome(props: {
user: User | null | undefined user: User | null | undefined
homeSections: { visible: string[]; hidden: string[] } homeSections: string[]
setHomeSections: (homeSections: { setHomeSections: (sections: string[]) => void
visible: string[]
hidden: string[]
}) => void
}) { }) {
const { user, homeSections, setHomeSections } = props const { user, homeSections, setHomeSections } = props
const groups = useMemberGroups(user?.id) ?? [] const groups = useMemberGroups(user?.id) ?? []
const { itemsById, visibleItems, hiddenItems } = getHomeItems( const { itemsById, sections } = getHomeItems(groups, homeSections)
groups,
homeSections
)
return ( return (
<DragDropContext <DragDropContext
@ -35,23 +29,16 @@ export function ArrangeHome(props: {
const item = itemsById[draggableId] const item = itemsById[draggableId]
const newHomeSections = { const newHomeSections = sections.map((section) => section.id)
visible: visibleItems.map((item) => item.id),
hidden: hiddenItems.map((item) => item.id),
}
const sourceSection = source.droppableId as 'visible' | 'hidden' newHomeSections.splice(source.index, 1)
newHomeSections[sourceSection].splice(source.index, 1) newHomeSections.splice(destination.index, 0, item.id)
const destSection = destination.droppableId as 'visible' | 'hidden'
newHomeSections[destSection].splice(destination.index, 0, item.id)
setHomeSections(newHomeSections) setHomeSections(newHomeSections)
}} }}
> >
<Row className="relative max-w-lg gap-4"> <Row className="relative max-w-md gap-4">
<DraggableList items={visibleItems} title="Visible" /> <DraggableList items={sections} title="Sections" />
<DraggableList items={hiddenItems} title="Hidden" />
</Row> </Row>
</DragDropContext> </DragDropContext>
) )
@ -64,16 +51,13 @@ function DraggableList(props: {
const { title, items } = props const { title, items } = props
return ( return (
<Droppable droppableId={title.toLowerCase()}> <Droppable droppableId={title.toLowerCase()}>
{(provided, snapshot) => ( {(provided) => (
<Col <Col
{...provided.droppableProps} {...provided.droppableProps}
ref={provided.innerRef} ref={provided.innerRef}
className={clsx( className={clsx('flex-1 items-stretch gap-1 rounded bg-gray-100 p-4')}
'width-[220px] flex-1 items-start rounded bg-gray-50 p-2',
snapshot.isDraggingOver && 'bg-gray-100'
)}
> >
<Subtitle text={title} className="mx-2 !my-2" /> <Subtitle text={title} className="mx-2 !mt-0 !mb-4" />
{items.map((item, index) => ( {items.map((item, index) => (
<Draggable key={item.id} draggableId={item.id} index={index}> <Draggable key={item.id} draggableId={item.id} index={index}>
{(provided, snapshot) => ( {(provided, snapshot) => (
@ -85,7 +69,7 @@ function DraggableList(props: {
> >
<SectionItem <SectionItem
className={clsx( className={clsx(
snapshot.isDragging && 'z-[9000] bg-gray-300' snapshot.isDragging && 'z-[9000] bg-gray-200'
)} )}
item={item} item={item}
/> />
@ -122,10 +106,7 @@ const SectionItem = (props: {
) )
} }
export const getHomeItems = ( export const getHomeItems = (groups: Group[], sections: string[]) => {
groups: Group[],
homeSections: { visible: string[]; hidden: string[] }
) => {
const items = [ const items = [
{ label: 'Trending', id: 'score' }, { label: 'Trending', id: 'score' },
{ label: 'New for you', id: 'newest' }, { label: 'New for you', id: 'newest' },
@ -136,23 +117,13 @@ export const getHomeItems = (
] ]
const itemsById = keyBy(items, 'id') const itemsById = keyBy(items, 'id')
const { visible, hidden } = homeSections const sectionItems = filterDefined(sections.map((id) => itemsById[id]))
const [visibleItems, hiddenItems] = [ // Add unmentioned items to the end.
filterDefined(visible.map((id) => itemsById[id])), sectionItems.push(...items.filter((item) => !sectionItems.includes(item)))
filterDefined(hidden.map((id) => itemsById[id])),
]
// Add unmentioned items to the visible list.
visibleItems.push(
...items.filter(
(item) => !visibleItems.includes(item) && !hiddenItems.includes(item)
)
)
return { return {
visibleItems, sections: sectionItems,
hiddenItems,
itemsById, itemsById,
} }
} }

View File

@ -16,14 +16,9 @@ export default function Home() {
useTracking('edit home') useTracking('edit home')
const [homeSections, setHomeSections] = useState( const [homeSections, setHomeSections] = useState(user?.homeSections ?? [])
user?.homeSections ?? { visible: [], hidden: [] }
)
const updateHomeSections = (newHomeSections: { const updateHomeSections = (newHomeSections: string[]) => {
visible: string[]
hidden: string[]
}) => {
if (!user) return if (!user) return
updateUser(user.id, { homeSections: newHomeSections }) updateUser(user.id, { homeSections: newHomeSections })
setHomeSections(newHomeSections) setHomeSections(newHomeSections)
@ -31,7 +26,7 @@ export default function Home() {
return ( return (
<Page> <Page>
<Col className="pm:mx-10 gap-4 px-4 pb-12"> <Col className="pm:mx-10 gap-4 px-4 pb-6 pt-2">
<Row className={'w-full items-center justify-between'}> <Row className={'w-full items-center justify-between'}>
<Title text="Edit your home page" /> <Title text="Edit your home page" />
<DoneButton /> <DoneButton />

View File

@ -38,10 +38,8 @@ const Home = () => {
const groups = useMemberGroups(user?.id) ?? [] const groups = useMemberGroups(user?.id) ?? []
const [homeSections] = useState( const [homeSections] = useState(user?.homeSections ?? [])
user?.homeSections ?? { visible: [], hidden: [] } const { sections } = getHomeItems(groups, homeSections)
)
const { visibleItems } = getHomeItems(groups, homeSections)
return ( return (
<Page> <Page>
@ -57,7 +55,7 @@ const Home = () => {
<div className="text-xl text-gray-800">Daily movers</div> <div className="text-xl text-gray-800">Daily movers</div>
<ProbChangeTable userId={user?.id} /> <ProbChangeTable userId={user?.id} />
{visibleItems.map((item) => { {sections.map((item) => {
const { id } = item const { id } = item
if (id === 'your-bets') { if (id === 'your-bets') {
return ( return (