Don't allow modification of private groups contracts
This commit is contained in:
parent
81941f1f1f
commit
87aeeaf0aa
|
@ -7,13 +7,13 @@ import { Button } from 'web/components/button'
|
||||||
import { GroupSelector } from 'web/components/groups/group-selector'
|
import { GroupSelector } from 'web/components/groups/group-selector'
|
||||||
import {
|
import {
|
||||||
addContractToGroup,
|
addContractToGroup,
|
||||||
canModifyGroupContracts,
|
|
||||||
removeContractFromGroup,
|
removeContractFromGroup,
|
||||||
} from 'web/lib/firebase/groups'
|
} from 'web/lib/firebase/groups'
|
||||||
import { User } from 'common/user'
|
import { User } from 'common/user'
|
||||||
import { Contract } from 'common/contract'
|
import { Contract } from 'common/contract'
|
||||||
import { SiteLink } from 'web/components/site-link'
|
import { SiteLink } from 'web/components/site-link'
|
||||||
import { useGroupsWithContract } from 'web/hooks/use-group'
|
import { useGroupsWithContract, useMemberGroupIds } from 'web/hooks/use-group'
|
||||||
|
import { Group } from 'common/group'
|
||||||
|
|
||||||
export function ContractGroupsList(props: {
|
export function ContractGroupsList(props: {
|
||||||
contract: Contract
|
contract: Contract
|
||||||
|
@ -22,6 +22,15 @@ export function ContractGroupsList(props: {
|
||||||
const { user, contract } = props
|
const { user, contract } = props
|
||||||
const { groupLinks } = contract
|
const { groupLinks } = contract
|
||||||
const groups = useGroupsWithContract(contract)
|
const groups = useGroupsWithContract(contract)
|
||||||
|
const memberGroupIds = useMemberGroupIds(user)
|
||||||
|
|
||||||
|
const canModifyGroupContracts = (group: Group, userId: string) => {
|
||||||
|
return (
|
||||||
|
group.creatorId === userId ||
|
||||||
|
group.anyoneCanJoin ||
|
||||||
|
memberGroupIds?.includes(group.id)
|
||||||
|
)
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<Col className={'gap-2'}>
|
<Col className={'gap-2'}>
|
||||||
<span className={'text-xl text-indigo-700'}>
|
<span className={'text-xl text-indigo-700'}>
|
||||||
|
@ -61,7 +70,7 @@ export function ContractGroupsList(props: {
|
||||||
<Button
|
<Button
|
||||||
color={'gray-white'}
|
color={'gray-white'}
|
||||||
size={'xs'}
|
size={'xs'}
|
||||||
onClick={() => removeContractFromGroup(group, contract, user.id)}
|
onClick={() => removeContractFromGroup(group, contract)}
|
||||||
>
|
>
|
||||||
<XIcon className="h-4 w-4 text-gray-500" />
|
<XIcon className="h-4 w-4 text-gray-500" />
|
||||||
</Button>
|
</Button>
|
||||||
|
|
|
@ -166,7 +166,6 @@ export async function addContractToGroup(
|
||||||
contract: Contract,
|
contract: Contract,
|
||||||
userId: string
|
userId: string
|
||||||
) {
|
) {
|
||||||
if (!canModifyGroupContracts(group, userId)) return
|
|
||||||
const newGroupLinks = [
|
const newGroupLinks = [
|
||||||
...(contract.groupLinks ?? []),
|
...(contract.groupLinks ?? []),
|
||||||
{
|
{
|
||||||
|
@ -193,11 +192,8 @@ export async function addContractToGroup(
|
||||||
|
|
||||||
export async function removeContractFromGroup(
|
export async function removeContractFromGroup(
|
||||||
group: Group,
|
group: Group,
|
||||||
contract: Contract,
|
contract: Contract
|
||||||
userId: string
|
|
||||||
) {
|
) {
|
||||||
if (!canModifyGroupContracts(group, userId)) return
|
|
||||||
|
|
||||||
if (contract.groupLinks?.map((l) => l.groupId).includes(group.id)) {
|
if (contract.groupLinks?.map((l) => l.groupId).includes(group.id)) {
|
||||||
const newGroupLinks = contract.groupLinks?.filter(
|
const newGroupLinks = contract.groupLinks?.filter(
|
||||||
(link) => link.slug !== group.slug
|
(link) => link.slug !== group.slug
|
||||||
|
@ -214,15 +210,6 @@ export async function removeContractFromGroup(
|
||||||
await deleteDoc(contractDoc)
|
await deleteDoc(contractDoc)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function canModifyGroupContracts(group: Group, userId: string) {
|
|
||||||
return (
|
|
||||||
group.creatorId === userId ||
|
|
||||||
// TODO: check if member document exists
|
|
||||||
// group.memberIds.includes(userId) ||
|
|
||||||
group.anyoneCanJoin
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getGroupLinkToDisplay(contract: Contract) {
|
export function getGroupLinkToDisplay(contract: Contract) {
|
||||||
const { groupLinks } = contract
|
const { groupLinks } = contract
|
||||||
const sortedGroupLinks = groupLinks?.sort(
|
const sortedGroupLinks = groupLinks?.sort(
|
||||||
|
|
|
@ -20,7 +20,7 @@ import {
|
||||||
import { formatMoney } from 'common/util/format'
|
import { formatMoney } from 'common/util/format'
|
||||||
import { removeUndefinedProps } from 'common/util/object'
|
import { removeUndefinedProps } from 'common/util/object'
|
||||||
import { ChoicesToggleGroup } from 'web/components/choices-toggle-group'
|
import { ChoicesToggleGroup } from 'web/components/choices-toggle-group'
|
||||||
import { canModifyGroupContracts, getGroup } from 'web/lib/firebase/groups'
|
import { getGroup } from 'web/lib/firebase/groups'
|
||||||
import { Group } from 'common/group'
|
import { Group } from 'common/group'
|
||||||
import { useTracking } from 'web/hooks/use-tracking'
|
import { useTracking } from 'web/hooks/use-tracking'
|
||||||
import { useWarnUnsavedChanges } from 'web/hooks/use-warn-unsaved-changes'
|
import { useWarnUnsavedChanges } from 'web/hooks/use-warn-unsaved-changes'
|
||||||
|
@ -139,7 +139,7 @@ export function NewContract(props: {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (groupId)
|
if (groupId)
|
||||||
getGroup(groupId).then((group) => {
|
getGroup(groupId).then((group) => {
|
||||||
if (group && canModifyGroupContracts(group, creator.id)) {
|
if (group) {
|
||||||
setSelectedGroup(group)
|
setSelectedGroup(group)
|
||||||
setShowGroupSelector(false)
|
setShowGroupSelector(false)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user