react-code-dataset / wp-calypso /apps /notifications /src /panel /state /action-middleware /notes /read-status.js
react-code-dataset / wp-calypso /apps /notifications /src /panel /state /action-middleware /notes /read-status.js
Download wp-calypso/apps/notifications/src/panel/state/action-middleware/notes/read-status.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.86 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/apps/notifications/src/panel/state/action-middleware/notes/read-status.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/wp-calypso/apps/notifications/src/panel/state/action-middleware/notes/read-status.js
-
curl -L -o read-status.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/apps/notifications/src/panel/state/action-middleware/notes/read-status.js
1.86 kB
| import { bumpStat } from '../../../rest-client/bump-stat'; | |
| import { markReadStatus, markPostAsSeen } from '../../../rest-client/wpcom'; | |
| import * as types from '../../action-types'; | |
| import getIsNoteRead from '../../selectors/get-is-note-read'; | |
| import getNote from '../../selectors/get-note'; | |
| const clearLocalReadCache = ( noteId ) => { | |
| try { | |
| localStorage.removeItem( `note_read_status_${ noteId }` ); | |
| } catch ( e ) {} | |
| }; | |
| // Clear the local cache of read status | |
| // if we get updated data from the server | |
| export const clearReadCache = ( store, action ) => { | |
| const noteIds = action.notes ? action.notes.map( ( { id } ) => id ) : action.noteIds; | |
| noteIds.forEach( clearLocalReadCache ); | |
| }; | |
| export const markAsRead = ( { getState }, { noteId } ) => { | |
| const state = getState(); | |
| const note = getNote( state, noteId ); | |
| if ( ! note || getIsNoteRead( state, note ) ) { | |
| return; | |
| } | |
| const isSeenSupportedType = note.type === 'automattcher' || note.type === 'new_post'; | |
| const hasRequiredIds = note.meta && note.meta.ids && note.meta.ids.site && note.meta.ids.post; | |
| if ( isSeenSupportedType && hasRequiredIds ) { | |
| // Mark post as seen if notification is open. | |
| markPostAsSeen( note.meta.ids.site, note.meta.ids.post ); | |
| } | |
| // If the note hasn't yet been marked as read then mark it | |
| try { | |
| localStorage.setItem( `note_read_status_${ noteId }`, '1' ); | |
| } catch ( e ) {} | |
| bumpStat( 'notes-read-type', note.type ); | |
| markReadStatus( noteId, true, ( error, data ) => { | |
| if ( error || ! data.success ) { | |
| try { | |
| return localStorage.removeItem( `note_read_status_${ noteId }` ); | |
| } catch ( e ) {} | |
| } | |
| try { | |
| localStorage.setItem( `note_read_status_${ noteId }`, '1' ); | |
| } catch ( e ) {} | |
| } ); | |
| }; | |
| export default { | |
| [ types.NOTES_ADD ]: [ clearReadCache ], | |
| [ types.NOTES_REMOVE ]: [ clearReadCache ], | |
| [ types.SELECT_NOTE ]: [ markAsRead ], | |
| }; | |