Skip to content

Commit

Permalink
fix: update for mes-adresses-api v2 and linter eslint (#45)
Browse files Browse the repository at this point in the history
* fix: update for mes-adresses-api v2

* feat: replace co by eslint

* fix ci

* fix: stats creation url
  • Loading branch information
MaGOs92 authored Jan 4, 2024
1 parent 5809b07 commit 5337bcc
Show file tree
Hide file tree
Showing 16 changed files with 987 additions and 1,579 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": ["next", "xo", "xo-nextjs"]
"extends": ["next", "prettier"]
}
21 changes: 10 additions & 11 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@ name: Node.js CI

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: yarn
- run: yarn xo
- run: yarn build
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: yarn
- run: yarn lint
- run: yarn build
1 change: 0 additions & 1 deletion components/dashboard/charts/creation-count.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable unicorn/no-array-reduce */
import {Chart} from 'react-chartjs-2'
import PropTypes from 'prop-types'
import {Chart as ChartJS, registerables} from 'chart.js'
Expand Down
1 change: 0 additions & 1 deletion components/dashboard/charts/publication-count.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable unicorn/no-array-reduce */
import {Chart} from 'react-chartjs-2'
import PropTypes from 'prop-types'
import {Chart as ChartJS, registerables} from 'chart.js'
Expand Down
1 change: 0 additions & 1 deletion components/dashboard/charts/publications-per-department.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ ChartJS.register(...registerables)
const PublicationPerDepartmentChart = ({publicationsResponse}) => {
const data = useMemo(() => {
const sortedDepartments = departements.map(({nom, code}) => {
// eslint-disable-next-line unicorn/no-array-reduce
const count = publicationsResponse.reduce((acc, {publishedBAL}) => {
const selectedCommunes = Object.keys(publishedBAL).filter(
codeCommune => codeCommune.slice(0, code.length) === code
Expand Down
3 changes: 1 addition & 2 deletions components/editable-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type EditableListProps<T> = {
actions?: Record<string, (item: T) => void>;
}

// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-constraint
export const EditableList = <T extends unknown>({
createBtn,
headers,
Expand All @@ -45,7 +44,7 @@ export const EditableList = <T extends unknown>({
useEffect(() => {
if (pageinationRef.current) {
const paginationLinks = pageinationRef.current.querySelectorAll('.fr-pagination__link')
paginationLinks.forEach((link: HTMLAnchorElement, index) => {
paginationLinks.forEach((link: Element, index) => {
if (index - 1 === page?.current) {
link.setAttribute('aria-current', 'page')
} else {
Expand Down
5 changes: 2 additions & 3 deletions components/modal-alerte.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useEffect} from 'react'
import React, {useEffect} from 'react'
import {createModal} from '@codegouvfr/react-dsfr/Modal'
import {useIsModalOpen} from '@codegouvfr/react-dsfr/Modal/useIsModalOpen'

Expand All @@ -9,12 +9,11 @@ const modal = createModal({

type ModalAlertProps<T> = {
item: T;
setItem: (item: T) => void;
setItem: (item: T | null) => void;
onAction: () => Promise<void>;
title: string;
}

// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-constraint
export const ModalAlert = <T extends unknown>({item, setItem, onAction, title}: ModalAlertProps<T>) => {
const isOpen = useIsModalOpen(modal)

Expand Down
1 change: 0 additions & 1 deletion components/multi-select-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const StyledAutocomplete = styled(Autocomplete)`
}
`

// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-constraint
export const MultiSelectInput = <T extends unknown>({label, placeholder, options, onChange, value, hint, isDisabled}: MultiSelectInputProps<T>) => {
const [searchValue, setSearchValue] = useState('')

Expand Down
1 change: 0 additions & 1 deletion components/partenaires-de-la-charte/partenaire-form.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/naming-convention */
import React, {useState} from 'react'
import styled from 'styled-components'
import type {PartenaireDeLaCharteCommuneType, PartenaireDeLaCharteEntrepriseType, PartenaireDeLaCharteOrganismeType} from 'types/partenaire-de-la-charte'
Expand Down
80 changes: 52 additions & 28 deletions lib/api-mes-adresses.tsx
Original file line number Diff line number Diff line change
@@ -1,54 +1,78 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type {BaseLocaleType} from '../types/mes-adresses'
import type {PageType} from '../types/page'
import type { BaseLocaleType } from "../types/mes-adresses";
import type { PageType } from "../types/page";

const NEXT_PUBLIC_API_MES_ADRESSES = process.env.NEXT_PUBLIC_API_MES_ADRESSES || 'https://api-bal.adresse.data.gouv.fr/v1'
const NEXT_PUBLIC_BAL_ADMIN_URL = process.env.NEXT_PUBLIC_BAL_ADMIN_URL || 'http://localhost:3000'
const PROXY_MES_ADRESSES_API = NEXT_PUBLIC_BAL_ADMIN_URL + '/api/proxy-mes-adresses-api'
const NEXT_PUBLIC_API_MES_ADRESSES =
process.env.NEXT_PUBLIC_API_MES_ADRESSES ||
"https://api-bal.adresse.data.gouv.fr/v2";
const NEXT_PUBLIC_BAL_ADMIN_URL =
process.env.NEXT_PUBLIC_BAL_ADMIN_URL || "http://localhost:3000";
const PROXY_MES_ADRESSES_API =
NEXT_PUBLIC_BAL_ADMIN_URL + "/api/proxy-mes-adresses-api";

export type SearchBasesLocalesParams = {
commune: string;
page: number;
limit: number;
deleted?: number;
}
};

async function processReponse(res: Response) {
if (!res.ok) {
const error = await res.json()
throw new Error(error.message)
const error = await res.json();
throw new Error(error.message);
}

try {
return await res.json()
return await res.json();
} catch {
return res
return res;
}
}

export async function getBaseLocale(baseLocaleId: string): Promise<BaseLocaleType> {
const res: Response = await fetch(`${PROXY_MES_ADRESSES_API}/bases-locales/${baseLocaleId}`)
return processReponse(res)
export async function getBaseLocale(
baseLocaleId: string
): Promise<BaseLocaleType> {
const res: Response = await fetch(
`${PROXY_MES_ADRESSES_API}/bases-locales/${baseLocaleId}`
);
return processReponse(res);
}

export async function removeBaseLocale(baseLocaleId: string) {
const res: Response = await fetch(`${PROXY_MES_ADRESSES_API}/bases-locales/${baseLocaleId}`, {
method: 'DELETE',
})
return processReponse(res)
const res: Response = await fetch(
`${PROXY_MES_ADRESSES_API}/bases-locales/${baseLocaleId}`,
{
method: "DELETE",
}
);
return processReponse(res);
}

export async function searchBasesLocales(query: SearchBasesLocalesParams): Promise<PageType<BaseLocaleType>> {
const {page = 1, limit = 20, deleted = 0} = query
const offset = (page - 1) * limit
const params = {...query, offset, limit, deleted}
const queryString: string = Object.keys(params).map((key: string) => `${key}=${String(params[key])}`).join('&')
export async function searchBasesLocales(
query: SearchBasesLocalesParams
): Promise<PageType<BaseLocaleType>> {
const { page = 1, limit = 20, deleted = 0 } = query;
const offset = (page - 1) * limit;
const params = { ...query, offset, limit, deleted };
const queryString: string = Object.keys(params)
.map((key: string) => `${key}=${String(params[key])}`)
.join("&");

const res: Response = await fetch(`${NEXT_PUBLIC_API_MES_ADRESSES}/bases-locales/search?${queryString}`)
return processReponse(res)
const res: Response = await fetch(
`${NEXT_PUBLIC_API_MES_ADRESSES}/bases-locales/search?${queryString}`
);
return processReponse(res);
}

export async function getStatCreations({from, to}: {from: string; to: string}) {
const res = await fetch(`${NEXT_PUBLIC_API_MES_ADRESSES}/stats/creations?from=${from}&to=${to}`)
return processReponse(res)
export async function getStatCreations({
from,
to,
}: {
from: string;
to: string;
}) {
const res = await fetch(
`${NEXT_PUBLIC_API_MES_ADRESSES}/stats/bals/creations?from=${from}&to=${to}`
);
return processReponse(res);
}
1 change: 0 additions & 1 deletion lib/partenaires-de-la-charte.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {PartenaireDeLaChartType} from 'types/partenaire-de-la-charte'

// eslint-disable-next-line @typescript-eslint/naming-convention
const NEXT_PUBLIC_BAL_ADMIN_URL = process.env.NEXT_PUBLIC_BAL_ADMIN_URL || 'http://localhost:3000'

async function processResponse(response) {
Expand Down
44 changes: 6 additions & 38 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dev": "node server",
"build": "next build",
"start": "NODE_ENV=production node server",
"lint": "xo",
"lint": "eslint --ext .js,.ts,.tsx .",
"predev": "only-include-used-icons",
"prebuild": "only-include-used-icons"
},
Expand Down Expand Up @@ -40,44 +40,12 @@
"devDependencies": {
"@types/react": "^18.2.14",
"dotenv": "^16.0.3",
"eslint-config-xo": "^0.42.0",
"eslint-config-xo-nextjs": "^4.2.1",
"eslint": "^8.54.0",
"eslint-config-next": "^14.0.3",
"eslint-config-prettier": "^9.0.0",
"next-transpile-modules": "^9.0.0",
"xo": "^0.52.3"
},
"xo": {
"extends": "xo-nextjs",
"semicolon": false,
"space": 2,
"envs": [
"browser",
"es2021"
],
"rules": {
"camelcase": "warn",
"unicorn/catch-error-name": "off",
"react/jsx-sort-props": "off",
"import/no-anonymous-default-export": "off",
"unicorn/prevent-abbreviations": "off",
"comma-dangle": "off",
"import/extensions": "off",
"node/prefer-global/process": "off",
"unicorn/no-array-for-each": "off",
"unicorn/prefer-switch": "off",
"n/prefer-global/process": "off",
"react/no-unknown-property": [
"error",
{
"ignore": [
"jsx"
]
}
],
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/triple-slash-reference": "off"
}
"prettier": "^3.1.0",
"typescript": "5.2.2"
},
"engines": {
"node": ">= 18"
Expand Down
1 change: 0 additions & 1 deletion pages/mes-adresses/base-locale/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {formatDate} from '@/lib/util/date'

import CopyToClipBoard from '@/components/copy-to-clipboard'

// eslint-disable-next-line @typescript-eslint/naming-convention
const NEXT_PUBLIC_MES_ADRESSES_URL = process.env.NEXT_PUBLIC_MES_ADRESSES_URL || 'http://mes-adresses.data.gouv.fr'

const BaseLocale = () => {
Expand Down
2 changes: 0 additions & 2 deletions server/migrations/populate-partners.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ async function main() {

main().catch(error => {
console.error(error)
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1)
}).then(() => {
// eslint-disable-next-line unicorn/no-process-exit
process.exit(0)
})
4 changes: 2 additions & 2 deletions server/proxy-mes-adresses-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ const got = require('got')

const w = require('./w')

const API_MES_ADRESSES_URL = process.env.NEXT_PUBLIC_API_MES_ADRESSES || 'https://api-bal.adresse.data.gouv.fr/v1'
const API_MES_ADRESSES_URL = process.env.NEXT_PUBLIC_API_MES_ADRESSES || 'https://api-bal.adresse.data.gouv.fr/v2'
const API_MES_ADDRESSES_TOKEN = process.env.API_MES_ADDRESSES_TOKEN || ''

const client = got.extend({
prefixUrl: API_MES_ADRESSES_URL,
headers: {
authorization: `Token ${API_MES_ADDRESSES_TOKEN}`
authorization: `Bearer ${API_MES_ADDRESSES_TOKEN}`
},
throwHttpErrors: false,
responseType: 'json'
Expand Down
Loading

0 comments on commit 5337bcc

Please sign in to comment.