Skip to content

Commit

Permalink
Trim school description in overview section (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
micorix authored May 14, 2023
1 parent 4d58cc7 commit 0dea861
Showing 1 changed file with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC } from 'react';
import { FC, useState } from 'react';
import dynamic from 'next/dynamic';
import { MdHome } from '@react-icons/all-files/md/MdHome';
import { MdLink } from '@react-icons/all-files/md/MdLink';
Expand Down Expand Up @@ -29,13 +29,32 @@ const ItemWithIcon: FC<ItemWithIconProps> = ({ children, icon: Icon }) => (
</span>
);

const SchoolDescription = ({ description }) => {
interface SchoolDescriptionProps {
description: string;
}
const SchoolDescription: FC<SchoolDescriptionProps> = ({ description }) => {
const [isWholeContentVisible, setIsWholeContentVisible] = useState(false);
const shouldTrim = description.length > 200;
const content =
isWholeContentVisible || !shouldTrim ? description : `${description.slice(0, 200)}...`;
const buttonText = isWholeContentVisible ? 'Pokaż mniej' : 'Pokaż więcej';

return (
<div className="border-t border-light py-2 px-5">
<h4 className="text-dark text-base font-semibold">O szkole</h4>
<div className="my-2 mx-auto prose max-w-none">
<ReactMarkdown>{description}</ReactMarkdown>
<ReactMarkdown>{content}</ReactMarkdown>
</div>
{shouldTrim && (
<div>
<button
className="underline hover:text-gray-700"
onClick={() => setIsWholeContentVisible((v) => !v)}
>
{buttonText}
</button>
</div>
)}
</div>
);
};
Expand Down Expand Up @@ -92,7 +111,9 @@ const OverviewSection: FC<SectionComponentProps> = ({ school }) => {
</div>
</div>
</div>
{school.description && <SchoolDescription description={school.description} />}
{school.description && school.description.trim().length > 0 && (
<SchoolDescription description={school.description.trim()} />
)}
</SchoolInfoSection>
);
};
Expand Down

0 comments on commit 0dea861

Please sign in to comment.