-
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.
Merge pull request #5 from MinaFoundation/feature/dockerfile-2
feat: get proposal data via HTTP
- Loading branch information
Showing
8 changed files
with
100 additions
and
119 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "", | ||
"name": "pgtgovbotocvfrontend", | ||
"type": "module", | ||
"version": "0.0.1", | ||
"scripts": { | ||
|
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
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,32 +1,33 @@ | ||
import type { APIRoute } from 'astro'; | ||
import Proposal from '../../../models/Proposal'; | ||
import type { APIRoute } from "astro"; | ||
import { fetchProposal } from "../../../services/apiService"; | ||
|
||
export const get: APIRoute = async ({ params }) => { | ||
export const GET: APIRoute = async ({ params }) => { | ||
const { id } = params; | ||
|
||
|
||
if (!id || isNaN(parseInt(id))) { | ||
return new Response(JSON.stringify({ error: "Invalid proposal ID" }), { | ||
status: 400, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
} | ||
|
||
try { | ||
const proposal = await Proposal.findByPk(id); | ||
if (proposal) { | ||
return new Response(JSON.stringify(proposal), { | ||
status: 200, | ||
headers: { | ||
"Content-Type": "application/json" | ||
} | ||
}); | ||
} else { | ||
return new Response(JSON.stringify({ error: 'Proposal not found' }), { | ||
status: 404, | ||
headers: { | ||
"Content-Type": "application/json" | ||
} | ||
}); | ||
} | ||
const proposal = await fetchProposal(parseInt(id)); | ||
return new Response(JSON.stringify(proposal), { | ||
status: 200, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
} catch (error) { | ||
return new Response(JSON.stringify({ error: 'Internal Server Error' }), { | ||
console.error("Error fetching proposal:", error); | ||
return new Response(JSON.stringify({ error: "Failed to fetch proposal" }), { | ||
status: 500, | ||
headers: { | ||
"Content-Type": "application/json" | ||
} | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
} | ||
} | ||
}; |
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,43 +1,46 @@ | ||
--- | ||
import MainLayout from '../layouts/MainLayout.astro'; | ||
import WalletConnectButton from '../components/WalletConnectButton.astro'; | ||
import NetworkSwitch from '../components/NetworkSwitch.astro'; | ||
import Proposal from '../models/Proposal'; | ||
import MainLayout from "../layouts/MainLayout.astro"; | ||
import WalletConnectButton from "../components/WalletConnectButton.astro"; | ||
import NetworkSwitch from "../components/NetworkSwitch.astro"; | ||
import { fetchActiveProposals } from "../services/apiService"; | ||
import type { ProposalAttributes } from "../models/Proposal"; | ||
let proposals: Proposal[] = []; | ||
let proposals: ProposalAttributes[] = []; | ||
let error = null; | ||
try { | ||
proposals = await Proposal.findAll({ | ||
where: { | ||
status: 'FUNDING_VOTE' | ||
}, | ||
order: [['createdAt', 'DESC']] | ||
}); | ||
proposals = await fetchActiveProposals(); | ||
} catch (e) { | ||
error = 'Error fetching proposals'; | ||
error = "Error fetching proposals"; | ||
} | ||
--- | ||
|
||
<MainLayout title="Mina Proposal Voting"> | ||
<WalletConnectButton /> | ||
<NetworkSwitch /> | ||
|
||
<h2 class="text-2xl font-bold mt-8 mb-4">Active Proposals</h2> | ||
|
||
{error ? ( | ||
<p class="text-red-500">{error}</p> | ||
) : proposals.length > 0 ? ( | ||
<ul class="space-y-4"> | ||
{proposals.map((proposal) => ( | ||
<li class="bg-white p-4 rounded-lg shadow-md"> | ||
<h3 class="text-xl font-semibold mb-2">{proposal.name}</h3> | ||
<p class="mb-2">Budget: {proposal.budget} MINA</p> | ||
<a href={`/vote/${proposal.id}`} class="text-blue-500 hover:underline">Vote on this proposal</a> | ||
</li> | ||
))} | ||
</ul> | ||
) : ( | ||
<p>No active proposals at the moment.</p> | ||
)} | ||
</MainLayout> | ||
|
||
{ | ||
error ? ( | ||
<p class="text-red-500">{error}</p> | ||
) : proposals.length > 0 ? ( | ||
<ul class="space-y-4"> | ||
{proposals.map((proposal) => ( | ||
<li class="bg-white p-4 rounded-lg shadow-md"> | ||
<h3 class="text-xl font-semibold mb-2">{proposal.name}</h3> | ||
<p class="mb-2">Budget: {proposal.budget} MINA</p> | ||
<a | ||
href={`/vote/${proposal.id}`} | ||
class="text-blue-500 hover:underline" | ||
> | ||
Vote on this proposal | ||
</a> | ||
</li> | ||
))} | ||
</ul> | ||
) : ( | ||
<p>No active proposals at the moment.</p> | ||
) | ||
} | ||
</MainLayout> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { ProposalAttributes } from "../models/Proposal"; | ||
|
||
const { API_BASE_URL } = import.meta.env; | ||
|
||
export async function fetchProposal(id: number): Promise<ProposalAttributes> { | ||
const response = await fetch(`${API_BASE_URL}/api/projects/${id}`); | ||
if (!response.ok) { | ||
throw new Error("Failed to fetch proposal"); | ||
} | ||
const data = await response.json(); | ||
return data.proposal; | ||
} | ||
|
||
export async function fetchActiveProposals(): Promise<ProposalAttributes[]> { | ||
const response = await fetch( | ||
`${API_BASE_URL}/api/projects?status=FUNDING_VOTE` | ||
); | ||
if (!response.ok) { | ||
throw new Error("Failed to fetch active proposals"); | ||
} | ||
const data = await response.json(); | ||
return data.proposals; | ||
} |