Skip to content

Commit

Permalink
Merge pull request #989 from MTES-MCT/refresh_opened_beacon_status
Browse files Browse the repository at this point in the history
Fix refresh opened beacon status comment
  • Loading branch information
louptheron authored Feb 7, 2022
2 parents d6239c1 + 211164f commit 22e5e7f
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
8 changes: 4 additions & 4 deletions frontend/src/domain/shared_slices/BeaconStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ const beaconStatusSlice = createSlice({
]
},
/**
* Open a single beacon status
* @function selectBeaconStatus
* Open a beacon status
* @function setOpenedBeaconStatus
* @memberOf BeaconStatusReducer
* @param {Object=} state
* @param {{payload: BeaconStatusWithDetails}} action - the beacon status to open
*/
selectBeaconStatus (state, action) {
setOpenedBeaconStatus (state, action) {
state.openedBeaconStatus = action.payload
},
/**
Expand All @@ -56,7 +56,7 @@ const beaconStatusSlice = createSlice({
export const {
setBeaconStatuses,
updateLocalBeaconStatuses,
selectBeaconStatus,
setOpenedBeaconStatus,
closeBeaconStatus
} = beaconStatusSlice.actions

Expand Down
11 changes: 11 additions & 0 deletions frontend/src/domain/types/beaconStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
* @property {string} dateTime
*/

/**
* @typedef BeaconStatusAction
* @property {number} id
* @property {int} beaconStatusId
* @property {string} propertyName
* @property {string} previousValue
* @property {string} nextValue
* @property {string} dateTime
*/

/**
* @typedef BeaconStatusCommentInput
* @property {string} comment
Expand All @@ -38,4 +48,5 @@
* @typedef BeaconStatusWithDetails
* @property {BeaconStatus} beaconStatus
* @property {BeaconStatusComment[]} comments
* @property {BeaconStatusAction[]} actions
*/
16 changes: 11 additions & 5 deletions frontend/src/domain/use_cases/openBeaconStatus.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { getBeaconStatusFromAPI } from '../../api/fetch'
import { setError } from '../shared_slices/Global'
import { selectBeaconStatus } from '../shared_slices/BeaconStatus'
import { setOpenedBeaconStatus } from '../shared_slices/BeaconStatus'

const openBeaconStatus = beaconStatus => (dispatch, getState) => {
/**
* Open a single beacon status
* @function setOpenedBeaconStatus
* @param {BeaconStatusWithDetails} beaconStatus - the beacon status to open
* @param {boolean} fromCron - true if called from cron
*/
const openBeaconStatus = (beaconStatus, fromCron) => (dispatch, getState) => {
const previousBeaconStatus = getState().beaconStatus.openedBeaconStatus
dispatch(selectBeaconStatus(beaconStatus))
dispatch(setOpenedBeaconStatus(beaconStatus))

getBeaconStatusFromAPI(beaconStatus.beaconStatus?.id).then(beaconStatusWithDetails => {
dispatch(selectBeaconStatus(beaconStatusWithDetails))
dispatch(setOpenedBeaconStatus(beaconStatusWithDetails))
}).catch(error => {
console.error(error)
dispatch(setError(error))
dispatch(selectBeaconStatus(previousBeaconStatus))
dispatch(setOpenedBeaconStatus(previousBeaconStatus))
})
}

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/domain/use_cases/saveBeaconStatusComment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { saveBeaconStatusCommentFromAPI } from '../../api/fetch'
import { setError } from '../shared_slices/Global'
import { selectBeaconStatus } from '../shared_slices/BeaconStatus'
import { setOpenedBeaconStatus } from '../shared_slices/BeaconStatus'

/**
* Save a new comment to a beacon status
Expand All @@ -16,7 +16,7 @@ const saveBeaconStatusComment = (beaconStatusId, comment) => (dispatch, getState
}

return saveBeaconStatusCommentFromAPI(beaconStatusId, newCommentInput).then(beaconStatusWithDetails => {
return dispatch(selectBeaconStatus(beaconStatusWithDetails))
return dispatch(setOpenedBeaconStatus(beaconStatusWithDetails))
}).catch(error => {
console.error(error)
dispatch(setError(error))
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/domain/use_cases/updateBeaconStatus.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { setError } from '../shared_slices/Global'
import { updateBeaconStatusFromAPI } from '../../api/fetch'
import { selectBeaconStatus, setBeaconStatuses, updateLocalBeaconStatuses } from '../shared_slices/BeaconStatus'
import { setOpenedBeaconStatus, setBeaconStatuses, updateLocalBeaconStatuses } from '../shared_slices/BeaconStatus'

/**
* Update a beacon status
Expand All @@ -15,7 +15,7 @@ const updateBeaconStatus = (id, nextBeaconStatus, updatedFields) => (dispatch, g

return updateBeaconStatusFromAPI(id, updatedFields).then(updatedBeaconStatusWithDetails => {
if (beaconStatusToUpdateIsOpened) {
dispatch(selectBeaconStatus(updatedBeaconStatusWithDetails))
dispatch(setOpenedBeaconStatus(updatedBeaconStatusWithDetails))
}
}).catch(error => {
dispatch(setError(error))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ const BeaconStatusDetailsBody = ({ comments, actions, beaconStatusId }) => {
const textareaRef = useRef(null)

useEffect(() => {
if (!comments?.length) {
setComment('')
}

if (comments?.length) {
setToday(getDate(new Date().toISOString()))

Expand Down

0 comments on commit 22e5e7f

Please sign in to comment.