Skip to content

Commit

Permalink
Added logic to update group with topic level permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
mzparacha committed Oct 10, 2024
1 parent edf786a commit de7d332
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
22 changes: 21 additions & 1 deletion libs/model/src/community/UpdateGroup.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function UpdateGroup(): Command<

const topics = await models.Topic.findAll({
where: {
id: { [Op.in]: payload.topics || [] },
id: { [Op.in]: payload.topics?.map((t) => t.id) || [] },
community_id,
},
});
Expand Down Expand Up @@ -90,6 +90,26 @@ export function UpdateGroup(): Command<
transaction,
},
);

// update topic level interaction permissions for current group
await Promise.all(
(payload.topics || [])?.map(async (t) => {
if (group.id) {
await models.GroupTopicPermission.update(
{
allowed_actions: t.permission,
},
{
where: {
group_id: group_id,
topic_id: t.id,
},
transaction,
},
);
}
}),
);
}

return group.toJSON();
Expand Down
9 changes: 8 additions & 1 deletion libs/schemas/src/commands/community.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,14 @@ export const UpdateGroup = {
group_id: PG_INT,
metadata: GroupMetadata.optional(),
requirements: z.array(Requirement).optional(),
topics: z.array(PG_INT).optional(),
topics: z
.array(
z.object({
id: PG_INT,
permission: z.nativeEnum(GroupTopicPermissionEnum),
}),
)
.optional(),
}),
output: Group.partial(),
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { trpc } from 'client/scripts/utils/trpcClient';
import { trpc } from 'utils/trpcClient';
import { GroupFormTopicSubmitValues } from 'views/pages/CommunityGroupsAndMembers/Groups/common/GroupForm/index.types';
import { userStore } from '../../ui/user';
import { ApiEndpoints, queryClient } from '../config';

Expand All @@ -8,7 +9,7 @@ interface EditGroupProps {
address: string;
groupName: string;
groupDescription?: string;
topicIds: number[];
topics: GroupFormTopicSubmitValues[];
requirementsToFulfill: number | undefined;
requirements?: any[];
}
Expand All @@ -19,7 +20,7 @@ export const buildUpdateGroupInput = ({
address,
groupName,
groupDescription,
topicIds,
topics,
requirementsToFulfill,
requirements,
}: EditGroupProps) => {
Expand All @@ -37,7 +38,7 @@ export const buildUpdateGroupInput = ({
}),
},
...(requirements && { requirements }),
topics: topicIds,
topics,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ const UpdateCommunityGroupPage = ({ groupId }: { groupId: string }) => {
topics: (foundGroup.topics || []).map((topic) => ({
label: topic.name,
value: topic.id,
permission: topic.permission,
})),
}}
onSubmit={(values) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ const GroupForm = ({
`${initialValues.requirementsToFulfill}`,
);
}

if (initialValues.topics) {
setTopicPermissionsSubForms(
initialValues.topics.map((t) => ({
permission: TOPIC_PERMISSIONS[t.permission],
topic: { id: parseInt(`${t.value}`), name: t.label },
})),
);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down

0 comments on commit de7d332

Please sign in to comment.