-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Reporting] Archive reporting with endpoint
/�reporting/archive
- Loading branch information
1 parent
5b53880
commit a9b508d
Showing
5 changed files
with
115 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 58 additions & 31 deletions
89
frontend/src/features/Reportings/useCases/archiveReporting.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,68 @@ | ||
import { reportingActions } from '@features/Reportings/slice' | ||
|
||
import { reportingsAPI } from '../../../api/reportingsAPI' | ||
import { setToast } from '../../../domain/shared_slices/Global' | ||
import { | ||
ReportingContext, | ||
setReportingFormVisibility, | ||
setToast, | ||
VisibilityState | ||
} from '../../../domain/shared_slices/Global' | ||
|
||
import type { Reporting } from '../../../domain/entities/reporting' | ||
import type { HomeAppThunk } from '@store/index' | ||
|
||
export const archiveReportingFromTable = (id: number) => async (dispatch, getState) => { | ||
const { | ||
reporting: { activeReportingId, reportings } | ||
} = getState() | ||
try { | ||
const isReportingExistInLocalStore = reportings[id] || undefined | ||
let reportingToArchive = isReportingExistInLocalStore ? reportings[id].reporting : undefined | ||
export const archiveReporting = | ||
( | ||
id: number, | ||
context: ReportingContext = ReportingContext.SIDE_WINDOW, | ||
closeReporting: boolean = false | ||
): HomeAppThunk => | ||
async (dispatch, getState) => { | ||
const { | ||
reporting: { reportings } | ||
} = getState() | ||
try { | ||
const isReportingExistInLocalStore = reportings[id] | ||
const archiveResponse = await dispatch(reportingsAPI.endpoints.archiveReportings.initiate({ ids: [id] })) | ||
|
||
if (id !== activeReportingId || !reportingToArchive) { | ||
const { data: reporting } = await dispatch(reportingsAPI.endpoints.getReporting.initiate(id)) | ||
reportingToArchive = reporting | ||
} | ||
if ('error' in archiveResponse) { | ||
throw Error("Erreur à l'archivage du signalement") | ||
} else { | ||
if (closeReporting) { | ||
dispatch(reportingActions.deleteSelectedReporting(id)) | ||
dispatch( | ||
setToast({ | ||
containerId: context === ReportingContext.MAP ? 'map' : 'sideWindow', | ||
message: 'Le signalement a bien été archivé', | ||
type: 'success' | ||
}) | ||
) | ||
|
||
dispatch( | ||
setReportingFormVisibility({ | ||
context, | ||
visibility: VisibilityState.NONE | ||
}) | ||
) | ||
|
||
return | ||
} | ||
|
||
dispatch( | ||
setToast({ | ||
containerId: 'sideWindow', | ||
message: 'Le signalement a bien été archivé', | ||
type: 'success' | ||
}) | ||
) | ||
if (isReportingExistInLocalStore) { | ||
const { data: reporting } = await dispatch(reportingsAPI.endpoints.getReporting.initiate(id)) | ||
|
||
const response = await dispatch( | ||
reportingsAPI.endpoints.updateReporting.initiate({ ...(reportingToArchive as Reporting), isArchived: true }) | ||
) | ||
if ('error' in response) { | ||
throw Error("Erreur à l'archivage du signalement") | ||
} else { | ||
dispatch( | ||
setToast({ | ||
containerId: 'sideWindow', | ||
message: 'Le signalement a bien été archivé', | ||
type: 'success' | ||
}) | ||
) | ||
if (isReportingExistInLocalStore) { | ||
dispatch(reportingActions.setReporting({ ...reportings[id].reporting, reporting: response.data })) | ||
if (reporting && reportings[id]) { | ||
dispatch(reportingActions.setReporting({ ...reportings[id], reporting })) | ||
} | ||
} | ||
} | ||
} catch (error) { | ||
dispatch(setToast({ containerId: 'sideWindow', message: error })) | ||
} | ||
} catch (error) { | ||
dispatch(setToast({ containerId: 'sideWindow', message: error })) | ||
} | ||
} |