Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

III-6393 - Display creator email #953

Merged
merged 6 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/hooks/api/ownerships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export type OwnershipRequest = {
state: RequestState;
};

export type OwnershipCreator = {
userId: string;
email: string;
};

export const RequestState = {
APPROVED: 'approved',
REQUESTED: 'requested',
Expand Down Expand Up @@ -108,9 +113,42 @@ const useDeleteOwnershipRequestMutation = (configuration = {}) =>
...configuration,
});

const getOwnershipCreator = async ({ headers, organizerId }) => {
const res = await fetchFromApi({
path: `/organizers/${organizerId}/creator`,
options: {
headers,
},
});
if (isErrorObject(res)) {
// eslint-disable-next-line no-console
return console.error(res);
}
return await res.json();
};

type UseGetOwnershipCreatorArguments = ServerSideQueryOptions & {
organizerId: string;
};

const useGetOwnershipCreatorQuery = (
{ req, queryClient, organizerId }: UseGetOwnershipCreatorArguments,
configuration: UseQueryOptions = {},
) =>
useAuthenticatedQuery<OwnershipCreator>({
req,
queryClient,
queryKey: ['ownership-creator'],
queryFn: getOwnershipCreator,
queryArguments: { organizerId },
refetchOnWindowFocus: false,
...configuration,
});

export {
useApproveOwnershipRequestMutation,
useDeleteOwnershipRequestMutation,
useGetOwnershipCreatorQuery,
useGetOwnershipRequestsQuery,
useRejectOwnershipRequestMutation,
};
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { useTranslation } from 'react-i18next';

import { OwnershipRequest } from '@/hooks/api/ownerships';
import { OwnershipCreator, OwnershipRequest } from '@/hooks/api/ownerships';
import { Inline } from '@/ui/Inline';
import { List } from '@/ui/List';
import { Stack } from '@/ui/Stack';
import { colors, getValueFromTheme } from '@/ui/theme';
import { Title } from '@/ui/Title';

type Props = {
creator?: OwnershipCreator;
requests: OwnershipRequest[];
renderActions: (request: OwnershipRequest) => JSX.Element;
};

const getGlobalValue = getValueFromTheme('global');

export const OwnershipsTable = ({ requests, renderActions }: Props) => {
export const OwnershipsTable = ({
creator,
requests,
renderActions,
}: Props) => {
const { grey3 } = colors;
const { t } = useTranslation();
return (
Expand All @@ -38,13 +43,13 @@ export const OwnershipsTable = ({ requests, renderActions }: Props) => {
<Title size={3}>{t('organizers.ownerships.table.user')}</Title>
<Title size={3}>{t('organizers.ownerships.table.actions.title')}</Title>
</Inline>
<List>
<List paddingY={3}>
<List.Item>{creator.email}</List.Item>
{requests.map((request) => (
<Inline
key={request.id}
justifyContent="space-between"
alignItems="center"
paddingY={3}
css={`
&:not(:last-child) {
border-bottom: 1px solid ${grey3};
Expand Down
88 changes: 52 additions & 36 deletions src/pages/organizers/[organizerId]/ownerships/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
RequestState,
useApproveOwnershipRequestMutation,
useDeleteOwnershipRequestMutation,
useGetOwnershipCreatorQuery,
useGetOwnershipRequestsQuery,
useRejectOwnershipRequestMutation,
} from '@/hooks/api/ownerships';
Expand Down Expand Up @@ -65,6 +66,10 @@ const Ownership = () => {
organizerId: organizerId,
});

const getOwnershipCreatorQuery = useGetOwnershipCreatorQuery({
organizerId: organizerId,
});

const requestsByState: { [key: string]: OwnershipRequest[] } = useMemo(
() =>
groupBy(
Expand All @@ -76,6 +81,9 @@ const Ownership = () => {
[getOwnershipRequestsQuery.data],
);

// @ts-expect-error
const creator = getOwnershipCreatorQuery.data;
Comment on lines +88 to +89
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// @ts-expect-error
const creator = getOwnershipCreatorQuery.data;
const creator: OwnershipCreator | undefined = getOwnershipCreatorQuery.data;

I think you can manually add the type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that the type does not include a data property.


const approvedRequests = requestsByState[RequestState.APPROVED] ?? [];
const pendingRequests = requestsByState[RequestState.REQUESTED] ?? [];

Expand Down Expand Up @@ -147,42 +155,45 @@ const Ownership = () => {
/>
</Alert>
)}
<Stack spacing={4}>
<Title size={3}>{t('organizers.ownerships.owners')}</Title>
<OwnershipsTable
requests={approvedRequests}
renderActions={(request) => (
<Button
variant={ButtonVariants.ICON}
iconName={Icons.TRASH}
onClick={() => setRequestToBeDeleted(request)}
/>
)}
/>
<Modal
title={t('organizers.ownerships.delete_modal.title')}
confirmTitle={t('organizers.ownerships.delete_modal.confirm')}
cancelTitle={t('organizers.ownerships.delete_modal.cancel')}
visible={!!requestToBeDeleted}
variant={ModalVariants.QUESTION}
onConfirm={() =>
deleteOwnershipRequestMutation.mutate({
ownershipId: requestToBeDeleted.id,
})
}
onClose={() => setRequestToBeDeleted(undefined)}
size={ModalSizes.MD}
>
<Box padding={4}>
<Trans
i18nKey="organizers.ownerships.delete_modal.body"
values={{
ownerEmail: requestToBeDeleted?.ownerEmail,
}}
/>
</Box>
</Modal>
</Stack>
{(approvedRequests.length > 0 || !!creator) && (
<Stack spacing={4}>
<Title size={3}>{t('organizers.ownerships.owners')}</Title>
<OwnershipsTable
creator={creator}
requests={approvedRequests}
renderActions={(request) => (
<Button
variant={ButtonVariants.ICON}
iconName={Icons.TRASH}
onClick={() => setRequestToBeDeleted(request)}
/>
)}
/>
<Modal
title={t('organizers.ownerships.delete_modal.title')}
confirmTitle={t('organizers.ownerships.delete_modal.confirm')}
cancelTitle={t('organizers.ownerships.delete_modal.cancel')}
visible={!!requestToBeDeleted}
variant={ModalVariants.QUESTION}
onConfirm={() =>
deleteOwnershipRequestMutation.mutate({
ownershipId: requestToBeDeleted.id,
})
}
onClose={() => setRequestToBeDeleted(undefined)}
size={ModalSizes.MD}
>
<Box padding={4}>
<Trans
i18nKey="organizers.ownerships.delete_modal.body"
values={{
ownerEmail: requestToBeDeleted?.ownerEmail,
}}
/>
</Box>
</Modal>
</Stack>
)}
{pendingRequests.length > 0 && (
<Stack spacing={4}>
<Title size={3}>{t('organizers.ownerships.pending')}</Title>
Expand Down Expand Up @@ -272,6 +283,11 @@ export const getServerSideProps = getApplicationServerSideProps(
queryClient,
organizerId: query.organizerId,
}),
await useGetOwnershipCreatorQuery({
req,
queryClient,
organizerId: query.organizerId,
}),
]);
return {
props: {
Expand Down
Loading