Skip to content

Commit

Permalink
feat: Finished poll (#161)
Browse files Browse the repository at this point in the history
* feat: polls updates

* feat: poll results

* update: dependencies

* fix: votes test

* fix: wrap searchParams in Suspense
  • Loading branch information
userMeh authored Jun 4, 2024
1 parent 221dd61 commit 25c3459
Show file tree
Hide file tree
Showing 30 changed files with 706 additions and 378 deletions.
6 changes: 3 additions & 3 deletions apps/admin/app/(dashboard)/dashboard/activities/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function ShowContent({ sports, addresses }: { sports: Sport[]; addresses: Addres
setMaxPage(Math.ceil(data.count / 10));
})
.catch((error: Error) => {
console.log(error);
console.error(error);
});
}, 500);

Expand Down Expand Up @@ -189,7 +189,7 @@ export default function Page(): JSX.Element {
setSports(sportsArray);
})
.catch((error: Error) => {
console.log(error);
console.error(error);
});

fetch(`${urlApi}/addresses?${queryParams}`, {
Expand Down Expand Up @@ -218,7 +218,7 @@ export default function Page(): JSX.Element {
setAddresses(addressesArray);
})
.catch((error: Error) => {
console.log(error);
console.error(error);
});
}, [router]);

Expand Down
2 changes: 1 addition & 1 deletion apps/admin/app/(dashboard)/dashboard/addresses/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function ShowContent() {
setMaxPage(Math.ceil(data.count / 10));
})
.catch((error: Error) => {
console.log(error);
console.error(error);
});
}, 500);

Expand Down
4 changes: 2 additions & 2 deletions apps/admin/app/(dashboard)/dashboard/materials/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function ShowContent({ addresses }: { addresses: Address[] }): JSX.Element {
setMaterials(data.data);
})
.catch((error: Error) => {
console.log(error);
console.error(error);
});
}, 500);

Expand Down Expand Up @@ -167,7 +167,7 @@ export default function Page(): JSX.Element {
setAddresses(addressesArray);
})
.catch((error: Error) => {
console.log(error);
console.error(error);
});
}, [router]);

Expand Down
6 changes: 3 additions & 3 deletions apps/admin/app/(dashboard)/dashboard/posts/details/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function ShowReports() {
setPost(data);
})
.catch((error) => {
console.log(error);
console.error(error);
});

const queryParams = new URLSearchParams({
Expand All @@ -108,7 +108,7 @@ function ShowReports() {
setMaxPage(Math.ceil(data.count / 10));
})
.catch((error) => {
console.log(error);
console.error(error);
});

fetch(`${urlApi}/reasons`, {
Expand All @@ -127,7 +127,7 @@ function ShowReports() {
setReasons(data);
})
.catch((error) => {
console.log(error);
console.error(error);
});
}, [idPost, router, page]);

Expand Down
2 changes: 1 addition & 1 deletion apps/admin/app/(dashboard)/dashboard/posts/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function ShowContent() {
setMaxPage(Math.ceil(data.count / 10));
})
.catch((error: Error) => {
console.log(error);
console.error(error);
});
}, 500);

Expand Down
4 changes: 2 additions & 2 deletions apps/admin/app/(dashboard)/dashboard/tournaments/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function ShowContent({ addresses }: { addresses: Address[] }): JSX.Element {
setMaxPage(Math.ceil(data.count / 10));
})
.catch((error: Error) => {
console.log(error);
console.error(error);
});
}, 500);

Expand Down Expand Up @@ -147,7 +147,7 @@ function page() {
setAddresses(data.data);
})
.catch((error: Error) => {
console.log(error);
console.error(error);
});
}, [router.push, urlApi]);

Expand Down
76 changes: 76 additions & 0 deletions apps/admin/app/(dashboard)/dashboard/votes/details/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use client';

import type { Vote } from '@/app/(dashboard)/dashboard/votes/page';
import { Progress } from '@repo/ui/components/ui/progress';
import { useRouter, useSearchParams } from 'next/navigation';
import React, { Suspense, useEffect, useState } from 'react';

function ShowContent() {
const searchParams = useSearchParams();
const router = useRouter();
const idPoll = searchParams.get('id');
const [results, setResults] = useState<{ id: number; votes: number; content: string }[]>([]);
const [title, setTitle] = useState<string>('');
const [totalVotes, setTotalVotes] = useState<number>(0);

useEffect(() => {
const urlApi = process.env.NEXT_PUBLIC_API_URL;
if (!idPoll) {
router.push('/dashboard/votes');
}

fetch(`${urlApi}/polls/${idPoll}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${localStorage.getItem('access_token')}`,
},
})
.then(async (response) => {
if (response.status === 403) {
router.push('/');
}
return response.json();
})
.then((data: Vote) => {
setTitle(data.title);
setResults(data.results);
setTotalVotes(data.results.reduce((acc, result) => acc + result.votes, 0));
})
.catch((error: Error) => {
console.error(error);
});
}, [router, idPoll]);

return (
<>
<div className="flex items-center gap-5">
<h1 className="text-lg font-semibold md:text-2xl">Vote: {title}</h1>
</div>
<div className="flex flex-1 rounded-lg border border-dashed shadow-sm p-4" x-chunk="dashboard-02-chunk-1">
<div className="grid gap-4 w-full">
{results.map((result) => (
<div key={result.id} className="flex justify-center flex-col gap-4 p-4">
<div>
{result.content} ({((result.votes / totalVotes) * 100).toFixed(2)} %)
</div>
<Progress value={(result.votes / totalVotes) * 100} />
</div>
))}
</div>
</div>
</>
);
}

function page() {
return (
<main className="flex flex-1 flex-col gap-4 p-4 lg:gap-6 lg:p-6 h-full">
<Suspense fallback={<div>Chargement...</div>}>
<ShowContent />
</Suspense>
</main>
);
}

export default page;
Loading

0 comments on commit 25c3459

Please sign in to comment.