Skip to content

Commit

Permalink
Small refactor: renaming of functions
Browse files Browse the repository at this point in the history
  • Loading branch information
barbarah committed Oct 11, 2023
1 parent ed6683f commit af9cadb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@ import {InsertObjectItem} from '@colonial-collections/database';
import {revalidatePath} from 'next/cache';

export async function getCommunityLists(communityId: string, objectId: string) {
return objectList.getByCommunityId(communityId, {includeObjectIri: objectId});
return objectList.getByCommunityId(communityId, {objectIri: objectId});
}

interface AddObjectToListProps {
listItem: InsertObjectItem;
objectItem: InsertObjectItem;
communityId: string;
}

export async function addObjectToList({
listItem,
objectItem,
communityId,
}: AddObjectToListProps) {
await objectList.addObject(listItem);
const community = await getCommunityBySlug(communityId);
const addObjectPromise = objectList.addObject(objectItem);
const getCommunityPromise = getCommunityBySlug(communityId);
const [community] = await Promise.all([
getCommunityPromise,
addObjectPromise,
]);

revalidatePath(`/[locale]/communities/${community.slug}`, 'page');
}

Expand Down
42 changes: 22 additions & 20 deletions apps/researcher/src/app/[locale]/objects/[id]/object-lists-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,48 +30,48 @@ function CommunityMenuItems({
const {addNotification} = useNotifications();

async function listClick(objectList: SelectObjectList) {
const objectInList = !!objectList.objects!.length;
const isEmptyList = !objectList.objects!.length;

if (objectInList) {
if (isEmptyList) {
try {
await removeObjectFromList(objectList.objects![0].id, communityId);
await addObjectToList({
objectItem: {
objectIri: objectId,
objectListId: objectList.id,
createdBy: userId,
},
communityId,
});

addNotification({
id: 'objectRemovedFromList',
message: t.rich('objectRemovedFromList', {
id: 'objectAddedToList',
message: t.rich('objectAddedToList', {
name: () => <em>{objectList.name}</em>,
}),
type: 'success',
});
} catch (err) {
addNotification({
id: 'errorObjectRemovedFromList',
message: t('errorObjectRemovedFromList'),
id: 'errorObjectAddedToList',
message: t('errorObjectAddedToList'),
type: 'error',
});
}
} else {
try {
await addObjectToList({
listItem: {
objectIri: objectId,
objectListId: objectList.id,
createdBy: userId,
},
communityId,
});
await removeObjectFromList(objectList.objects![0].id, communityId);

addNotification({
id: 'objectAddedToList',
message: t.rich('objectAddedToList', {
id: 'objectRemovedFromList',
message: t.rich('objectRemovedFromList', {
name: () => <em>{objectList.name}</em>,
}),
type: 'success',
});
} catch (err) {
addNotification({
id: 'errorObjectAddedToList',
message: t('errorObjectAddedToList'),
id: 'errorObjectRemovedFromList',
message: t('errorObjectRemovedFromList'),
type: 'error',
});
}
Expand Down Expand Up @@ -127,7 +127,9 @@ export default function ObjectListsMenu({objectId}: ObjectListsMenuProps) {
return null;
}

const communities = user.organizationMemberships.map(m => m.organization);
const communities = user.organizationMemberships.map(
membership => membership.organization
);

return (
<Menu as="div" className="relative inline-block text-left">
Expand Down
8 changes: 4 additions & 4 deletions packages/database/src/object-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {SelectObjectList} from './db/types';
interface Option {
withObjects?: boolean;
limitObjects?: number;
includeObjectIri?: string;
objectIri?: string;
}

export async function getByCommunityId(
communityId: string,
{withObjects, limitObjects, includeObjectIri}: Option = {withObjects: false}
{withObjects, limitObjects, objectIri}: Option = {withObjects: false}
// Explicitly set the return type, or else `objects` will not be included.
): Promise<SelectObjectList[]> {
const options: DBQueryConfig = {};
Expand All @@ -25,8 +25,8 @@ export async function getByCommunityId(
};
}

if (includeObjectIri) {
const objectId = iriToHash(includeObjectIri);
if (objectIri) {
const objectId = iriToHash(objectIri);
options.with = {
objects: {
where: (objectItems, {eq}) => eq(objectItems.objectId, objectId),
Expand Down

0 comments on commit af9cadb

Please sign in to comment.