Skip to content

feat: integrate API into poll creation form & poll details #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 29 additions & 28 deletions app/actions/verify.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
"use server";

import { cookies } from "next/headers";
import { nanoid } from "nanoid";

export async function verifyWalletAndWorldID(formData: FormData) {
const walletPayload = JSON.parse(formData.get("walletPayload") as string);
const worldIdProof = JSON.parse(formData.get("worldIdProof") as string);
const nonce = formData.get("nonce") as string;
export async function getNonce() {
try {
const nonce = nanoid(32);

const siweCookie = cookies().get("siwe")?.value;
cookies().set("siwe_nonce", nonce, {
secure: true,
httpOnly: true,
sameSite: "strict",
maxAge: 600,
});

if (nonce !== siweCookie) {
return {
status: "error",
isValid: false,
message: "Invalid nonce",
};
return { nonce, success: true };
} catch (error) {
console.error("Error generating nonce:", error);
return { error: "Failed to generate nonce", success: false };
}
}

export async function verifyNonceCookie(submittedNonce: string) {
try {
const res = await fetch(`${process.env.BACKEND_URL}/auth/verify-world-id`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ walletPayload, worldIdProof, nonce }),
});
const storedNonce = cookies().get("siwe_nonce")?.value;

if (!storedNonce) {
return { isValid: false, error: "No nonce found in cookies" };
}

const isValid = storedNonce === submittedNonce;

const data = await res.json();
if (isValid) {
cookies().delete("siwe_nonce");
}

return {
status: "success",
data,
};
return { isValid };
} catch (error) {
return {
status: "error",
message: "Server verification failed",
error,
};
console.error("Error verifying nonce:", error);
return { isValid: false, error: "Nonce verification failed" };
}
}
12 changes: 0 additions & 12 deletions app/api/nonce/route.ts

This file was deleted.

13 changes: 12 additions & 1 deletion app/poll/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
"use client";

import PollVoteCard from "@/components/Poll/PollVoteCard";
import Header from "@/components/Header";
import { useParams } from "next/navigation";

export default function PollPage() {
const { id } = useParams();
const idParam = id && Array.isArray(id) ? id[0] : id;
const pollId = Number(idParam);

if (!pollId) {
return <div>Poll not found</div>;
}

return (
<main className="flex-1 bg-white rounded-t-3xl p-5">
<Header backUrl="/polls" />
<PollVoteCard />
<PollVoteCard pollId={pollId} />
</main>
);
}
22 changes: 22 additions & 0 deletions app/poll/[id]/results/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use client";

import PollResultsCard from "@/components/Poll/PollResultsCard";
import Header from "@/components/Header";
import { useParams } from "next/navigation";

export default function PollPage() {
const { id } = useParams();
const idParam = id && Array.isArray(id) ? id[0] : id;
const pollId = Number(idParam);

if (!pollId) {
return <div>Poll not found</div>;
}

return (
<main className="flex-1 bg-white rounded-t-3xl p-5">
<Header backUrl="/polls" />
<PollResultsCard pollId={pollId} />
</main>
);
}
2 changes: 1 addition & 1 deletion components/Login/SplashScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function SplashScreen() {
</div>
</div>
)}
{isLoggingIn || isLoggedIn && (
{(isLoggingIn || isLoggedIn) && (
<div className="flex flex-col items-center justify-center mt-8">
<BlurredCard />
<BlurredCard />
Expand Down
46 changes: 46 additions & 0 deletions components/Modals/ConfirmDeleteModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import BottomModal from "../ui/BottomModal";
import { Button } from "../ui/Button";

type ConfirmDeleteModalProps = {
modalOpen: boolean;
setModalOpen: (open: boolean) => void;
onDelete: () => void;
isLoading: boolean;
};

export default function ConfirmDeleteModal({
modalOpen,
setModalOpen,
onDelete,
isLoading,
}: ConfirmDeleteModalProps) {
if (!modalOpen) return null;

return (
<BottomModal modalOpen={modalOpen} setModalOpen={setModalOpen}>
<h2 className="text-2xl font-semibold text-center text-gray-900 mb-8 text-sora">
Are you sure?
</h2>
<p className="text-center text-gray-700 mb-8 text-sora">
This action is irreversible, and all collected data will be permanently
deleted.
</p>
<div className="flex flex-col gap-4 justify-center">
<Button
className="text-sm font-semibold font-sora"
onClick={onDelete}
disabled={isLoading}
>
{isLoading ? "Deleting..." : "Delete"}
</Button>
<Button
variant="ghost"
className="text-sm font-semibold font-sora text-gray-500"
onClick={() => setModalOpen(false)}
>
Cancel
</Button>
</div>
</BottomModal>
);
}
6 changes: 3 additions & 3 deletions components/Modals/PollCreatedModal.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { useRouter } from "next/navigation";
import Image from "next/image";
import { ShareIcon } from "../icon-components";
import { handleShare } from "@/utils/share";
import { handleSharePoll } from "@/utils/share";
import { Modal } from "../ui/Modal";
import { Button } from "../ui/Button";

interface IModalProps {
open: boolean;
onOpenChange: (open: boolean) => void;
pollTitle: string;
pollId: number;
pollId: number | undefined;
}

export default function PollCreatedModal({
Expand Down Expand Up @@ -48,7 +48,7 @@ export default function PollCreatedModal({
<Button
variant="primary"
className="w-full flex items-center justify-center gap-2 font-medium"
onClick={() => handleShare(pollTitle, pollId)}
onClick={() => handleSharePoll(pollTitle, pollId!)}
>
<ShareIcon size={24} color="white" />
Share this Poll
Expand Down
4 changes: 2 additions & 2 deletions components/Modals/VotingSuccessModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Image from "next/image";
import { ShareIcon } from "../icon-components";
import { handleShare } from "@/utils/share";
import { handleSharePoll } from "@/utils/share";

interface IModalProps {
setShowModal: (showModal: boolean) => void;
Expand Down Expand Up @@ -44,7 +44,7 @@ export default function VotingSuccessModal({

<button
className="w-full flex items-center justify-center gap-2 text-gray-500 py-4 rounded-xl font-semibold font-sora"
onClick={() => handleShare(pollTitle, pollId)}
onClick={() => handleSharePoll(pollTitle, pollId)}
>
<ShareIcon size={24} />
Share
Expand Down
Loading