22
33import React , { useState , useEffect } from 'react' ;
44import { useStatements } from '../../hooks/useStatements' ;
5- // import { Button } from '../../components/ui/button';
65import { ConfirmationDialog } from '../ui/confirmation-dialog' ;
76import type { Statement } from '../../../types/types' ;
87import preStatements from '../../../data/preStatements.json' ;
@@ -28,6 +27,11 @@ const StatementList: React.FC<{ username: string }> = ({ username }) => {
2827 statement : Statement | null ;
2928 } > ( { isOpen : false , statement : null } ) ;
3029
30+ const [ actionDeleteConfirmation , setActionDeleteConfirmation ] = useState < {
31+ isOpen : boolean ;
32+ actionId : string | null ;
33+ } > ( { isOpen : false , actionId : null } ) ;
34+
3135 // If there are no statements, set default ones from preStatements.json
3236 useEffect ( ( ) => {
3337 if ( statements . length === 0 ) {
@@ -129,7 +133,7 @@ const StatementList: React.FC<{ username: string }> = ({ username }) => {
129133 }
130134 } ;
131135
132- // NEW: handleAddAction callback for adding a new action to a statement.
136+ // HandleAddAction callback for adding a new action to a statement.
133137 const handleAddAction = (
134138 statementId : string ,
135139 newAction : { text : string ; dueDate : string }
@@ -157,7 +161,7 @@ const StatementList: React.FC<{ username: string }> = ({ username }) => {
157161 updateStatement ( updatedStatement ) ;
158162 } ;
159163
160- // New: Handler for editing an existing action.
164+ // Handler for editing an existing action.
161165 const handleEditAction = (
162166 actionId : string ,
163167 updated : { text : string ; dueDate : string }
@@ -181,6 +185,42 @@ const StatementList: React.FC<{ username: string }> = ({ username }) => {
181185 updateStatement ( updatedStatement ) ;
182186 } ;
183187
188+ // Handler for deleting an existing action.
189+
190+ const handleDeleteActionClick = ( actionId : string ) => {
191+ setActionDeleteConfirmation ( { isOpen : true , actionId } ) ;
192+ } ;
193+
194+ const handleDeleteActionConfirm = ( ) => {
195+ if ( actionDeleteConfirmation . actionId ) {
196+ const actionId = actionDeleteConfirmation . actionId ;
197+ // Find the statement that contains this action.
198+ const statementToUpdate = statements . find (
199+ ( s ) => s . actions && s . actions . some ( ( a ) => a . id === actionId )
200+ ) ;
201+ if ( statementToUpdate && statementToUpdate . actions ) {
202+ // Remove the action from the actions array.
203+ const updatedActions = statementToUpdate . actions . filter (
204+ ( a ) => a . id !== actionId
205+ ) ;
206+ const updatedStatement : Statement = {
207+ ...statementToUpdate ,
208+ actions : updatedActions ,
209+ } ;
210+
211+ // Update the context.
212+ dispatch ( { type : 'UPDATE_STATEMENT' , payload : updatedStatement } ) ;
213+ // Persist the change via the API.
214+ updateStatement ( updatedStatement ) ;
215+ }
216+ }
217+ setActionDeleteConfirmation ( { isOpen : false , actionId : null } ) ;
218+ } ;
219+
220+ const handleDeleteActionCancel = ( ) => {
221+ setActionDeleteConfirmation ( { isOpen : false , actionId : null } ) ;
222+ } ;
223+
184224 return (
185225 < div className = 'mt-8 bg-white rounded-xl shadow-lg p-6 w-full' >
186226 < h2 className = 'text-xl font-semibold mb-4' > Created Statements</ h2 >
@@ -201,6 +241,7 @@ const StatementList: React.FC<{ username: string }> = ({ username }) => {
201241 onEditClick = { handleEditClick }
202242 onAddAction = { handleAddAction }
203243 onEditAction = { handleEditAction }
244+ onDeleteAction = { handleDeleteActionClick }
204245 />
205246 </ li >
206247 ) ) }
@@ -224,6 +265,13 @@ const StatementList: React.FC<{ username: string }> = ({ username }) => {
224265 description = 'This statement already exists and will not be added.'
225266 singleButton
226267 />
268+ < ConfirmationDialog
269+ isOpen = { actionDeleteConfirmation . isOpen }
270+ onClose = { handleDeleteActionCancel }
271+ onConfirm = { handleDeleteActionConfirm }
272+ title = 'Delete Action'
273+ description = 'Are you sure you want to delete this action? This action cannot be undone.'
274+ />
227275 </ div >
228276 ) ;
229277} ;
0 commit comments