-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
764d578
commit 0997ca0
Showing
5 changed files
with
154 additions
and
206 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/app/(use-header)/codes/_components/view/Page/Card.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 16 additions & 11 deletions
27
src/app/(use-header)/post/_components/logic/useCrazyScore.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |