Skip to content

Commit

Permalink
DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
minwoox committed Nov 7, 2024
1 parent f0fa46b commit fb2d8cd
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
Button,
HStack,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
} from '@chakra-ui/react';

interface DeleteConfirmationModalProps {
isOpen: boolean;
onClose: () => void;
type: string;
id: string;
projectName: string;
handleDelete: () => void;
isLoading: boolean;
}

export const DeleteConfirmationModal = ({
isOpen,
onClose,
id,
type,
projectName,
handleDelete,
isLoading,
}: DeleteConfirmationModalProps): JSX.Element => {
return (
<Modal isOpen={isOpen} onClose={onClose}>
<ModalOverlay />
<ModalContent>
<ModalHeader>Are you sure?</ModalHeader>
<ModalCloseButton />
<ModalBody>
Delete {type} &apos;{id}&apos; from {projectName}?
</ModalBody>
<ModalFooter>
<HStack spacing={3}>
<Button colorScheme="red" variant="outline" onClick={onClose}>
Cancel
</Button>
<Button colorScheme="red" onClick={handleDelete} isLoading={isLoading} loadingText="Deleting">
Delete
</Button>
</HStack>
</ModalFooter>
</ModalContent>
</Modal>
);
};
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import {
Button,
HStack,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
useDisclosure,
} from '@chakra-ui/react';
import { Button, useDisclosure } from '@chakra-ui/react';
import { newNotification } from 'dogma/features/notification/notificationSlice';
import ErrorMessageParser from 'dogma/features/services/ErrorMessageParser';
import { useAppDispatch } from 'dogma/hooks';
import { MdDelete } from 'react-icons/md';
import { DeleteConfirmationModal } from 'dogma/features/project/settings/DeleteConfirmationModal';

