11import {
2+ AlertDialog ,
3+ AlertDialogBody ,
4+ AlertDialogContent ,
5+ AlertDialogFooter ,
6+ AlertDialogHeader ,
7+ AlertDialogOverlay ,
28 Box ,
39 Button ,
410 Divider ,
@@ -8,6 +14,7 @@ import {
814 RadioGroup ,
915 Text ,
1016 Textarea ,
17+ useDisclosure ,
1118 VStack ,
1219} from '@chakra-ui/react' ;
1320import '@mdxeditor/editor/style.css' ;
@@ -18,9 +25,77 @@ import { ArticleWithContent } from './types';
1825import { getRequestObject , isValidArticle } from './utils' ;
1926import {
2027 useAddArticle ,
28+ useDeleteArticle ,
2129 useGetEditorArticleData ,
2230 useUpdateArticle ,
2331} from '@services' ;
32+ import React from 'react' ;
33+ import { isEmpty } from 'lodash' ;
34+
35+ const DeleteArticleButton = ( {
36+ articleKey,
37+ onDelete,
38+ } : {
39+ articleKey : string ;
40+ onDelete : ( ) => void ;
41+ } ) => {
42+ const { mutate } = useDeleteArticle ( ) ;
43+ const { isOpen, onOpen, onClose } = useDisclosure ( ) ;
44+ const cancelRef = React . useRef < HTMLButtonElement > ( null ) ;
45+ const [ articleKeyValidation , setArticleKeyValidation ] = useState < string > ( '' ) ;
46+
47+ return (
48+ < >
49+ < Button
50+ colorScheme = "red"
51+ onClick = { onOpen }
52+ isDisabled = { isEmpty ( articleKey ) }
53+ >
54+ Delete Article
55+ </ Button >
56+ < AlertDialog
57+ isOpen = { isOpen }
58+ leastDestructiveRef = { cancelRef }
59+ onClose = { onClose }
60+ >
61+ < AlertDialogOverlay >
62+ < AlertDialogContent >
63+ < AlertDialogHeader fontSize = "lg" fontWeight = "bold" >
64+ Delete Customer
65+ </ AlertDialogHeader >
66+
67+ < AlertDialogBody >
68+ { `Are you sure of deleting the article? You can't undo this action afterwards.` }
69+ < Input
70+ mt = { 3 }
71+ placeholder = { `Type ${ articleKey } to confirm` }
72+ value = { articleKeyValidation }
73+ onChange = { ( e ) => setArticleKeyValidation ( e . target . value ) }
74+ />
75+ </ AlertDialogBody >
76+ < AlertDialogFooter >
77+ < Button ref = { cancelRef } onClick = { onClose } >
78+ Cancel
79+ </ Button >
80+ < Button
81+ colorScheme = "red"
82+ onClick = { ( ) => {
83+ mutate ( { articleKey } ) ;
84+ onClose ( ) ;
85+ onDelete ( ) ;
86+ } }
87+ isDisabled = { articleKeyValidation !== articleKey }
88+ ml = { 3 }
89+ >
90+ Delete
91+ </ Button >
92+ </ AlertDialogFooter >
93+ </ AlertDialogContent >
94+ </ AlertDialogOverlay >
95+ </ AlertDialog >
96+ </ >
97+ ) ;
98+ } ;
2499
25100const ArticleEditor = ( ) => {
26101 const { setCursorType } = useCursor ( ) ;
@@ -191,6 +266,10 @@ const ArticleEditor = () => {
191266 Preload
192267 </ Button >
193268 </ HStack >
269+ < DeleteArticleButton
270+ articleKey = { state . article_key }
271+ onDelete = { ( ) => setState ( ARTICLE_DEFAULT ) }
272+ />
194273 < ArticleCard { ...state } />
195274 </ VStack >
196275 </ HStack >
0 commit comments