Skip to content

Commit

Permalink
feat: 鑑賞画面のロジック結合
Browse files Browse the repository at this point in the history
  • Loading branch information
SatooRu65536 committed Nov 5, 2024
1 parent 764d578 commit 0997ca0
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 206 deletions.
69 changes: 69 additions & 0 deletions src/app/(use-header)/codes/_components/view/Page/Card.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
.container {
padding: 20px 50px;
border: solid 1.5px #333; /*線の種類・太さ・色*/
width: 100%;
max-width: 800px;
border-radius: 10px; /*角の丸み*/

.tags {
display: flex;
justify-content: start;
p {
padding: 0.5px 15px;
border: solid 2px #333; /*線の種類・太さ・色*/
border-radius: 10px; /*角の丸み*/
}
}

p,
img,
details {
margin: 5px;
}

img {
width: 36px;
height: 36px;
clip-path: circle(50%);
}

.userAndValue {
display: flex;
justify-content: space-between;
align-items: center;

.user {
display: flex;
align-items: center;
}
}

.score_box {
display: flex;
p {
padding-top: 5px;
padding-left: 25px;
}
}

.buttons {
display: flex;
gap: 20px;

button {
background-color: transparent;
border: none;
cursor: pointer;
outline: none;
padding: 0;
appearance: none;

&[data-active='true'] {
border-bottom: 1.5px solid;
}
&:hover {
border-bottom: 1.5px solid;
}
}
}
}
62 changes: 62 additions & 0 deletions src/app/(use-header)/codes/_components/view/Page/Card.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use client';

import { useCallback } from 'react';
import styles from './Card.module.scss';
import { useCrazyScore } from '@/app/(use-header)/post/_components/logic/useCrazyScore';
import CodeArea from '@/components/CodeArea';
import { type Post } from '@/types/post';

interface CardProps {
post: Post;
}

const CRAZY_SCORE = [
{ text: 'クソだね', n: 3 },
{ text: 'よくないね', n: 2 },
{ text: 'ふーん', n: 1 },
{ text: 'へぇー', n: 0 },
] as const;

export function Card({ post }: CardProps) {
const [score, setScore] = useCrazyScore(post.id);

const handleCLick = useCallback((newScore: 0 | 1 | 2 | 3) => {
setScore(newScore);
}, []);

return (
<div key={post.id} className={styles.container}>
<h1>{post.title}</h1>
<div className={styles.tags}>
{post.post_tags.map((tag) => (
<p key={tag.id}>{tag.tag}</p>
))}
</div>
<p>{post.description}</p>
<CodeArea code={post.code} language={post.language.name.toLowerCase()} />
<div className={styles.userAndValue}>
<div className={styles.user}>
<img
alt="user_icon"
src="https://img.esa.io/uploads/production/attachments/19973/2024/10/22/148910/a3ad87c4-103f-4a77-8de4-bb1c89b42a38.png"
/>
<p>{post.user.name}</p>
</div>
<div className={styles.buttons}>
{CRAZY_SCORE.map(({ text, n }) => (
<button
key={text}
data-active={score === n}
onClick={() => {
handleCLick(n);
}}
type="button"
>
{text}
</button>
))}
</div>
</div>
</div>
);
}
67 changes: 0 additions & 67 deletions src/app/(use-header)/codes/_components/view/Page/index.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,4 @@
flex-direction: column;
align-items: center;
gap: 25px;

.container {
padding: 20px 50px;
border: solid 1.5px #333; /*線の種類・太さ・色*/
width: 100%;
max-width: 800px;
border-radius: 10px; /*角の丸み*/

.tags {
display: flex;
justify-content: start;
p {
padding: 0.5px 15px;
border: solid 2px #333; /*線の種類・太さ・色*/
border-radius: 10px; /*角の丸み*/
}
}

p,
img,
details {
margin: 5px;
}

img {
width: 36px;
height: 36px;
clip-path: circle(50%);
}

.userAndValue {
display: flex;
justify-content: space-between;
align-items: center;

.user {
display: flex;
align-items: center;
}
}

.score_box {
display: flex;
p {
padding-top: 5px;
padding-left: 25px;
}
}

.buttons {
display: flex;
gap: 20px;

button {
background-color: transparent;
border: none;
cursor: pointer;
outline: none;
padding: 0;
appearance: none;
}

:hover {
border-bottom: 1.5px solid;
}
}
}
}
135 changes: 7 additions & 128 deletions src/app/(use-header)/codes/_components/view/Page/index.tsx
Original file line number Diff line number Diff line change
@@ -1,100 +1,13 @@
import styles from './index.module.scss';
import CodeArea from '@/components/CodeArea';
import { type Post } from '@/types/post';
'use client';

