Skip to content

Commit

Permalink
Simplification de différentes fonctions
Browse files Browse the repository at this point in the history
  • Loading branch information
mariheck committed Jun 17, 2024
1 parent 1edd749 commit 2e422a3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
11 changes: 5 additions & 6 deletions packages/site/app/contact/ContactForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ const initFormData: FormData = {
const ContactForm = () => {
const [formData, setFormData] = useState<FormData>(initFormData);
const [status, setStatus] = useState<'success' | 'error' | null>(null);
const [isContactPanier, setIsContactPanier] = useState(false);

const searchParams = useSearchParams();
const router = useRouter();

const isContactPanier = searchParams.get('panier') === 'true' ? true : false;

const handleChange = (
event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
) => {
Expand Down Expand Up @@ -84,10 +85,7 @@ const ContactForm = () => {
};

useEffect(() => {
const paiContact = searchParams.get('panier') === 'true' ? true : false;
setIsContactPanier(paiContact);

if (paiContact) {
if (isContactPanier) {
const stringToFind = "Informations sur le panier d'actions à impact";
const optionGroup = options.find(opt =>
opt.options.some(o => o.label === stringToFind),
Expand All @@ -106,7 +104,8 @@ const ContactForm = () => {
}));
}
}
}, [searchParams]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return (
<>
Expand Down
24 changes: 10 additions & 14 deletions packages/site/app/faq/ListeQuestions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,21 @@ const ListeQuestions = ({questions}: ListeQuestionsProps) => {
const pathname = usePathname();
const router = useRouter();

const getOngletId = useCallback(() => {
const ongletParam = searchParams.get('onglet');
const ongletId = ongletParam
? onglets.findIndex(onglet => onglet.param === ongletParam)
: 0;
if (ongletId === -1) router.push(`${pathname}?onglet=${onglets[0].param}`);
return ongletId;
}, [pathname, router, searchParams]);

const [currentTab, setCurrentTab] = useState<number>(getOngletId());

useEffect(() => {
setCurrentTab(getOngletId());
}, [searchParams, getOngletId]);
const ongletParam = searchParams.get('onglet');
const currentTab = ongletParam
? onglets.findIndex(onglet => onglet.param === ongletParam)
: 0;

const handleChangeTab = (activeTab: number) => {
router.push(`${pathname}?onglet=${onglets[activeTab].param}`);
};

useEffect(() => {
if (currentTab === -1)
router.push(`${pathname}?onglet=${onglets[0].param}`);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return (
<Tabs
defaultActiveTab={currentTab}
Expand Down
31 changes: 18 additions & 13 deletions packages/site/components/strapiImage/StrapiImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ import {CSSProperties, useState} from 'react';
import Image from 'next/image';
import {StrapiItem} from 'src/strapi/StrapiItem';

const baseURL = process.env.NEXT_PUBLIC_STRAPI_URL;

type Size = 'large' | 'medium' | 'small' | 'thumbnail';

type Format = {[size: string]: {url: string}};

const getImageSrc = (size: Size | undefined, formats: Format, url: string) => {
const baseURL = process.env.NEXT_PUBLIC_STRAPI_URL;

const tempURL = size && formats?.size ? `${formats[size].url}` : `${url}`;

if (tempURL.startsWith('http')) return tempURL;
if (tempURL.startsWith('/')) return `${baseURL}${tempURL}`;
return `${baseURL}/${tempURL}`;
};

type StrapiImageProps = {
data: StrapiItem;
size?: Size;
Expand All @@ -31,22 +41,17 @@ export function StrapiImage({

const attributes = data.attributes;

const url =
size && attributes.formats?.size
? `${attributes.formats[size].url}`
: `${attributes.url}`;
const url = getImageSrc(
size,
attributes.formats as unknown as Format,
attributes.url as unknown as string,
);

return !error ? (
<div className={containerClassName} style={containerStyle}>
<Image
className={classNames('block', className)}
src={
url.startsWith('http')
? url
: url.startsWith('/')
? `${baseURL}${url}`
: `${baseURL}/${url}`
}
src={url}
alt={`${attributes.alternativeText ?? ''}`}
width={attributes.width as unknown as number}
height={attributes.height as unknown as number}
Expand Down

0 comments on commit 2e422a3

Please sign in to comment.