Skip to content

Commit

Permalink
451 display option users at comments page (#452)
Browse files Browse the repository at this point in the history
* Create fetchOptionUsers and type

* Add query to comments page

* Comment author + co authors & fetchOptionUsers
  • Loading branch information
camilovegag authored May 14, 2024
1 parent cb337b0 commit 3589638
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
27 changes: 27 additions & 0 deletions packages/api/src/fetchOptionUsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { GetOptionUsersResponse } from './types/OptionUsersType';

async function fetchOptionUsers(optionId: string): Promise<GetOptionUsersResponse | null> {
try {
const response = await fetch(
`${import.meta.env.VITE_SERVER_URL}/api/options/${optionId}/users`,
{
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
},
);

if (!response.ok) {
throw new Error(`HTTP Error! Status: ${response.status}`);
}

const alerts = (await response.json()) as { data: GetOptionUsersResponse };
return alerts.data;
} catch (error) {
console.error('Error fetching users:', error);
return null;
}
}

export default fetchOptionUsers;
5 changes: 3 additions & 2 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ export { default as fetchGroupCategories } from './fetchGroupCategories';
export { default as fetchGroups } from './fetchGroups';
export { default as fetchGroupMembers } from './fetchGroupMembers';
export { default as fetchOption } from './fetchOption';
export { default as fetchOptionUsers } from './fetchOptionUsers';
export { default as fetchRegistrationData } from './fetchRegistrationData';
export { default as fetchRegistrationFields } from './fetchRegistrationFields';
export { default as fetchRegistrations } from './fetchRegistrations';
export { default as fetchUserAttributes } from './fetchUserAttributes';
export { default as fetchUser } from './fetchUser';
export { default as fetchUsersToGroups } from './fetchUsersToGroups';
export { default as fetchUserAttributes } from './fetchUserAttributes';
export { default as fetchUserRegistrations } from './fetchUserRegistrations';
export { default as fetchUsersToGroups } from './fetchUsersToGroups';
export { default as fetchUserVotes } from './fetchUserVotes';
export { default as logout } from './logout';
export { default as postComment } from './postComment';
Expand Down
11 changes: 11 additions & 0 deletions packages/api/src/types/OptionUsersType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { GetGroupsResponse } from './GroupType';
import { GetUserResponse } from './UserType';

export type GetOptionUsersResponse = {
optionId: string;
registrationId: string | null;
userId: string | null;
user: GetUserResponse | null;
groupId: string | null;
group: GetGroupsResponse | null;
};
22 changes: 18 additions & 4 deletions packages/berlin/src/pages/Comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import { useEffect, useMemo, useState } from 'react';
import toast from 'react-hot-toast';

// API
import { fetchOption, postVotes, fetchUserVotes, fetchComments, postComment } from 'api';
import {
fetchOption,
postVotes,
fetchUserVotes,
fetchComments,
postComment,
// fetchOptionUsers,
} from 'api';

// Hooks
import useUser from '../hooks/useUser';
Expand Down Expand Up @@ -58,6 +65,13 @@ function Comments() {
enabled: !!user?.id && !!cycleId,
retry: false,
});

// const { data: optionUsers } = useQuery({
// queryKey: ['optionUsers', optionId],
// queryFn: () => fetchOptionUsers(optionId || ''),
// enabled: !!optionId,
// });

const { data: comments } = useQuery({
queryKey: ['comments', optionId],
queryFn: () => fetchComments({ optionId: optionId || '' }),
Expand Down Expand Up @@ -169,12 +183,12 @@ function Comments() {
</FlexRow>
<Subtitle>{option?.optionTitle}</Subtitle>
<Body>{option?.optionSubTitle}</Body>
<Body>
<Bold>Lead author:</Bold> [// TODO]
{/* <Body>
<Bold>Lead author: {option?.user}</Bold> [// TODO]
</Body>
<Body>
<Bold>Co-authors:</Bold> [// TODO]
</Body>
</Body> */}
</FlexColumn>

<Button onClick={handleSaveVotesWrapper} disabled={!votesAreDifferent}>
Expand Down

0 comments on commit 3589638

Please sign in to comment.