const articles: Post[] = [
{
id: 1,
code: 'var num;',
description: 'varは使わない方がいいらしいね',
title: 'jsでvar使ってみた',
crazy_score: 3.4,
post_tags: [
{ id: 2, post_id: 4, tag: 'var' },
{ id: 1, post_id: 2, tag: 'js' },
],
user: {
id: 1,
name: 'へのへのもへじ',
uid: 'ghjkjcfhvbnfalzbsdvgfakndkvadaaf',
},
language: { id: 3, name: 'JavaScript' },
},
{
id: 2,
// eslint-disable-next-line no-template-curly-in-string
code: "const [firstName, setFirstName] = useState(''); const [lastName, setLastName] = useState(''); const [fullName, setFullName] = useState('');\n \n useEffect(() => {\n setFullName(`${firstName} ${lastName}`);}, [firstName, lastName]);",
description: 'stateに依存するものはstateに突っ込んでuseEffectで変更しよう!',
title: 'useEffectの使い方',
crazy_score: 1.4,
post_tags: [
{ id: 4, post_id: 4, tag: 'react' },
{ id: 4, post_id: 4, tag: 'react' },
],
user: {
id: 2,
name: '愛工太郎',
uid: 'fgahjksdnfgvaofawesdrf',
},
language: { id: 3, name: 'JavaScript' },
},
{
id: 3,
// eslint-disable-next-line no-template-curly-in-string
code: "const [firstName, setFirstName] = useState('');\n const [lastName, setLastName] = useState('');\n const [fullName, setFullName] = useState('');\n \n useEffect(() => {\n setFullName(`${firstName} ${lastName}`);\n }, [firstName, lastName]);",
description: 'stateに依存するものはstateに突っ込んでuseEffectで変更しよう!',
title: 'useEffectの使い方',
crazy_score: 1.4,
post_tags: [
{ id: 4, post_id: 4, tag: 'react' },
{ id: 4, post_id: 4, tag: 'react' },
],
user: {
id: 2,
name: '愛工太郎',
uid: 'fgahjksdnfgvaofawesdrf',
},
language: { id: 3, name: 'JavaScript' },
},
{
id: 4,
// eslint-disable-next-line no-template-curly-in-string
code: "const [firstName, setFirstName] = useState('');\n const [lastName, setLastName] = useState('');\n const [fullName, setFullName] = useState('');\n \n useEffect(() => {\n setFullName(`${firstName} ${lastName}`);\n }, [firstName, lastName]);",
description: 'stateに依存するものはstateに突っ込んでuseEffectで変更しよう!',
title: 'useEffectの使い方',
crazy_score: 1.4,
post_tags: [
{ id: 4, post_id: 4, tag: 'react' },
{ id: 4, post_id: 4, tag: 'react' },
],
user: {
id: 2,
name: '愛工太郎',
uid: 'fgahjksdnfgvaofawesdrf',
},
language: { id: 3, name: 'JavaScript' },
},
{
id: 5,
// eslint-disable-next-line no-template-curly-in-string
code: "const [firstName, setFirstName] = useState('');\n const [lastName, setLastName] = useState('');\n const [fullName, setFullName] = useState('');\n \n useEffect(() => {\n setFullName(`${firstName} ${lastName}`);\n }, [firstName, lastName]);",
description: 'stateに依存するものはstateに突っ込んでuseEffectで変更しよう!',
title: 'useEffectの使い方',
crazy_score: 1.4,
post_tags: [
{ id: 4, post_id: 4, tag: 'react' },
{ id: 4, post_id: 4, tag: 'react' },
],
user: {
id: 2,
name: '愛工太郎',
uid: 'fgahjksdnfgvaofawesdrf',
},
language: { id: 3, name: 'JavaScript' },
},
];
import { useAtomValue } from 'jotai';
import { Card } from './Card';
import styles from './index.module.scss';
import { postListAtom } from '@/stores/postListAtom';

export default function Page() {
const articles = useAtomValue(postListAtom);

return (
<div className={styles.containers}>
{articles.map((post) => (
Expand All @@ -103,37 +16,3 @@ export default function Page() {
</div>
);
}

interface CardProps {
post: Post;
}

function Card({ post }: CardProps) {
return (
<div key={post.id} className={styles.container}>
<h1>{post.title}</h1>
<div className={styles.tags}>
{post.post_tags.map((tag) => (
<p key={tag.id}>{tag.tag}</p>
))}
</div>
<p>{post.description}</p>
<CodeArea code={post.code} language={post.language.name.toLowerCase()} />
<div className={styles.userAndValue}>
<div className={styles.user}>
<img
alt="user_icon"
src="https://img.esa.io/uploads/production/attachments/19973/2024/10/22/148910/a3ad87c4-103f-4a77-8de4-bb1c89b42a38.png"
/>
<p>{post.user.name}</p>
</div>
<div className={styles.buttons}>
<button type="button">クソだね</button>
<button type="button">よくないね</button>
<button type="button">ふーん</button>
<button type="button">へぇ、いいじゃん</button>
</div>
</div>
</div>
);
}
27 changes: 16 additions & 11 deletions src/app/(use-header)/post/_components/logic/useCrazyScore.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import { useAtomValue } from 'jotai';
import { useState, useEffect } from 'react';
import { authAtom } from '@/stores/authAtom';
import upsertCrazyScore from '@/utils/upsertCrazyScore';

type CrazyScore = 0 | 1 | 2 | 3;

export const useCrazyScore = () => {
const [value, setValue] = useState<CrazyScore>();
export const useCrazyScore = (postId: number) => {
const auth = useAtomValue(authAtom);
const [score, setScore] = useState<CrazyScore>();

const updateValue = (newValue: CrazyScore) => {
if (newValue === value) {
setValue(undefined);
} else {
setValue(newValue);
}
const setScore_ = (newScore: CrazyScore) => {
setScore(newScore);
};

useEffect(() => {
console.log('value:', value); // eslint-disable-line no-console
}, [value]);
void (async () => {
if (auth === undefined) return;
if (score === undefined) return;

return { value, updateValue };
await upsertCrazyScore({ post_id: postId, score, checked_user_uid: auth.uid });
})();
}, [score]);

return [score, setScore_] as const;
};

0 comments on commit 0997ca0

Please sign in to comment.