Skip to content

Commit

Permalink
#1 Extract elements to own components
Browse files Browse the repository at this point in the history
  • Loading branch information
markusbink committed Aug 12, 2022
1 parent 96f5de6 commit 8a9e0dc
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 201 deletions.
5 changes: 3 additions & 2 deletions src/components/Discussion.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IEntry, UserRole } from "../types";
import { Entry } from "./Entry";
import { Entry } from "./entry/Entry";

const mockEntries: IEntry[] = [
{
Expand Down Expand Up @@ -30,10 +30,11 @@ export const Discussion = () => {
<span>1.</span> Gliederungspunkt
</div>
<div className="space-y-8">
{mockEntries.map((entry) => (
{mockEntries.map((entry, index) => (
<Entry
key={entry.id}
entry={entry}
isBookmarked={index % 2 === 0}
viewedBy={UserRole.Plaintiff}
/>
))}
Expand Down
199 changes: 0 additions & 199 deletions src/components/Entry.tsx

This file was deleted.

32 changes: 32 additions & 0 deletions src/components/entry/Action.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import cx from "classnames";

interface ActionProps extends React.HTMLAttributes<HTMLDivElement> {
onClick?: (e: React.MouseEvent) => void;
isPlaintiff: boolean;
children: React.ReactNode;
}

export const Action: React.FC<ActionProps> = ({
onClick,
isPlaintiff,
children,
className,
...restProps
}) => {
return (
<div
onClick={onClick}
className={cx(
"rounded-md p-1 cursor-pointer",
{
"hover:bg-darkPurple hover:text-lightPurple": isPlaintiff,
"hover:bg-darkPetrol hover:text-lightPetrol": !isPlaintiff,
},
className
)}
{...restProps}
>
{children}
</div>
);
};
61 changes: 61 additions & 0 deletions src/components/entry/Entry.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import cx from "classnames";
import { ArrowBendLeftUp } from "phosphor-react";
import React, { useState } from "react";
import { IEntry, UserRole } from "../../types";
import { Button } from "../Button";
import { EntryBody } from "./EntryBody";
import { EntryHeader } from "./EntryHeader";

interface EntryProps {
entry: IEntry;
isBookmarked?: boolean;
viewedBy: UserRole;
}

export const Entry: React.FC<EntryProps> = ({
entry,
isBookmarked = false,
viewedBy,
}) => {
const [isExpanded, setIsExpanded] = useState<boolean>(true);

const isJudge = viewedBy === UserRole.Judge;
const isPlaintiff = entry.role === UserRole.Plaintiff;
const isOwnEntry =
(viewedBy === UserRole.Plaintiff && entry.role === "Kläger") ||
(viewedBy === UserRole.Defendant && entry.role === "Beklagter");

const toggleBody = (e: React.MouseEvent) => {
setIsExpanded(!isExpanded);
};

return (
<div className={`flex flex-col ${isPlaintiff ? "" : "items-end"}`}>
<div className="w-1/2">
<div>
<EntryHeader
entry={entry}
isExpanded={isExpanded}
toggleBody={toggleBody}
isBookmarked={isBookmarked}
/>
{isExpanded && (
<EntryBody isPlaintiff={isPlaintiff}>{entry.text}</EntryBody>
)}
</div>
{isJudge || !isOwnEntry ? (
<Button
icon={<ArrowBendLeftUp weight="bold" size={18} />}
size="sm"
bgColor="transparent"
textColor={`font-bold ${
isPlaintiff ? "text-darkPurple" : "text-darkPetrol"
}`}
>
Text verfassen
</Button>
) : null}
</div>
</div>
);
};
22 changes: 22 additions & 0 deletions src/components/entry/EntryBody.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import cx from "classnames";

interface EntryBodyProps {
isPlaintiff: boolean;
children: React.ReactNode;
}

export const EntryBody: React.FC<EntryBodyProps> = ({
isPlaintiff,
children,
}) => {
return (
<div
className={cx("p-6 bg-white rounded-b-lg border border-t-0", {
"border-lightPurple": isPlaintiff,
"border-lightPetrol": !isPlaintiff,
})}
>
<p dangerouslySetInnerHTML={{ __html: children as string }}></p>
</div>
);
};
Loading

0 comments on commit 8a9e0dc

Please sign in to comment.