Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tech - Migre la liste des règlementations du BackOffice vers Typescript #3777

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ module.exports = {
caughtErrorsIgnorePattern: '^_'
}
],
'@typescript-eslint/prefer-nullish-coalescing': 'warn',
'@typescript-eslint/prefer-nullish-coalescing': 'error',

'typescript-sort-keys/interface': 'error',
'typescript-sort-keys/string-enum': 'error'
Expand Down
26 changes: 13 additions & 13 deletions frontend/cypress/e2e/back_office/regulation_form/creation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,21 @@
cy.get('.Component-SingleTag').should('have.length', 2)
})

it('Enter a reg text name with a valid url', () => {
it.only('Enter a reg text name with a valid url', () => {

Check failure on line 79 in frontend/cypress/e2e/back_office/regulation_form/creation.spec.ts

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

Unexpected exclusive mocha test
// TODO A re-render might stop the first fill(), we need to re-fill a second time
cy.fill('Nom', 'zone name', { delay: 20 })
cy.fill('Nom', 'zone name', { delay: 20 })
cy.fill('URL', 'http://url.com', { delay: 20 })
cy.clickButton('Enregistrer')

cy.get('.Component-SingleTag').contains('zone name')
cy.get('.Component-SingleTag').find('button').click()
cy.get('.Component-SingleTag').should('not.exist')

// Clear reg zone name form
cy.clickButton('Effacer')
cy.get('[name="reference"]').invoke('val').should('equal', '')
cy.get('[name="url"]').invoke('val').should('equal', '')
// cy.fill('Nom', 'zone name', { delay: 20 })
// cy.fill('URL', 'http://url.com', { delay: 20 })
// cy.clickButton('Enregistrer')

// cy.get('.Component-SingleTag').contains('zone name')
// cy.get('.Component-SingleTag').find('button').click()
// cy.get('.Component-SingleTag').should('not.exist')

// // Clear reg zone name form
// cy.clickButton('Effacer')
// cy.get('[name="reference"]').invoke('val').should('equal', '')
// cy.get('[name="url"]').invoke('val').should('equal', '')
})

it('A modal should be open on go back button click', () => {
Expand Down
136 changes: 59 additions & 77 deletions frontend/src/api/geoserver.jsx → frontend/src/api/geoserver.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import GML from 'ol/format/GML'
import WFS from 'ol/format/WFS'

import { HttpStatusCode } from './constants'
import { LayerProperties } from '../domain/entities/layers/constants'
import { OPENLAYERS_PROJECTION, WSG84_PROJECTION } from '../domain/entities/map/constants'
import WFS from 'ol/format/WFS'
import GML from 'ol/format/GML'
import { REGULATION_ACTION_TYPE } from '../features/Regulation/utils'
import { HttpStatusCode } from './constants'
import { ApiError } from '../libs/ApiError'

import type { ShowedLayer } from 'domain/entities/layers/types'

export const REGULATORY_ZONE_METADATA_ERROR_MESSAGE = "Nous n'avons pas pu récupérer la couche réglementaire"
const REGULATORY_ZONES_ERROR_MESSAGE = "Nous n'avons pas pu récupérer les zones réglementaires"
const REGULATORY_ZONES_ZONE_SELECTION_ERROR_MESSAGE = "Nous n'avons pas pu filtrer les zones réglementaires par zone"
Expand Down Expand Up @@ -39,12 +42,11 @@
.then(response => {
if (response.status === HttpStatusCode.OK) {
return response.json()
} else {
response.text().then(text => {
console.error(text)
})
throw Error(REGULATORY_ZONES_ERROR_MESSAGE)
}
response.text().then(text => {
console.error(text)
})
throw Error(REGULATORY_ZONES_ERROR_MESSAGE)
})
.catch(error => {
if (import.meta.env.DEV) {
Expand Down Expand Up @@ -73,18 +75,19 @@
'regulatory_references IS NULL AND zone IS NULL AND region IS NULL AND law_type IS NULL AND topic IS NULL'
const REQUEST =
`${geoserverURL}/geoserver/wfs?service=WFS&version=1.1.0&request=GetFeature&typename=monitorfish:` +
`${LayerProperties.REGULATORY.code}&outputFormat=application/json&propertyName=geometry,id&CQL_FILTER=` +
filter.replace(/'/g, '%27').replace(/ /g, '%20')
`${LayerProperties.REGULATORY.code}&outputFormat=application/json&propertyName=geometry,id&CQL_FILTER=${filter
.replace(/'/g, '%27')
.replace(/ /g, '%20')}`

return fetch(REQUEST)
.then(response => {
if (response.status === HttpStatusCode.OK) {
return response.json()
} else {
response.text().then(text => {
console.error(text)
})
throw Error(GEOMETRY_ERROR_MESSAGE)
}
response.text().then(text => {
console.error(text)
})
throw Error(GEOMETRY_ERROR_MESSAGE)
})
.catch(error => {
console.error(error)
Expand All @@ -103,25 +106,22 @@
* @returns {Promise<GeoJSON>} The feature GeoJSON
* @throws {Error}
*/
function getAdministrativeZoneFromAPI(administrativeZone, extent, subZone, fromBackoffice) {
export async function getAdministrativeZoneFromAPI(administrativeZone, extent, subZone, fromBackoffice) {
const geoserverURL = fromBackoffice ? GEOSERVER_BACKOFFICE_URL : GEOSERVER_URL

return fetch(getAdministrativeZoneURL(administrativeZone, extent, subZone, geoserverURL))
.then(response => {

Check failure on line 113 in frontend/src/api/geoserver.tsx

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

Expected to return a value at the end of arrow function
if (response.status === HttpStatusCode.OK) {
return response
.json()
.then(response => {
return response
})
.then(response => response)

Check failure on line 117 in frontend/src/api/geoserver.tsx

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

'response' is already declared in the upper scope on line 113 column 11
.catch(e => {
throwIrretrievableAdministrativeZoneError(e, administrativeZone)
})
} else {
response.text().then(response => {
throwIrretrievableAdministrativeZoneError(response, administrativeZone)
})
}
response.text().then(response => {

Check failure on line 122 in frontend/src/api/geoserver.tsx

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

'response' is already declared in the upper scope on line 113 column 11
throwIrretrievableAdministrativeZoneError(response, administrativeZone)
})
})
.catch(e => {
throwIrretrievableAdministrativeZoneError(e, administrativeZone)
Expand All @@ -148,41 +148,36 @@
if (subZone) {
const filter = `${subZone.replace(/'/g, "''")}`

subZoneFilter = '&featureID=' + filter.replace(/'/g, '%27').replace(/ /g, '%20')
subZoneFilter = `&featureID=${filter.replace(/'/g, '%27').replace(/ /g, '%20')}`
}

return (
`${geoserverURL}/geoserver/wfs?service=WFS&` +
`version=1.1.0&request=GetFeature&typename=monitorfish:${type}&` +
`outputFormat=application/json&srsname=${WSG84_PROJECTION}` +
extentFilter +
subZoneFilter
`outputFormat=application/json&srsname=${WSG84_PROJECTION}${extentFilter}${subZoneFilter}`
)
}

/**
* @description This API isn't authenticated
*/
function getRegulatoryZoneFromAPI(type, regulatoryZone, fromBackoffice) {
export async function getRegulatoryZoneFromAPI(type: string, regulatoryZone: ShowedLayer, fromBackoffice: boolean) {
try {
const geoserverURL = fromBackoffice ? GEOSERVER_BACKOFFICE_URL : GEOSERVER_URL

return fetch(getRegulatoryZoneURL(type, regulatoryZone, geoserverURL))
return await fetch(getRegulatoryZoneURL(type, regulatoryZone, geoserverURL))
.then(response => {

Check failure on line 169 in frontend/src/api/geoserver.tsx

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

Expected to return a value at the end of arrow function
if (response.status === HttpStatusCode.OK) {
return response
.json()
.then(response => {
return getFirstFeature(response)
})
.then(response => getFirstFeature(response))

Check failure on line 173 in frontend/src/api/geoserver.tsx

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

'response' is already declared in the upper scope on line 169 column 13
.catch(e => {
throw getIrretrievableRegulatoryZoneError(e, regulatoryZone)
})
} else {
response.text().then(response => {
throw getIrretrievableRegulatoryZoneError(response, regulatoryZone)
})
}
response.text().then(response => {

Check failure on line 178 in frontend/src/api/geoserver.tsx

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

'response' is already declared in the upper scope on line 169 column 13
throw getIrretrievableRegulatoryZoneError(response, regulatoryZone)
})
})
.catch(e => {
throw getIrretrievableRegulatoryZoneError(e, regulatoryZone)
Expand All @@ -207,11 +202,11 @@
/'/g,
"''"
)}' AND zone='${encodeURIComponent(regulatoryZone.zone).replace(/'/g, "''")}'`

return (
`${geoserverURL}/geoserver/wfs?service=WFS` +
`&version=1.1.0&request=GetFeature&typename=monitorfish:${type}` +
'&outputFormat=application/json&CQL_FILTER=' +
filter.replace(/'/g, '%27').replace(/ /g, '%20')
`&outputFormat=application/json&CQL_FILTER=${filter.replace(/'/g, '%27').replace(/ /g, '%20')}`
)
}

Expand All @@ -232,37 +227,34 @@
`${geoserverURL}/geoserver/wfs?service=WFS` +
`&version=1.1.0&request=GetFeature&typename=monitorfish:${LayerProperties.REGULATORY.code}` +
`&outputFormat=application/json&srsname=${WSG84_PROJECTION}` +
`&bbox=${extent.join(',')},${OPENLAYERS_PROJECTION}` +
'&propertyName=id,law_type,topic,gears,species,regulatory_references,zone,region'
`&bbox=${extent.join(',')},${OPENLAYERS_PROJECTION}${'&propertyName=id,law_type,topic,gears,species,regulatory_references,zone,region'
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/ /g, '%20')
.replace(/ /g, '%20')}`
)
.then(response => {
if (response.status === HttpStatusCode.OK) {
return response
.json()
.then(response => {
return response
})
.then(response => response)

Check failure on line 240 in frontend/src/api/geoserver.tsx

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

'response' is already declared in the upper scope on line 236 column 13
.catch(error => {
console.error(error)
throw REGULATORY_ZONES_ZONE_SELECTION_ERROR_MESSAGE

Check failure on line 243 in frontend/src/api/geoserver.tsx

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

Expected an error object to be thrown
})
} else {
response.text().then(response => {
console.error(response)
})
throw REGULATORY_ZONES_ZONE_SELECTION_ERROR_MESSAGE
}
response.text().then(response => {

Check failure on line 246 in frontend/src/api/geoserver.tsx

View workflow job for this annotation

GitHub Actions / Run frontend unit tests

'response' is already declared in the upper scope on line 236 column 13
console.error(response)
})
throw REGULATORY_ZONES_ZONE_SELECTION_ERROR_MESSAGE
})
.catch(error => {
console.error(error)
throw REGULATORY_ZONES_ZONE_SELECTION_ERROR_MESSAGE
})
} catch (error) {
console.error(error)

return Promise.reject(REGULATORY_ZONES_ZONE_SELECTION_ERROR_MESSAGE)
}
}
Expand All @@ -273,9 +265,8 @@

if (response.features.length === 1 && response.features[FIRST_FEATURE]) {
return response.features[FIRST_FEATURE]
} else {
throw Error('We found multiple features for this zone')
}
throw Error('We found multiple features for this zone')
}

/**
Expand All @@ -294,15 +285,12 @@
return fetch(url)
.then(response => {
if (response.status === HttpStatusCode.OK) {
return response.json().then(response => {
return getFirstFeature(response)
})
} else {
response.text().then(text => {
console.error(text)
})
throw Error(REGULATORY_ZONE_METADATA_ERROR_MESSAGE)
return response.json().then(response => getFirstFeature(response))
}
response.text().then(text => {
console.error(text)
})
throw Error(REGULATORY_ZONE_METADATA_ERROR_MESSAGE)
})
.catch(error => {
console.error(error)
Expand All @@ -323,8 +311,9 @@
query =
`${geoserverURL}/geoserver/wfs?service=WFS&` +
`version=1.1.0&request=GetFeature&typename=monitorfish:${type}&` +
`outputFormat=application/json&srsname=${WSG84_PROJECTION}&CQL_FILTER=` +
filter.replace(/'/g, '%27').replace(/ /g, '%20')
`outputFormat=application/json&srsname=${WSG84_PROJECTION}&CQL_FILTER=${filter
.replace(/'/g, '%27')
.replace(/ /g, '%20')}`
} else {
query =
`${geoserverURL}/geoserver/wfs?service=WFS&` +
Expand All @@ -337,17 +326,14 @@
if (response.status === HttpStatusCode.OK) {
return response
.json()
.then(response => {
return response
})
.then(response => response)
.catch(e => {
throwIrretrievableAdministrativeZoneError(e, type)
})
} else {
response.text().then(response => {
throwIrretrievableAdministrativeZoneError(response, type)
})
}
response.text().then(response => {
throwIrretrievableAdministrativeZoneError(response, type)
})
})
.catch(e => {
if (import.meta.env.DEV) {
Expand Down Expand Up @@ -383,16 +369,14 @@
const payload = xs.serializeToString(transaction)

return fetch(`${GEOSERVER_BACKOFFICE_URL}/geoserver/wfs`, {
body: payload.replace('feature:', ''),
contentType: 'text/xml',
dataType: 'xml',
method: 'POST',
mode: 'no-cors',
dataType: 'xml',
processData: false,
contentType: 'text/xml',
body: payload.replace('feature:', '')
processData: false
})
.then(r => {
return r
})
.then(r => r)
.catch(error => {
console.error(error)
throw Error(UPDATE_REGULATION_MESSAGE)
Expand All @@ -404,9 +388,7 @@
getAdministrativeSubZonesFromAPI,
getRegulatoryFeatureMetadataFromAPI,
getRegulatoryZoneURL,
getRegulatoryZoneFromAPI,
getAdministrativeZoneURL,
getAdministrativeZoneFromAPI,
getAllGeometryWithoutProperty,
getAllRegulatoryLayersFromAPI
}
2 changes: 1 addition & 1 deletion frontend/src/auth/components/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function Login() {

return (
<LoginBackground>
{auth?.isLoading || isLoading ? (
{!!auth?.isLoading || isLoading ? (
<LoadingSpinnerWall isVesselShowed />
) : (
<div>
Expand Down
3 changes: 0 additions & 3 deletions frontend/src/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ export const COLORS = {

export const HIT_PIXEL_TO_TOLERANCE = 10

/**
* @enum {string}
*/
export const SQUARE_BUTTON_TYPE = {
DELETE: 'delete'
}
2 changes: 1 addition & 1 deletion frontend/src/coordinates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function getDDCoordinates(transformedCoordinates: number[], forPrint: boolean):
return []
}
let longitude = longitudeInteger.toString().trim().replace(/-/g, '')
const decimals = longitudeDecimals?.substring(0, precision) || '000000'
const decimals = longitudeDecimals?.substring(0, precision) ?? '000000'
longitude = `${negative ? '-' : ''}${getPaddedDegrees(longitude, CoordinateLatLon.LONGITUDE)}.${decimals}`

return [`${transformedCoordinates[1].toFixed(precision)}°`, `${longitude}°`]
Expand Down
Loading
Loading