@@ -12,8 +12,12 @@ import {
1212 selectIsPDL ,
1313 selectIsAdmin ,
1414 selectCheckin ,
15- selectProfile
15+ selectProfile ,
16+ selectCanViewPrivateNotesPermission ,
17+ selectCanCreatePrivateNotesPermission ,
18+ selectCanUpdatePrivateNotesPermission ,
1619} from '../../context/selectors' ;
20+ import { UPDATE_TOAST } from '../../context/actions' ;
1721import { debounce } from 'lodash/function' ;
1822import { Editor } from '@tinymce/tinymce-react' ;
1923import LockIcon from '@mui/icons-material/Lock' ;
@@ -49,19 +53,36 @@ const PrivateNote = () => {
4953
5054 useEffect ( ( ) => {
5155 async function getPrivateNotes ( ) {
52- setIsLoading ( true ) ;
53- try {
54- let res = await getPrivateNoteByCheckinId ( checkinId , csrf ) ;
55- if ( res . error ) throw new Error ( res . error ) ;
56- const currentNote =
57- res . payload && res . payload . data && res . payload . data . length > 0
58- ? res . payload . data [ 0 ]
59- : null ;
60- if ( currentNote ) {
61- setNote ( currentNote ) ;
62- } else if ( currentUserId === pdlId ) {
63- if ( ! noteRef . current . some ( id => id === checkinId ) ) {
64- noteRef . current . push ( checkinId ) ;
56+ if ( selectCanViewPrivateNotesPermission ( state ) ) {
57+ setIsLoading ( true ) ;
58+ try {
59+ let res = await getPrivateNoteByCheckinId ( checkinId , csrf ) ;
60+ if ( res . error ) throw new Error ( res . error ) ;
61+ const currentNote =
62+ res . payload && res . payload . data && res . payload . data . length > 0
63+ ? res . payload . data [ 0 ]
64+ : null ;
65+ if ( currentNote ) {
66+ setNote ( currentNote ) ;
67+ } else if ( currentUserId === pdlId ) {
68+ if ( ! noteRef . current . some ( id => id === checkinId ) &&
69+ selectCanCreatePrivateNotesPermission ( state ) ) {
70+ noteRef . current . push ( checkinId ) ;
71+ res = await createPrivateNote (
72+ {
73+ checkinid : checkinId ,
74+ createdbyid : currentUserId ,
75+ description : ''
76+ } ,
77+ csrf
78+ ) ;
79+ noteRef . current = noteRef . current . filter ( id => id !== checkinId ) ;
80+ if ( res . error ) throw new Error ( res . error ) ;
81+ if ( res && res . payload && res . payload . data ) {
82+ setNote ( res . payload . data ) ;
83+ }
84+ }
85+ } else if ( selectCanCreatePrivateNotesPermission ( state ) ) {
6586 res = await createPrivateNote (
6687 {
6788 checkinid : checkinId ,
@@ -70,37 +91,45 @@ const PrivateNote = () => {
7091 } ,
7192 csrf
7293 ) ;
73- noteRef . current = noteRef . current . filter ( id => id !== checkinId ) ;
7494 if ( res . error ) throw new Error ( res . error ) ;
7595 if ( res && res . payload && res . payload . data ) {
7696 setNote ( res . payload . data ) ;
7797 }
7898 }
79- } else {
80- res = await createPrivateNote (
81- {
82- checkinid : checkinId ,
83- createdbyid : currentUserId ,
84- description : ''
85- } ,
86- csrf
87- ) ;
88- if ( res . error ) throw new Error ( res . error ) ;
89- if ( res && res . payload && res . payload . data ) {
90- setNote ( res . payload . data ) ;
91- }
99+ } catch ( e ) {
100+ console . error ( "getPrivateNotes: " + e ) ;
92101 }
93- } catch ( e ) {
94- console . error ( "getPrivateNotes: " + e ) ;
102+ setIsLoading ( false ) ;
95103 }
96- setIsLoading ( false ) ;
97104 }
98105 if ( csrf ) {
99106 getPrivateNotes ( ) ;
100107 }
101108 } , [ csrf , checkinId , currentUserId , pdlId ] ) ;
102109
103110 const handleNoteChange = ( content , delta , source , editor ) => {
111+ if ( note == null ) {
112+ window . snackDispatch ( {
113+ type : UPDATE_TOAST ,
114+ payload : {
115+ severity : 'error' ,
116+ toast : selectCanCreatePrivateNotesPermission ( state )
117+ ? 'No private note was created'
118+ : 'No permission to create private notes'
119+ }
120+ } ) ;
121+ return ;
122+ }
123+ if ( ! selectCanUpdatePrivateNotesPermission ( state ) ) {
124+ window . snackDispatch ( {
125+ type : UPDATE_TOAST ,
126+ payload : {
127+ severity : 'error' ,
128+ toast : 'No permission to update private notes'
129+ }
130+ } ) ;
131+ return ;
132+ }
104133 if ( Object . keys ( note ) . length === 0 || ! csrf || currentCheckin ?. completed ) {
105134 return ;
106135 }
0 commit comments