Skip to content

Commit

Permalink
Spaghetti bowl (#1014)
Browse files Browse the repository at this point in the history
* fix: activité inconnue ()

* fix: MultChoice

* feat: create dedicated pages for modifications
  • Loading branch information
XavierJp committed Apr 24, 2024
1 parent eabf512 commit cdc24f9
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 105 deletions.
27 changes: 8 additions & 19 deletions app/(header-default)/faq/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ import ButtonLink from '#components-ui/button';
import TextWrapper from '#components-ui/text-wrapper';
import { RenderMarkdownServerOnly } from '#components/markdown';
import { allFaqArticles, getFaqArticle } from '#models/article/faq';
import { InternalError } from '#models/exceptions';
import { AppRouterProps } from '#utils/server-side-helper/app/extract-params';
import { redirectFAQPageNotFound } from '#utils/server-side-helper/app/redirect-faq-not-found';

type IParams = {
slug: string;
};

export default async function FAQArticle({ params }: { params: IParams }) {
const slug = params.slug;
const article = getFaqArticle(slug);
export default async function FAQArticle(props: AppRouterProps) {
const article = getFaqArticle(props.params.slug);
if (!article) {
throw new InternalError({
message: 'Article not found',
context: params,
});
return redirectFAQPageNotFound(props.params.slug);
}

return (
<>
<TextWrapper>
Expand Down Expand Up @@ -69,17 +65,10 @@ export async function generateStaticParams(): Promise<Array<IParams>> {
});
}

export const generateMetadata = function ({
params,
}: {
params: IParams;
}): Metadata {
const article = getFaqArticle(params.slug);
export const generateMetadata = function (props: AppRouterProps): Metadata {
const article = getFaqArticle(props.params.slug);
if (!article) {
throw new InternalError({
message: 'Article not found',
context: params,
});
return redirectFAQPageNotFound(props.params.slug);
}
return {
title: article.seo.title || article.title,
Expand Down
96 changes: 96 additions & 0 deletions app/(header-default)/faq/modifier/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Metadata } from 'next';
import Breadcrumb from '#components-ui/breadcrumb';
import ButtonLink from '#components-ui/button';
import TextWrapper from '#components-ui/text-wrapper';
import {
allDataToModify,
getDataToModify,
} from '#models/administrations/data-to-modify';
import { AppRouterProps } from '#utils/server-side-helper/app/extract-params';
import { redirectFAQPageNotFound } from '#utils/server-side-helper/app/redirect-faq-not-found';

type IParams = {
slug: string;
};

export default async function FAQArticle(props: AppRouterProps) {
const dataToModify = getDataToModify(props.params.slug);
if (!dataToModify) {
return redirectFAQPageNotFound(props.params.slug);
}
return (
<>
<TextWrapper>
<Breadcrumb
links={[
{ href: '/faq', label: 'Questions fréquentes' },
{ href: '/faq', label: 'Comment modifier ces informations' },

{ href: '', label: dataToModify.label },
]}
/>
<h1>
Comment modifier : “<strong>{dataToModify.label}</strong>” ?
</h1>
<p></p>
<p>Ces informations proviennent de :</p>
<ul>
<li>
Source de la donnée :{' '}
<a href={dataToModify.datagouvLink}>{dataToModify.dataSource}</a>
</li>
<li>
Service responsable :{' '}
<a href={dataToModify.site}>{dataToModify.long}</a>.
</li>
</ul>
{dataToModify.form ? (
<>
<p>Cette administration propose une démarche en ligne&nbsp;:</p>
<ButtonLink to={dataToModify.form}>
Accéder à la démarche en ligne
</ButtonLink>
</>
) : (
<>
<br />
<ButtonLink to={dataToModify.contact}>
Contacter le service ({dataToModify.short})
</ButtonLink>
</>
)}

<h2>Vous ne trouvez pas votre réponse ?</h2>
<div className="layout-left">
<ButtonLink to="/faq" alt small>
Consultez notre FAQ
</ButtonLink>
</div>
</TextWrapper>
</>
);
}

export async function generateStaticParams(): Promise<Array<IParams>> {
return allDataToModify
.filter(({ slug }) => !!slug)
.map(({ slug }) => {
return {
slug,
};
});
}

export const generateMetadata = function (props: AppRouterProps): Metadata {
const dataToModify = getDataToModify(props.params.slug);
if (!dataToModify) {
return redirectFAQPageNotFound(props.params.slug);
}
return {
title: dataToModify.label,
robots: 'index, follow',
alternates: {
canonical: `https://annuaire-entreprises.data.gouv.fr/faq/modifier/${dataToModify.slug}`,
},
};
};
10 changes: 10 additions & 0 deletions app/(header-default)/faq/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { Metadata } from 'next';
import TextWrapper from '#components-ui/text-wrapper';
import parseMarkdownSync from '#components/markdown/parse-markdown';
import StructuredDataFAQ from '#components/structured-data/faq';
import { allDataToModify } from '#models/administrations/data-to-modify';
import { allFaqArticles } from '#models/article/faq';

export default function FAQPage() {
const articles = allFaqArticles;

return (
<>
<StructuredDataFAQ
Expand All @@ -23,6 +25,14 @@ export default function FAQPage() {
</li>
))}
</ul>
<h2>Comment modifier les informations d’une entreprise ?</h2>
<ul>
{allDataToModify.map(({ label, slug }) => (
<li key={slug}>
<a href={`/faq/modifier/${slug}`}>{label}</a>
</li>
))}
</ul>
</TextWrapper>
</>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use client';

import { useRef, useState } from 'react';
import { MultiChoice } from '#components-ui/multi-choice';
import Question, { EQuestionType } from '#components/faq-parcours/question';
Expand Down
9 changes: 5 additions & 4 deletions components-ui/multi-choice/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use client';

import { ChangeEventHandler } from 'react';
import styles from './style.module.css';

export type IProps = {
values: {
label: string;
value?: string;
onClick?: Function;
onClick?: ChangeEventHandler<HTMLInputElement>;
href?: string;
checked?: boolean;
}[];
Expand Down Expand Up @@ -60,9 +61,9 @@ export const MultiChoice: React.FC<IProps> = ({
name={name}
value={value}
required={required}
//@ts-ignore
onChange={!!onClick ? onClick : () => null}
defaultChecked={checked}
onChange={!!onClick ? onClick : undefined}
checked={!!onClick ? checked : undefined}
defaultChecked={!!onClick ? undefined : checked}
tabIndex={-1}
/>
<label
Expand Down
56 changes: 3 additions & 53 deletions components/faq-parcours/question.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { PropsWithChildren, useEffect, useRef, useState } from 'react';
import ButtonLink from '#components-ui/button';
import { MultiChoice } from '#components-ui/multi-choice';
import TextWrapper from '#components-ui/text-wrapper';
import MatomoEvent from '#components/matomo-event';
import { allData } from '#models/administrations';
import { allDataToModify } from '#models/administrations/data-to-modify';
import { IFaqArticle } from '#models/article/faq';
import constants from '#models/constants';
export enum EQuestionType {
Expand Down Expand Up @@ -114,7 +113,7 @@ export default function Question({
</p>
<MultiChoice
idPrefix="modification"
values={allData
values={allDataToModify
.filter((data) => {
if (userType === 'agent' || userType === 'all') {
return true;
Expand All @@ -127,60 +126,11 @@ export default function Question({
})
.map((data) => {
return {
onClick: () => selectDataToModify(data),
checked: data.label === dataToModify.label,
href: `/faq/modifier/${data.slug}`,
label: data.label,
};
})}
/>
{dataToModify && (
<Answer>
<span ref={bottomRef} />
<MatomoEvent
category="parcours:modification"
action={userType}
name={dataToModify.label}
/>
Comment modifier les informations suivantes ?
<p>
<strong>{dataToModify.label}</strong>
</p>
<p>Ces informations proviennent de :</p>
<ul>
<li>
Source de la donnée :{' '}
<a href={dataToModify.datagouvLink}>
{dataToModify.dataSource}
</a>
</li>
<li>
Service responsable :{' '}
<a href={dataToModify.site}>{dataToModify.long}</a>.
</li>
</ul>
{dataToModify.form ? (
<>
<p>
Cette administration propose une démarche en ligne&nbsp;:
</p>
<div className="layout-center">
<ButtonLink to={dataToModify.form}>
Accéder à la démarche en ligne
</ButtonLink>
</div>
</>
) : (
<>
<br />
<div className="layout-center">
<ButtonLink to={dataToModify.contact}>
Contacter le service ({dataToModify.short})
</ButtonLink>
</div>
</>
)}
</Answer>
)}
</>
);
case EQuestionType.ALL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
@media only screen and (min-width: 1px) and (max-width: 576px) {
.advanced-search-tutorial > svg {
rotate: 225deg;
height: 60px;
position: absolute;
top: -40px;
height: 70px !important;
top: -45px !important;
}

.advanced-search-tutorial > div {
Expand Down
3 changes: 2 additions & 1 deletion components/search-results/results-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ const ResultItem: React.FC<{
)}
</div>
<div>
{result.libelleActivitePrincipale} ({result.activitePrincipale})
{result.libelleActivitePrincipale}{' '}
{result.activitePrincipale ? `(${result.activitePrincipale})` : null}
</div>
<DirigeantsOrElusList
dirigeantsOrElus={
Expand Down
34 changes: 34 additions & 0 deletions models/administrations/data-to-modify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { slugify } from '#utils/helpers';
import { administrationsMetaData } from '.';

const loadDataToModify = () => {
return Object.values(administrationsMetaData).flatMap(
({ dataSources, contact, site, long, short }) => {
return dataSources.flatMap((datasource) => {
return (datasource.data || []).map(
({ label, form = '', targets = [] }) => {
const slug = slugify(label);
return {
label: label,
slug,
dataSource: datasource.label,
datagouvLink: datasource.datagouvLink,
targets,
form,
contact,
site,
long,
short,
};
}
);
});
}
);
};

export const getDataToModify = (slug: string) => {
return allDataToModify.find((data) => data.slug === slug);
};

export const allDataToModify = loadDataToModify();
22 changes: 0 additions & 22 deletions models/administrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,4 @@ const loadMonitoringIds = () =>

export const administrationsMetaData: IAdministrationsMetaData = loadMetadata();

export const allData = Object.values(administrationsMetaData).flatMap(
({ dataSources, contact, site, long, short }) => {
return dataSources.flatMap((datasource) => {
return (datasource.data || []).map(
({ label, form = '', targets = [] }) => {
return {
label: label,
dataSource: datasource.label,
datagouvLink: datasource.datagouvLink,
targets,
form,
contact,
site,
long,
short,
};
}
);
});
}
);

export const allMonitoringIds = loadMonitoringIds();
14 changes: 14 additions & 0 deletions utils/helpers/formatting/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,20 @@ export const removeSpecialChars = (term = '') => {
.replace(/^\s+/, '');
};

/**
* Turn a string into a slug for URL use
*/
export const slugify = (text: string) =>
text
.toString()
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.toLowerCase()
.trim()
.replace(/\s+/g, '-')
.replace(/[^\w-]+/g, '')
.replace(/--+/g, '-');

/**
* Removes whitespace
*/
Expand Down
Loading

0 comments on commit cdc24f9

Please sign in to comment.