11import { useState , useEffect } from 'react' ;
22import { toast } from 'react-toastify' ;
3- import { Button , Modal , ModalHeader , ModalBody , ModalFooter , Input } from 'reactstrap' ;
3+ import {
4+ Button ,
5+ Modal ,
6+ ModalHeader ,
7+ ModalBody ,
8+ ModalFooter ,
9+ Input ,
10+ Table ,
11+ Pagination ,
12+ PaginationItem ,
13+ PaginationLink ,
14+ } from 'reactstrap' ;
415import { connect , useDispatch } from 'react-redux' ;
516import hasPermission from '~/utils/permissions' ;
617import { boxStyle , boxStyleDark } from '../../styles' ;
@@ -14,16 +25,19 @@ import {
1425 getOwnerMessage ,
1526 updateOwnerMessage ,
1627 deleteOwnerMessage ,
28+ getOwnerMessageHistory ,
1729} from '../../actions/ownerMessageAction' ;
1830
1931function OwnerMessage ( {
2032 auth,
2133 ownerMessage,
2234 ownerStandardMessage,
35+ ownerMessageHistory,
2336 darkMode,
2437 getMessage,
2538 updateMessage,
2639 deleteMessage,
40+ getMessageHistory,
2741} ) {
2842 const dispatch = useDispatch ( ) ;
2943 const { user } = auth ;
@@ -32,6 +46,7 @@ function OwnerMessage({
3246 const [ disableButtons , setDisableButtons ] = useState ( true ) ;
3347 const [ message , setMessage ] = useState ( '' ) ;
3448 const [ modal , setModal ] = useState ( false ) ;
49+ const [ historyModalOpen , setHistoryModalOpen ] = useState ( false ) ;
3550 const [ modalDeleteWarning , setModalDeleteWarning ] = useState ( false ) ;
3651 const [ modalWrongPictureFormatWarning , setModalWrongPictureFormatWarning ] = useState ( false ) ;
3752
@@ -107,9 +122,33 @@ function OwnerMessage({
107122 return < span className = { styles . message } > { messages } </ span > ;
108123 }
109124
125+ function getHistoryContent ( messages ) {
126+ if ( isImage . test ( messages ) ) {
127+ return < img src = { messages } alt = "" className = { styles . ownerMessageImg } /> ;
128+ }
129+ return < span > { messages } </ span > ;
130+ }
131+
132+ const formatDateTimePST = date =>
133+ new Intl . DateTimeFormat ( 'en-US' , {
134+ timeZone : 'America/Los_Angeles' ,
135+ year : 'numeric' ,
136+ month : 'short' ,
137+ day : 'numeric' ,
138+ hour : '2-digit' ,
139+ minute : '2-digit' ,
140+ hour12 : true ,
141+ } ) . format ( new Date ( date ) ) ;
142+
143+ const page = ownerMessageHistory ?. pagination ?. page ? ownerMessageHistory . pagination . page : 1 ;
144+ const totalPages = ownerMessageHistory ?. pagination ?. totalPages
145+ ? ownerMessageHistory . pagination . totalPages
146+ : 1 ;
147+
110148 useEffect ( ( ) => {
111149 async function fetchMessages ( ) {
112150 await getMessage ( ) ;
151+ await getMessageHistory ( page , 10 ) ;
113152 }
114153 fetchMessages ( ) ;
115154 } , [ ] ) ;
@@ -177,6 +216,22 @@ function OwnerMessage({
177216 />
178217 </ button >
179218 ) }
219+ < button
220+ type = "submit"
221+ className = { styles . ownerMessageButton }
222+ onClick = { ( ) => setHistoryModalOpen ( true ) }
223+ aria-label = "View owner message edit history"
224+ >
225+ < i
226+ style = { {
227+ fontSize : '24px' ,
228+ marginLeft : '0.25rem' ,
229+ color : 'white' ,
230+ } }
231+ className = "fa fa-history"
232+ aria-label = "View owner message edit history"
233+ > </ i >
234+ </ button >
180235 </ span >
181236 ) }
182237
@@ -271,6 +326,104 @@ function OwnerMessage({
271326 </ Button >
272327 </ ModalFooter >
273328 </ Modal >
329+
330+ { /* Owner Edit History Modal */ }
331+ < Modal
332+ size = "xl"
333+ isOpen = { historyModalOpen }
334+ toggle = { ( ) => setHistoryModalOpen ( false ) }
335+ className = { `${ fontColor } ` }
336+ >
337+ < ModalHeader toggle = { ( ) => setHistoryModalOpen ( false ) } >
338+ Owner Message Edit History
339+ </ ModalHeader >
340+ < ModalBody className = { headerBg } >
341+ { ! ownerMessageHistory ?. data ?. length ? (
342+ < p > No edit history available.</ p >
343+ ) : (
344+ < >
345+ < Table className = { styles . ownerHistoryTable } >
346+ < thead className = { `${ darkMode ? 'bg-space-cadet' : '' } ` } >
347+ < tr >
348+ < th
349+ className = { `${ styles . ownerHistorySmallColumn } ${
350+ darkMode ? 'bg-space-cadet' : ''
351+ } `}
352+ >
353+ Date
354+ </ th >
355+ < th
356+ className = { `${ styles . ownerHistorySmallColumn } ${
357+ darkMode ? 'bg-space-cadet' : ''
358+ } `}
359+ >
360+ Edited By
361+ </ th >
362+ < th className = { `${ darkMode ? 'bg-space-cadet' : '' } ` } > Action</ th >
363+ < th className = { `${ darkMode ? 'bg-space-cadet' : '' } ` } > Old Message</ th >
364+ < th className = { `${ darkMode ? 'bg-space-cadet' : '' } ` } > New Message</ th >
365+ </ tr >
366+ </ thead >
367+ < tbody className = { darkMode ? 'bg-yinmn-blue dark-mode' : '' } >
368+ { ownerMessageHistory . data . map ( ( historyItem , index ) => (
369+ < tr key = { index } >
370+ < td className = { styles . ownerHistorySmallColumn } >
371+ < span className = { styles . showInTablet } >
372+ < b > Date:</ b >
373+ </ span >
374+ { formatDateTimePST ( historyItem . createdAt ) } PST
375+ </ td >
376+
377+ < td className = { styles . ownerHistorySmallColumn } >
378+ < span className = { styles . showInTablet } >
379+ < b > Edited By:</ b >
380+ </ span >
381+ { historyItem . requestorName } ({ historyItem . requestorEmail } )
382+ </ td >
383+ < td >
384+ < span className = { styles . showInTablet } >
385+ < b > Action:</ b >
386+ </ span >
387+ { historyItem . action }
388+ </ td >
389+ < td >
390+ < span className = { styles . showInTablet } >
391+ < b > Old Message:</ b >
392+ </ span >
393+ { getHistoryContent ( historyItem . oldMessage ) }
394+ </ td >
395+ < td >
396+ < span className = { styles . showInTablet } >
397+ < b > New Message:</ b >
398+ </ span >
399+ { getHistoryContent ( historyItem . newMessage ) }
400+ </ td >
401+ </ tr >
402+ ) ) }
403+ </ tbody >
404+ </ Table >
405+ < Pagination aria-label = "Table pagination" >
406+ < PaginationItem disabled = { page === 1 } >
407+ < PaginationLink previous onClick = { ( ) => getMessageHistory ( page - 1 , 10 ) } />
408+ </ PaginationItem >
409+
410+ < PaginationItem active >
411+ < PaginationLink > { page } </ PaginationLink >
412+ </ PaginationItem >
413+
414+ < PaginationItem disabled = { page === totalPages } >
415+ < PaginationLink next onClick = { ( ) => getMessageHistory ( page + 1 , 10 ) } />
416+ </ PaginationItem >
417+ </ Pagination >
418+ </ >
419+ ) }
420+ </ ModalBody >
421+ < ModalFooter className = { bodyBg } >
422+ < Button color = "secondary" onClick = { ( ) => setHistoryModalOpen ( false ) } style = { boxStyling } >
423+ Close
424+ </ Button >
425+ </ ModalFooter >
426+ </ Modal >
274427 </ div >
275428 ) ;
276429}
@@ -280,12 +433,14 @@ const mapStateToProps = state => ({
280433 ownerMessage : state . ownerMessage . message ,
281434 ownerStandardMessage : state . ownerMessage . standardMessage ,
282435 darkMode : state . theme . darkMode ,
436+ ownerMessageHistory : state . ownerMessage . history ,
283437} ) ;
284438
285439const mapDispatchToProps = dispatch => ( {
286440 getMessage : ( ) => dispatch ( getOwnerMessage ( ) ) ,
287441 updateMessage : ownerMessage => dispatch ( updateOwnerMessage ( ownerMessage ) ) ,
288442 deleteMessage : ( ) => dispatch ( deleteOwnerMessage ( ) ) ,
443+ getMessageHistory : ( page , limit ) => dispatch ( getOwnerMessageHistory ( page , limit ) ) ,
289444} ) ;
290445
291446export default connect ( mapStateToProps , mapDispatchToProps ) ( OwnerMessage ) ;
0 commit comments