Pass user and members via props

This commit is contained in:
Ian Philips 2022-09-02 19:36:49 -06:00
parent 57b74a5d09
commit c74d972caf
3 changed files with 36 additions and 12 deletions

View File

@ -72,29 +72,34 @@ function GroupsList(props: { groups: Group[] }) {
function GroupItem(props: { group: Group; className?: string }) { function GroupItem(props: { group: Group; className?: string }) {
const { group, className } = props const { group, className } = props
const user = useUser()
const memberIds = useMemberIds(group.id)
return ( return (
<Row className={clsx('items-center justify-between gap-2 p-2', className)}> <Row className={clsx('items-center justify-between gap-2 p-2', className)}>
<Row className="line-clamp-1 items-center gap-2"> <Row className="line-clamp-1 items-center gap-2">
<GroupLinkItem group={group} /> <GroupLinkItem group={group} />
</Row> </Row>
<JoinOrLeaveGroupButton group={group} /> <JoinOrLeaveGroupButton
group={group}
user={user}
isMember={user ? memberIds?.includes(user.id) : false}
/>
</Row> </Row>
) )
} }
export function JoinOrLeaveGroupButton(props: { export function JoinOrLeaveGroupButton(props: {
group: Group group: Group
isMember: boolean
user: User | undefined | null
small?: boolean small?: boolean
className?: string className?: string
}) { }) {
const { group, small, className } = props const { group, small, className, isMember, user } = props
const currentUser = useUser()
const memberIds = useMemberIds(group.id)
const isMember = memberIds?.includes(currentUser?.id ?? '') ?? false
const smallStyle = const smallStyle =
'btn !btn-xs border-2 border-gray-500 bg-white normal-case text-gray-500 hover:border-gray-500 hover:bg-white hover:text-gray-500' 'btn !btn-xs border-2 border-gray-500 bg-white normal-case text-gray-500 hover:border-gray-500 hover:bg-white hover:text-gray-500'
if (!currentUser) { if (!user) {
if (!group.anyoneCanJoin) if (!group.anyoneCanJoin)
return <div className={clsx(className, 'text-gray-500')}>Closed</div> return <div className={clsx(className, 'text-gray-500')}>Closed</div>
return ( return (
@ -107,12 +112,12 @@ export function JoinOrLeaveGroupButton(props: {
) )
} }
const onJoinGroup = () => { const onJoinGroup = () => {
joinGroup(group, currentUser.id).catch(() => { joinGroup(group, user.id).catch(() => {
toast.error('Failed to join group') toast.error('Failed to join group')
}) })
} }
const onLeaveGroup = () => { const onLeaveGroup = () => {
leaveGroup(group, currentUser.id).catch(() => { leaveGroup(group, user.id).catch(() => {
toast.error('Failed to leave group') toast.error('Failed to leave group')
}) })
} }

View File

@ -331,6 +331,7 @@ function GroupOverview(props: {
const shareUrl = `https://${ENV_CONFIG.domain}${groupPath( const shareUrl = `https://${ENV_CONFIG.domain}${groupPath(
group.slug group.slug
)}${postFix}` )}${postFix}`
const isMember = user ? members.map((m) => m.id).includes(user.id) : false
return ( return (
<> <>
@ -349,7 +350,11 @@ function GroupOverview(props: {
) : ( ) : (
user && ( user && (
<Row> <Row>
<JoinOrLeaveGroupButton group={group} /> <JoinOrLeaveGroupButton
group={group}
user={user}
isMember={isMember}
/>
</Row> </Row>
) )
)} )}

View File

@ -134,6 +134,8 @@ export default function Groups(props: {
key={group.id} key={group.id}
group={group} group={group}
creator={creatorsDict[group.creatorId]} creator={creatorsDict[group.creatorId]}
user={user}
isMember={memberGroupIds.includes(group.id)}
/> />
))} ))}
</div> </div>
@ -159,6 +161,8 @@ export default function Groups(props: {
key={group.id} key={group.id}
group={group} group={group}
creator={creatorsDict[group.creatorId]} creator={creatorsDict[group.creatorId]}
user={user}
isMember={memberGroupIds.includes(group.id)}
/> />
))} ))}
</div> </div>
@ -173,8 +177,13 @@ export default function Groups(props: {
) )
} }
export function GroupCard(props: { group: Group; creator: User | undefined }) { export function GroupCard(props: {
const { group, creator } = props group: Group
creator: User | undefined
user: User | undefined | null
isMember: boolean
}) {
const { group, creator, user, isMember } = props
const { totalContracts } = group const { totalContracts } = group
return ( return (
<Col className="relative min-w-[20rem] max-w-xs gap-1 rounded-xl bg-white p-8 shadow-md hover:bg-gray-100"> <Col className="relative min-w-[20rem] max-w-xs gap-1 rounded-xl bg-white p-8 shadow-md hover:bg-gray-100">
@ -201,7 +210,12 @@ export function GroupCard(props: { group: Group; creator: User | undefined }) {
<div className="text-sm text-gray-500">{group.about}</div> <div className="text-sm text-gray-500">{group.about}</div>
</Row> </Row>
<Col className={'mt-2 h-full items-start justify-end'}> <Col className={'mt-2 h-full items-start justify-end'}>
<JoinOrLeaveGroupButton group={group} className={'z-10 w-24'} /> <JoinOrLeaveGroupButton
group={group}
className={'z-10 w-24'}
user={user}
isMember={isMember}
/>
</Col> </Col>
</Col> </Col>
) )