Skip to content

Commit c61aa96

Browse files
committed
Refactor petition-related components and actions
Consolidate petition actions into a single file and update imports accordingly. Deleted redundant components and cleaned up petition handling logic for better maintainability. Took 30 minutes
1 parent adf75e3 commit c61aa96

File tree

14 files changed

+209
-78
lines changed

14 files changed

+209
-78
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use client'
2+
3+
import { SignPetitionButton } from '@/app/petitions/components/SignPetitionButton'
4+
import { useEffect, useState } from 'react'
5+
import { useSession } from 'next-auth/react'
6+
import { checkPetitionSignature } from '@/app/petitions/petitionActions'
7+
8+
const neoBrutalistStyles = {
9+
button: "border-4 border-black p-4 text-lg font-bold transition-all hover:translate-x-1 hover:translate-y-1 hover:shadow-none bg-white shadow-[8px_8px_0px_0px_rgba(0,0,0,1)] text-black min-w-[200px] min-h-[60px] rounded-none",
10+
signed: "border-4 border-black p-4 text-lg font-bold bg-pink-400 shadow-[4px_4px_0px_0px_rgba(0,0,0,1)] hover:bg-pink-500 text-white min-w-[200px] min-h-[60px] rounded-none"
11+
}
12+
13+
export function DFDASignPetitionButton() {
14+
const { data: session } = useSession()
15+
const [hasSigned, setHasSigned] = useState(false)
16+
17+
useEffect(() => {
18+
let mounted = true
19+
20+
async function checkSignature() {
21+
if (!session?.user?.id) {
22+
if (mounted) setHasSigned(false)
23+
return
24+
}
25+
26+
try {
27+
const hasSignature = await checkPetitionSignature('right-to-trial-act')
28+
if (mounted) setHasSigned(hasSignature)
29+
} catch (error) {
30+
console.error('Failed to check signature status:', error)
31+
if (mounted) setHasSigned(false)
32+
}
33+
}
34+
35+
checkSignature()
36+
37+
return () => {
38+
mounted = false
39+
}
40+
}, [session?.user?.id])
41+
42+
return (
43+
<SignPetitionButton
44+
petitionId="right-to-trial-act"
45+
hasSigned={hasSigned}
46+
status="ACTIVE"
47+
className={neoBrutalistStyles.button}
48+
signedClassName={neoBrutalistStyles.signed}
49+
onSignatureChange={() => {
50+
// Toggle the hasSigned state immediately for better UX
51+
setHasSigned(prev => !prev)
52+
}}
53+
/>
54+
)
55+
}

app/dfda/right-to-trial-act/components/PetitionSection.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client'
22

33
import { GitHubEditButton } from './GitHubEditButton'
4-
import { SignPetitionButton } from './SignPetitionButton'
4+
import { DFDASignPetitionButton } from './DFDASignPetitionButton'
55

66
export function PetitionSection() {
77
return (
@@ -10,8 +10,8 @@ export function PetitionSection() {
1010
<p className="text-xl mb-8 text-black">
1111
Sign the petition to support the Right to Trial Act and help end the suffering of billions of people.
1212
</p>
13-
<div className="flex flex-col sm:flex-row gap-4 justify-center">
14-
<SignPetitionButton />
13+
<div className="inline-flex gap-4 justify-center">
14+
<DFDASignPetitionButton />
1515
<GitHubEditButton />
1616
</div>
1717
</div>

app/dfda/right-to-trial-act/components/SignPetitionButton.tsx

Lines changed: 0 additions & 26 deletions
This file was deleted.

app/dfda/right-to-trial-act/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Metadata } from 'next'
22
import { getMarkdownContent } from '@/lib/content/getMarkdownContent'
33
import { PetitionSection } from './components/PetitionSection'
44
import { FloatingPetitionButton } from './components/FloatingPetitionButton'
5-
import { SignPetitionButton } from './components/SignPetitionButton'
5+
import { DFDASignPetitionButton } from './components/DFDASignPetitionButton'
66
import CureAccelerationAct from './components/right-to-trial-act'
77
import { GitHubEditButton } from './components/GitHubEditButton'
88

@@ -50,7 +50,7 @@ export default async function RightToTrialPage() {
5050
{/* Bottom Petition Section */}
5151
<div className="mt-16 text-center border-t-4 pt-12">
5252
<h3 className="text-2xl font-serif mb-6">Add Your Name to Support This Act</h3>
53-
<SignPetitionButton /> <GitHubEditButton />
53+
<DFDASignPetitionButton /> <GitHubEditButton />
5454
</div>
5555
</div>
5656
</div>

app/petitions/components/Comments.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { useState } from 'react'
44
import { useSession } from 'next-auth/react'
55
import { LoginPromptButton } from '@/components/LoginPromptButton'
6-
import { addComment } from '../actions'
6+
import { addComment } from '../petitionActions'
77
import { Button } from '@/components/ui/button'
88
import { Textarea } from '@/components/ui/textarea'
99
import { PetitionComment, User } from "@prisma/client"

app/petitions/components/CreatePetitionForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client'
22

33
import { useState } from "react"
4-
import { createPetition, generatePetition } from "../actions"
4+
import { createPetition, generatePetition } from "../petitionActions"
55
import { useRouter } from "next/navigation"
66
import { Button } from "@/components/ui/button"
77
import { Input } from "@/components/ui/input"

app/petitions/components/FollowButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useState } from 'react'
44
import { useSession } from 'next-auth/react'
55
import { LoginPromptButton } from '@/components/LoginPromptButton'
66
import { Button } from '@/components/ui/button'
7-
import { followPetition, unfollowPetition } from '../actions'
7+
import { followPetition, unfollowPetition } from '../petitionActions'
88

99
export function FollowButton({
1010
petitionId,

app/petitions/components/FollowSettings.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { useState } from 'react'
44
import { Switch } from "@/components/ui/switch"
55
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
6-
import { updateFollowSettings } from '../actions'
6+
import { updateFollowSettings } from '../petitionActions'
77
import { EmailFrequency } from "@prisma/client"
88

99
interface FollowSettings {

app/petitions/components/ImageUpload.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { useState } from 'react'
44
import { Button } from "@/components/ui/button"
5-
import { uploadImage } from '../actions'
5+
import { uploadImage } from '../petitionActions'
66

77
interface ImageUploadProps {
88
onUploadComplete: (url: string) => void

app/petitions/components/PetitionAdminControls.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { useState } from "react"
44
import { Button } from "@/components/ui/button"
55
import { Textarea } from "@/components/ui/textarea"
6-
import { createPetitionUpdate } from "../actions"
6+
import { createPetitionUpdate } from "../petitionActions"
77
import { UpdatesTimeline } from "./UpdatesTimeline"
88
import { Petition, PetitionStatusUpdate } from "@prisma/client"
99

0 commit comments

Comments
 (0)