@@ -26,6 +26,9 @@ export default function HomeScreen() {
2626 null ,
2727 ) ;
2828 const [ items , setItems ] = useState < ExerciseItem [ ] > ( [ ] ) ;
29+ const [ checkboxStatus , setCheckboxStatus ] = useState < {
30+ [ key : number ] : boolean ;
31+ } > ( { } ) ;
2932 const [ refreshing , setRefreshing ] = useState ( false ) ;
3033 const [ loading , setLoading ] = useState ( false ) ;
3134 const {
@@ -53,30 +56,49 @@ export default function HomeScreen() {
5356 if ( ! success ) return ;
5457
5558 if ( data ) {
56- const exercises = data . map ( ( item : any ) => mapDbToExerciseItem ( item ) ) ;
59+ const exercises = data . map ( ( item : any ) => {
60+ const exercise = mapDbToExerciseItem ( item ) ;
61+ return {
62+ ...exercise ,
63+ Status : checkboxStatus [ exercise . Id ] || false ,
64+ } ;
65+ } ) ;
5766
5867 setItems ( exercises ) ;
5968 }
60- } , [ data , success ] ) ;
69+ } , [ data , success , checkboxStatus ] ) ;
6170
62- const handleAdd = async ( item : ExerciseItem ) => {
71+ const handleAdd = ( item : ExerciseItem ) => {
6372 setLoading ( true ) ;
6473 addExercise ( mapExerciseItemToDbForAdd ( item ) ) ;
6574 setLoading ( false ) ;
6675
76+ setItems ( ( prevItems ) => [
77+ ...prevItems ,
78+ { ...item , Status : false } , // Add the new item with default Status
79+ ] ) ;
80+
6781 setModalVisible ( false ) ;
6882 } ;
6983
70- const handleUpdate = async ( updatedItem : ExerciseItem ) => {
84+ const handleUpdate = ( updatedItem : ExerciseItem ) => {
7185 setLoading ( true ) ;
7286 updateExercise ( mapExerciseItemToDbForUpdate ( updatedItem ) ) ;
7387 setLoading ( false ) ;
7488
89+ setItems ( ( prevItems ) =>
90+ prevItems . map ( ( item ) =>
91+ item . Id === updatedItem . Id
92+ ? { ...updatedItem , Status : item . Status } // Preserve the existing status
93+ : item ,
94+ ) ,
95+ ) ;
96+
7597 setSelectedExercise ( null ) ;
7698 setUpdateModalVisible ( false ) ;
7799 } ;
78100
79- const handleRemove = async ( key : number ) => {
101+ const handleRemove = ( key : number ) => {
80102 setLoading ( true ) ;
81103 removeExercise ( key ) ;
82104 setLoading ( false ) ;
@@ -85,11 +107,10 @@ export default function HomeScreen() {
85107 } ;
86108
87109 const handleCheckboxPress = ( key : number ) => {
88- setItems ( ( prevItems ) =>
89- prevItems . map ( ( item ) =>
90- item . Id === key ? { ...item , Status : ! item . Status } : item ,
91- ) ,
92- ) ;
110+ setCheckboxStatus ( ( prevStatus ) => ( {
111+ ...prevStatus ,
112+ [ key ] : ! prevStatus [ key ] ,
113+ } ) ) ;
93114 } ;
94115
95116 const handleEditPress = async ( exercise : ExerciseItem ) => {
0 commit comments