export const DeleteCredential = ({
projectName,
Expand Down Expand Up @@ -42,26 +32,15 @@ export const DeleteCredential = ({
<Button leftIcon={<MdDelete />} colorScheme="red" size="sm" variant="ghost" onClick={onToggle}>
Delete
</Button>
<Modal isOpen={isOpen} onClose={onClose}>
<ModalOverlay />
<ModalContent>
<ModalHeader>Are you sure?</ModalHeader>
<ModalCloseButton />
<ModalBody>
Delete credential &apos{id}&apos from {projectName}?
</ModalBody>
<ModalFooter>
<HStack spacing={3}>
<Button colorScheme="red" variant="outline" onClick={onClose}>
Cancel
</Button>
<Button colorScheme="red" onClick={handleDelete} isLoading={isLoading} loadingText="Deleting">
Delete
</Button>
</HStack>
</ModalFooter>
</ModalContent>
</Modal>
<DeleteConfirmationModal
isOpen={isOpen}
onClose={onClose}
id={id}
type={'credential'}
projectName={projectName}
handleDelete={handleDelete}
isLoading={isLoading}
/>
</>
);
};
43 changes: 11 additions & 32 deletions webapp/src/dogma/features/project/settings/members/DeleteMember.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import {
Button,
HStack,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
useDisclosure,
} from '@chakra-ui/react';
import { Button, useDisclosure } from '@chakra-ui/react';
import { newNotification } from 'dogma/features/notification/notificationSlice';
import ErrorMessageParser from 'dogma/features/services/ErrorMessageParser';
import { useAppDispatch } from 'dogma/hooks';
import { MdDelete } from 'react-icons/md';
import { DeleteConfirmationModal } from '../DeleteConfirmationModal';

export const DeleteMember = ({
projectName,
Expand Down Expand Up @@ -44,26 +34,15 @@ export const DeleteMember = ({
<Button leftIcon={<MdDelete />} colorScheme="red" size="sm" variant="ghost" onClick={onToggle}>
Delete
</Button>
<Modal isOpen={isOpen} onClose={onClose}>
<ModalOverlay />
<ModalContent>
<ModalHeader>Are you sure?</ModalHeader>
<ModalCloseButton />
<ModalBody>
Delete {id} from {projectName}?
</ModalBody>
<ModalFooter>
<HStack spacing={3}>
<Button colorScheme="red" variant="outline" onClick={onClose}>
Cancel
</Button>
<Button colorScheme="red" onClick={handleDelete} isLoading={isLoading} loadingText="Deleting">
Delete
</Button>
</HStack>
</ModalFooter>
</ModalContent>
</Modal>
<DeleteConfirmationModal
isOpen={isOpen}
onClose={onClose}
id={id}
type={'member'}
projectName={projectName}
handleDelete={handleDelete}
isLoading={isLoading}
/>
</>
);
};
43 changes: 11 additions & 32 deletions webapp/src/dogma/features/project/settings/mirrors/DeleteMirror.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import {
Button,
HStack,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalFooter,
ModalHeader,
ModalOverlay,
useDisclosure,
} from '@chakra-ui/react';
import { Button, useDisclosure } from '@chakra-ui/react';
import { DeleteConfirmationModal } from 'dogma/features/project/settings/DeleteConfirmationModal';
import { newNotification } from 'dogma/features/notification/notificationSlice';
import ErrorMessageParser from 'dogma/features/services/ErrorMessageParser';
import { useAppDispatch } from 'dogma/hooks';
Expand Down Expand Up @@ -42,26 +32,15 @@ export const DeleteMirror = ({
<Button leftIcon={<MdDelete />} colorScheme="red" size="sm" variant="ghost" onClick={onToggle}>
Delete
</Button>
<Modal isOpen={isOpen} onClose={onClose}>
<ModalOverlay />
<ModalContent>
<ModalHeader>Are you sure?</ModalHeader>
<ModalCloseButton />
<ModalBody>
Delete mirror &apos{id}&apos from {projectName}?
</ModalBody>
<ModalFooter>
<HStack spacing={3}>
<Button colorScheme="red" variant="outline" onClick={onClose}>
Cancel
</Button>
<Button colorScheme="red" onClick={handleDelete} isLoading={isLoading} loadingText="Deleting">
Delete
</Button>
</HStack>
</ModalFooter>
</ModalContent>
</Modal>
<DeleteConfirmationModal
isOpen={isOpen}
onClose={onClose}
id={id}
type={'mirror'}
projectName={projectName}
handleDelete={handleDelete}
isLoading={isLoading}
/>
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import { DataTableClientPagination } from 'dogma/common/components/table/DataTab
import { useMemo } from 'react';
import { useDeleteTokenMemberMutation } from 'dogma/features/api/apiSlice';
import { AppTokenDetailDto } from 'dogma/features/project/settings/tokens/AppTokenDto';
import { DeleteMember } from 'dogma/features/project/settings/members/DeleteMember';
import { DeleteToken } from 'dogma/features/project/settings/tokens/DeleteToken';

export type AppTokenListProps<Data extends object> = {
data: Data[];
projectName: string;
};

const AppTokenList = <Data extends object>({ data, projectName }: AppTokenListProps<Data>) => {
const [deleteMember, { isLoading }] = useDeleteTokenMemberMutation();
const [deleteToken, { isLoading }] = useDeleteTokenMemberMutation();
const columnHelper = createColumnHelper<AppTokenDetailDto>();
const columns = useMemo(
() => [
Expand All @@ -39,18 +39,18 @@ const AppTokenList = <Data extends object>({ data, projectName }: AppTokenListPr
}),
columnHelper.accessor((row: AppTokenDetailDto) => row.appId, {
cell: (info) => (
<DeleteMember
<DeleteToken
projectName={projectName}
id={info.getValue()}
deleteMember={(projectName, id) => deleteMember({ projectName, id }).unwrap()}
deleteToken={(projectName, id) => deleteToken({ projectName, id }).unwrap()}
isLoading={isLoading}
/>
),
header: 'Actions',
enableSorting: false,
}),
],
[columnHelper, deleteMember, isLoading, projectName],
[columnHelper, deleteToken, isLoading, projectName],
);
return <DataTableClientPagination columns={columns as ColumnDef<Data>[]} data={data} />;
};
Expand Down
48 changes: 48 additions & 0 deletions webapp/src/dogma/features/project/settings/tokens/DeleteToken.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Button, useDisclosure } from '@chakra-ui/react';
import { newNotification } from 'dogma/features/notification/notificationSlice';
import ErrorMessageParser from 'dogma/features/services/ErrorMessageParser';
import { useAppDispatch } from 'dogma/hooks';
import { MdDelete } from 'react-icons/md';
import { DeleteConfirmationModal } from '../DeleteConfirmationModal';

export const DeleteToken = ({
projectName,
repoName,
id,
deleteToken,
isLoading,
}: {
projectName: string;
repoName?: string;
id: string;
deleteToken: (projectName: string, id: string, repoName?: string) => Promise<void>;
isLoading: boolean;
}): JSX.Element => {
const { isOpen, onToggle, onClose } = useDisclosure();
const dispatch = useAppDispatch();
const handleDelete = async () => {
try {
await deleteToken(projectName, id, repoName);
dispatch(newNotification('Token deleted.', `Successfully deleted ${id}`, 'success'));
onClose();
} catch (error) {
dispatch(newNotification(`Failed to delete ${id}`, ErrorMessageParser.parse(error), 'error'));
}
};
return (
<>
<Button leftIcon={<MdDelete />} colorScheme="red" size="sm" variant="ghost" onClick={onToggle}>
Delete
</Button>
<DeleteConfirmationModal
isOpen={isOpen}
onClose={onClose}
id={id}
type={'token'}
projectName={projectName}
handleDelete={handleDelete}
isLoading={isLoading}
/>
</>
);
};

0 comments on commit fb2d8cd

Please sign in to comment.