1- import React , { useEffect , useState } from 'react' ;
1+ import React , { useEffect , useRef , useState } from 'react' ;
22import CustomModal from '../Custom/CustomModal' ;
33import {
44 ICategory ,
@@ -11,7 +11,12 @@ import {
1111import Calculator from '../calculator/Calculator' ;
1212import { evaluate } from 'mathjs' ;
1313import CustomTextField from '../Custom/CustomTextField' ;
14- import { createRecord , deleteRecord , updateRecord } from '../../apis/record' ;
14+ import {
15+ createRecord ,
16+ deleteRecord ,
17+ getRemarks ,
18+ updateRecord ,
19+ } from '../../apis/record' ;
1520import { useQueryClient , useMutation , useQuery } from '@tanstack/react-query' ;
1621import { AxiosError } from 'axios' ;
1722import { toast } from 'react-toastify' ;
@@ -73,8 +78,16 @@ const RecordModal = ({
7378 profile ( userId ! ) ,
7479 ) ;
7580
81+ const { data : remarks } = useQuery < string [ ] > (
82+ [ 'remarks' , selectedCategory ?. id ] ,
83+ ( ) => getRemarks ( selectedCategory ?. id ! ) ,
84+ ) ;
85+
7686 const { wallets } = useRecord ( ) ;
7787
88+ const datePickerRef = useRef < any > ( null ) ;
89+ const textInputRef = useRef < HTMLDivElement > ( null ) ;
90+
7891 const updateCalc = ( key : string ) => {
7992 if ( key === '=' ) {
8093 return setValue ( ( prev ) => {
@@ -267,6 +280,7 @@ const RecordModal = ({
267280 type : 'Once' | 'Continue' ,
268281 ) => {
269282 event . preventDefault ( ) ;
283+
270284 try {
271285 if ( ! editRecord . price ) {
272286 return toast ( 'Please input the expense/income' , { type : 'warning' } ) ;
@@ -301,6 +315,12 @@ const RecordModal = ({
301315
302316 useEffect ( ( ) => {
303317 const keyListener = ( event : KeyboardEvent ) => {
318+ if (
319+ textInputRef . current ?. contains ( document . activeElement ) ||
320+ datePickerRef . current . input . contains ( document . activeElement )
321+ ) {
322+ return ;
323+ }
304324 const reg = / \d | \/ | \* | - | \+ | \. / g;
305325
306326 if ( event . key === 'Backspace' ) {
@@ -447,6 +467,7 @@ const RecordModal = ({
447467 </ div >
448468 </ div >
449469 < DatePicker
470+ ref = { datePickerRef }
450471 onChange = { ( e ) => {
451472 if ( e ) {
452473 setEditRecord ( ( prev ) => {
@@ -480,7 +501,7 @@ const RecordModal = ({
480501 } }
481502 />
482503 </ div >
483- < div className = "py-2" >
504+ < div className = "py-2" ref = { textInputRef } >
484505 < CustomTextField
485506 type = { 'text' }
486507 name = "Remarks"
@@ -494,6 +515,23 @@ const RecordModal = ({
494515 } ) ;
495516 } }
496517 />
518+ < div className = "text-sm pt-2 flex flex-wrap gap-2" >
519+ { remarks ?. map ( ( remark ) => (
520+ < span
521+ className = "bg-info-100 rounded-lg p-1 cursor-pointer hover:bg-info-200"
522+ onClick = { ( ) => {
523+ setEditRecord ( ( prev ) => {
524+ return {
525+ ...prev ,
526+ remarks : remark ,
527+ } ;
528+ } ) ;
529+ } }
530+ >
531+ { remark }
532+ </ span >
533+ ) ) }
534+ </ div >
497535 </ div >
498536
499537 { /* Submit button and choose continue or close */ }
0 commit comments