Skip to content

Commit 34f99c2

Browse files
committed
feat(content): divide the buttons for displaying attributes and insight
1 parent 0ab33d3 commit 34f99c2

File tree

7 files changed

+84
-58
lines changed

7 files changed

+84
-58
lines changed

__tests__/e2e/articles/article.test.ts

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test, expect } from "@playwright/test";
1+
import { expect, test } from "@playwright/test";
22

33
test.beforeEach(async ({ page }) => {
44
await page.goto("http://localhost:3000/articles/nested/standard/");
@@ -94,19 +94,38 @@ test.describe("Article", () => {
9494
// await expect(tags[1]).toHaveValue('Cats');
9595
});
9696

97-
test("should clickable `Attributes / Insight` button - with screenshot", async ({
97+
test("should clickable `Attributes` button - with screenshot", async ({
9898
page
9999
}, testInfo) => {
100-
const button = await page.locator("main>article>div>div>div>span");
101-
await expect(button).toHaveText("Attributes / Insight ▼");
100+
const buttons = await page.locator("main>article>div>div>div").all();
101+
const button = await buttons[0].locator("span");
102+
await expect(button).toHaveText("Attributes ▼");
102103

103104
await button.click();
104-
const content = await page.locator("main>article>div>div>div>pre");
105+
const content = await page.locator("main>article>div>pre");
105106

106107
// TODO: assert JSON
107108
await expect(content).toContainText("attributes");
108109
// NOTE: content id
109110
await expect(content).toContainText("01h81rme1cy11e3ffgagtym2xh");
111+
112+
const screenshot = await page.screenshot({ fullPage: true });
113+
await testInfo.attach("screenshot", {
114+
body: screenshot,
115+
contentType: "image/png"
116+
});
117+
});
118+
119+
test("should clickable `Insight` button - with screenshot", async ({
120+
page
121+
}, testInfo) => {
122+
const buttons = await page.locator("main>article>div>div>div").all();
123+
const button = await buttons[1].locator("span");
124+
await expect(button).toHaveText("Insight ▼");
125+
126+
await button.click();
127+
const content = await page.locator("main>article>div>pre");
128+
110129
// NOTE: commit hash
111130
await expect(content).toContainText("24af403");
112131

src/components/accordion.tsx

-19
This file was deleted.

src/components/actionbutton.tsx

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import styles from "../styles/actionbutton.module.scss";
2+
3+
export const ActionButton: React.FunctionComponent<{
4+
title: string;
5+
onclick: () => void;
6+
}> = ({ title, onclick }) => {
7+
return (
8+
<div>
9+
<span
10+
className={`'unstyled' ${styles["action-button"]}`}
11+
onClick={onclick}
12+
dangerouslySetInnerHTML={{ __html: title }}
13+
/>
14+
</div>
15+
);
16+
};

src/components/components.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from "./accordion";
1+
export * from "./actionbutton";
22
export * from "./archives";
33
export * from "./articles";
44
export * from "./backtotop";

src/components/content.tsx

+35-31
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,21 @@ import { BackendMeta, Content, ContentMeta, Insight } from "../models/models";
66
import containerStyles from "../styles/components/container.module.scss";
77
import contentStyles from "../styles/components/content.module.scss";
88
import { mergeBackendMeta } from "../utils/insight";
9-
import { Accordion } from "./accordion";
9+
import { ActionButton } from "./actionbutton";
10+
import { PreContent } from "./precontent";
1011

1112
export const ContentComponent: React.FunctionComponent<{
1213
content: Content;
1314
insight: Insight | null;
1415
}> = ({ content, insight }) => {
15-
const [isOpen, setIsOpen] = useState(false);
16-
const [isFetchedBackendMeta, setIsFetchedBackendMeta] = useState(false);
17-
const [metaAndInsight, setData] = useState(null);
16+
const [isAttributesOpen, setIsAttributesOpen] = useState(false);
17+
const [attributes, setAttributes] = useState(null);
1818

19-
const fetchBackendMetaData = async () => {
20-
const response: Response = await fetchSystemMetadata();
21-
let ins = insight;
22-
if (response.status === 200) {
23-
const bm = (await response.json()) as BackendMeta;
24-
ins = mergeBackendMeta(insight, bm);
25-
setIsFetchedBackendMeta(true);
26-
}
19+
const [isMetadataOpen, setIsMetadataOpen] = useState(false);
20+
const [isFetchedBackendMeta, setIsFetchedBackendMeta] = useState(false);
21+
const [metadata, setMetaData] = useState(null);
2722

23+
const formatAttributes = () => {
2824
let actualRobotsMeta = "";
2925
let maybeRobotsMeta = document.querySelector('meta[name="robots"]');
3026
if (maybeRobotsMeta) {
@@ -57,37 +53,45 @@ export const ContentComponent: React.FunctionComponent<{
5753
updatedAt: content.updatedAt
5854
};
5955

60-
setData(
61-
JSON.stringify(
62-
{
63-
attributes: meta,
64-
insight: ins
65-
},
66-
null,
67-
2
68-
)
69-
);
56+
setAttributes(JSON.stringify({ attributes: meta }, null, 2));
57+
};
58+
59+
const fetchBackendMetaData = async () => {
60+
const response: Response = await fetchSystemMetadata();
61+
let ins = insight;
62+
if (response.status === 200) {
63+
const bm = (await response.json()) as BackendMeta;
64+
ins = mergeBackendMeta(insight, bm);
65+
setIsFetchedBackendMeta(true);
66+
}
67+
68+
setMetaData(JSON.stringify({ insight: ins }, null, 2));
69+
};
70+
71+
const toggleAttributes = () => {
72+
formatAttributes();
73+
setIsMetadataOpen(false);
74+
setIsAttributesOpen(!isAttributesOpen);
7075
};
7176

72-
const toggle = () => {
77+
const toggleMetadata = () => {
7378
if (!isFetchedBackendMeta) {
7479
fetchBackendMetaData();
7580
setIsFetchedBackendMeta(true);
7681
}
77-
setIsOpen(!isOpen);
82+
setIsAttributesOpen(false);
83+
setIsMetadataOpen(!isMetadataOpen);
7884
};
7985

8086
return (
8187
<article className={contentStyles.content}>
8288
<div className={containerStyles.container}>
83-
<div className={`${contentStyles["accordion-wrap"]}`}>
84-
<Accordion
85-
open={isOpen}
86-
onclick={toggle}
87-
title="Attributes / Insight ▼"
88-
content={metaAndInsight}
89-
/>
89+
<div className={`${contentStyles["actionbutton-wrap"]}`}>
90+
<ActionButton onclick={toggleAttributes} title="Attributes ▼" />
91+
<ActionButton onclick={toggleMetadata} title="Insight ▼" />
9092
</div>
93+
{isAttributesOpen && <PreContent content={attributes} />}
94+
{isMetadataOpen && <PreContent content={metadata} />}
9195
<div className={`${contentStyles["words"]}`}>
9296
{content.length.toLocaleString()} words
9397
</div>

src/styles/accordion.module.scss renamed to src/styles/actionbutton.module.scss

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
@use "sass:map";
22
@use '_variables';
33

4-
.menu-button {
4+
.action-button {
55
font-size: 1.45rem;
6+
width: 90px;
7+
text-align: center;
68
display: inline-block;
79
color: map.get(variables.$basic-colors, 'base');
810
background: transparent;

src/styles/components/content.module.scss

+5-1
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@
4040
}
4141
}
4242
}
43-
.accordion-wrap {
43+
.actionbutton-wrap {
4444
margin-top: 0.7em;
45+
display: flex;
46+
flex-direction: row;
47+
gap: 1rem;
48+
flex-wrap: wrap;
4549
}
4650
}
4751

0 commit comments

Comments
 (0)