Skip to content

Commit

Permalink
feat: refacto closing scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
maximeperrault committed Sep 24, 2024
1 parent 3fe4ae4 commit 43454e5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
3 changes: 1 addition & 2 deletions frontend/src/api/dashboardsAPI.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GET_EXTRACTED_AREAS_ERROR_MESSAGE } from '@features/Dashboard/useCases/createDashboard'
import { FrontendApiError } from '@libs/FrontendApiError'
import { geoJsonToWKT } from '@utils/geojsonToWKT'

Expand All @@ -6,8 +7,6 @@ import { monitorenvPrivateApi } from './api'
import type { Dashboard } from '@features/Dashboard/types'
import type { GeoJSON } from 'domain/types/GeoJSON'

const GET_EXTRACTED_AREAS_ERROR_MESSAGE = "Nous n'avons pas pu créer le tableau de bord"

export const dashboardsAPI = monitorenvPrivateApi.injectEndpoints({
endpoints: build => ({
getExtratedArea: build.query<Dashboard.ExtractedArea, GeoJSON.Geometry>({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { useAppSelector } from '@hooks/useAppSelector'

export function DashboardForm() {
const extractedArea = useAppSelector(state => state.dashboard.extractedArea)

return (
<div>
<h1>Dashboard Form</h1>
{JSON.stringify(extractedArea)}
</div>
)
}
9 changes: 7 additions & 2 deletions frontend/src/features/Dashboard/components/MenuButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import styled from 'styled-components'

import { DrawDashboard } from './DrawDashboard'
import { dashboardActions } from '../slice'
import { closeDashboard } from '../useCases/closeDashboard'

export function DashboardMenuButton() {
const dispatch = useAppDispatch()
Expand Down Expand Up @@ -40,6 +41,10 @@ export function DashboardMenuButton() {
}

const closeModal = () => {
dispatch(closeDashboard())
}

const cancel = () => {
dispatch(dashboardActions.setIsDrawing(false))
}

Expand All @@ -48,11 +53,11 @@ export function DashboardMenuButton() {
{isDashboardDialogVisible && (
<StyledMapMenuDialogContainer>
<MapMenuDialog.Header>
<MapMenuDialog.CloseButton Icon={Icon.Close} onClick={toggleDashboardDialog} />
<MapMenuDialog.CloseButton Icon={Icon.Close} onClick={closeModal} />
<StyledTitle as="h2">Briefs pour les unités</StyledTitle>
</MapMenuDialog.Header>
{isDrawing ? (
<StyledDrawDashboard onCancel={closeModal} />
<StyledDrawDashboard onCancel={cancel} />
) : (
<MapMenuDialog.Footer>
<Button Icon={Icon.Plus} isFullWidth onClick={openDrawModal}>
Expand Down
21 changes: 21 additions & 0 deletions frontend/src/features/Dashboard/useCases/closeDashboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { globalActions } from 'domain/shared_slices/Global'

import { dashboardActions } from '../slice'

import type { HomeAppThunk } from '@store/index'

export const GET_EXTRACTED_AREAS_ERROR_MESSAGE = "Nous n'avons pas pu créer le tableau de bord"

export const closeDashboard = (): HomeAppThunk => dispatch => {
resetDrawing()
closeMenuDialog()

function closeMenuDialog() {
dispatch(globalActions.setDisplayedItems({ isDashboardDialogVisible: false }))
}

function resetDrawing() {
dispatch(dashboardActions.setIsDrawing(false))
dispatch(dashboardActions.setGeometry({ coordinates: [], type: 'MultiPolygon' }))
}
}
15 changes: 12 additions & 3 deletions frontend/src/features/Dashboard/useCases/createDashboard.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import { dashboardsAPI } from '@api/dashboardsAPI'
import { addMainWindowBanner } from '@features/MainWindow/useCases/addMainWindowBanner'
import { sideWindowActions } from '@features/SideWindow/slice'
import { Level } from '@mtes-mct/monitor-ui'
import { sideWindowPaths } from 'domain/entities/sideWindow'

import { closeDashboard } from './closeDashboard'
import { dashboardActions } from '../slice'

import type { HomeAppThunk } from '@store/index'
import type { GeoJSON } from 'domain/types/GeoJSON'

export const GET_EXTRACTED_AREAS_ERROR_MESSAGE = "Nous n'avons pas pu créer le tableau de bord"

export const createDashboard =
(geometry: GeoJSON.Geometry): HomeAppThunk =>
async dispatch => {
const { data } = await dispatch(dashboardsAPI.endpoints.getExtratedArea.initiate(geometry))
const { data, error } = await dispatch(dashboardsAPI.endpoints.getExtratedArea.initiate(geometry))
if (data) {
dispatch(closeDashboard())

dispatch(dashboardActions.setExtractedArea(data))
} else {
dispatch(sideWindowActions.focusAndGoTo(sideWindowPaths.DASHBOARD))
}
if (error) {
dispatch(
addMainWindowBanner({
children: `Une erreur est survenue lors de la création du tableau de bord.`,
children: GET_EXTRACTED_AREAS_ERROR_MESSAGE,
isClosable: true,
isFixed: true,
level: Level.ERROR,
Expand Down

0 comments on commit 43454e5

Please sign in to comment.