Skip to content
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

feat: add cast vote page #10

Merged
merged 2 commits into from
Jul 31, 2024
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
27 changes: 26 additions & 1 deletion apps/frontend/src/app/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AboutComponent } from './features/about/about.component'
import { LoginComponent } from './features/login/login.component'
import { RegisterComponent } from './features/register/register.component'
import { AuthGuard } from './guards/auth.guard'
import { CastComponent } from './features/voting/cast/cast.component'

export const appRoutes: Route[] = [
{
Expand All @@ -25,7 +26,31 @@ export const appRoutes: Route[] = [
component: RegisterComponent,
},
{
path: '**',
path: 'voting',
children: [
{
path: 'cast',
children: [
{
path: '',
redirectTo: '/',
pathMatch: 'full',
},
{
path: ':id',
component: CastComponent,
},
],
},
{
path: '',
redirectTo: '/',
pathMatch: 'full',
},
],
},
{
path: 'about',
redirectTo: '',
},
]
7 changes: 4 additions & 3 deletions apps/frontend/src/app/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as process from 'process'

const API_URL = process.env.API_URL || 'http://localhost:3000'

export { API_URL }
const API_URL = process.env['API_URL'] || 'http://localhost:3000'
const CONTRACT_ID = process.env['CONTRACT_ID'] || ''
const TEST_ACCOUNT = process.env['TEST_ACCOUNT'] || ''
export { API_URL, CONTRACT_ID, TEST_ACCOUNT }
83 changes: 83 additions & 0 deletions apps/frontend/src/app/core/stellar/castVote.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Injectable } from '@angular/core'
import { CONTRACT_ID } from '../../config/config'
import {
BASE_FEE,
Contract,
nativeToScVal,
Networks,
SorobanRpc,
TransactionBuilder,
} from '@stellar/stellar-sdk'
import { Keypair } from '@stellar/typescript-wallet-sdk'

@Injectable({
providedIn: 'root',
})
export class CastVoteService {
private contractId = CONTRACT_ID
private isLoading = true
private hasError = false
private errorMessage = ''

async castVote(
server: SorobanRpc.Server,
sourceKeypair: Keypair,
voteId: string,
currentOption: string,
) {
try {
const contract = new Contract(this.contractId)

const sourceAccount = await server.getAccount(sourceKeypair.publicKey())

const builtTransaction = new TransactionBuilder(sourceAccount, {
fee: BASE_FEE,
networkPassphrase: Networks.TESTNET,
})
.addOperation(
contract.call(
'cast',
nativeToScVal(voteId, { type: 'symbol' }),
nativeToScVal(currentOption, { type: 'symbol' }),
nativeToScVal(sourceKeypair.publicKey(), { type: 'address' }),
),
)
.setTimeout(30)
.build()

const preparedTransaction = await server.prepareTransaction(
builtTransaction,
)

preparedTransaction.sign(sourceKeypair)

const sendResponse = await server.sendTransaction(preparedTransaction)

if (sendResponse.status === 'PENDING') {
let getResponse = await server.getTransaction(sendResponse.hash)
while (getResponse.status === 'NOT_FOUND') {
await new Promise(resolve => setTimeout(resolve, 1000))
getResponse = await server.getTransaction(sendResponse.hash)
}
if (getResponse.status === 'SUCCESS') {
this.isLoading = false
} else {
return
}
} else {
return
}
} catch (err) {
this.isLoading = false
this.hasError = true
this.errorMessage =
'There was an Error submitting your vote, did you vote already?'
}

return {
isLoading: this.isLoading,
hasError: this.hasError,
errorMessage: this.errorMessage,
}
}
}
91 changes: 91 additions & 0 deletions apps/frontend/src/app/core/stellar/getVoteOption.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Injectable } from '@angular/core'
import { CONTRACT_ID } from '../../config/config'
import {
BASE_FEE,
Contract,
nativeToScVal,
Networks,
SorobanRpc,
TransactionBuilder,
} from '@stellar/stellar-sdk'
import { Keypair } from '@stellar/typescript-wallet-sdk'

@Injectable({
providedIn: 'root',
})
export class GetVoteOptionService {
private contractId = CONTRACT_ID
private optionsArr = new Array<string>()
private isLoading = true
private hasError = false
private errorMessage = ''

async getVoteOptions(
server: SorobanRpc.Server,
sourceKeypair: Keypair,
voteId: string,
) {
try {
const contract = new Contract(this.contractId)

const sourceAccount = await server.getAccount(sourceKeypair.publicKey())

const builtTransaction = new TransactionBuilder(sourceAccount, {
fee: BASE_FEE,
networkPassphrase: Networks.TESTNET,
})
.addOperation(
contract.call(
'get_vote_options',
nativeToScVal(voteId, { type: 'symbol' }),
),
)
.setTimeout(30)
.build()

const preparedTransaction = await server.prepareTransaction(
builtTransaction,
)

preparedTransaction.sign(sourceKeypair)

const sendResponse = await server.sendTransaction(preparedTransaction)

if (sendResponse.status === 'PENDING') {
let getResponse = await server.getTransaction(sendResponse.hash)
while (getResponse.status === 'NOT_FOUND') {
await new Promise(resolve => setTimeout(resolve, 1000))
getResponse = await server.getTransaction(sendResponse.hash)
}
if (getResponse.status === 'SUCCESS') {
if (!getResponse.returnValue?.vec()) {
return
}

getResponse.returnValue.vec()?.forEach(item => {
if (!item.value()) {
return
}
this.optionsArr.push(String(item.value()))
})
this.isLoading = false
} else {
return
}
} else {
return
}
} catch (err) {
this.isLoading = false
this.hasError = true
this.errorMessage =
'There was an Error getting this vote ID, please try again.'
}
return {
optionsArr: this.optionsArr,
isLoading: this.isLoading,
hasError: this.hasError,
errorMessage: this.errorMessage,
}
}
}
14 changes: 7 additions & 7 deletions apps/frontend/src/app/features/register/register.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import { Keypair } from '@stellar/typescript-wallet-sdk'
styleUrls: ['./register.component.css'],
})
export class RegisterComponent {
publicKey: string = ''
secretKey: string = ''
captchaImage: string = ''
captchaId: string = ''
transaction: string = ''
captchaAnswer: string = ''
authToken: string = ''
publicKey = ''
secretKey = ''
captchaImage = ''
captchaId = ''
transaction = ''
captchaAnswer = ''
authToken = ''

constructor(private authService: AuthService) {}

Expand Down
Empty file.
83 changes: 83 additions & 0 deletions apps/frontend/src/app/features/voting/cast/cast.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<div class="flex justify-center mt-20">
<fieldset class="space-y-4 w-11/12">
@if(isLoading){
<div class="flex justify-center">
<svg
aria-hidden="true"
class="inline w-12 h-12 text-gray-200 animate-spin fill-blue-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span class="sr-only">Loading...</span>
</div>
} @if (!isLoading && !hasError){ @for (option of optionsArr; track option){
<div (click)="currentOption = option">
<label
class="flex cursor-pointer justify-between gap-4 rounded-lg border border-gray-100 bg-white p-4 text-sm font-medium shadow-sm hover:border-gray-200 has-[:checked]:border-blue-500 has-[:checked]:ring-1 has-[:checked]:ring-blue-500"
>
<div>
<p class="text-gray-700 text-lg">{{ option }}</p>
</div>

<input
type="radio"
name="DeliveryOption"
value="DeliveryStandard"
id="DeliveryStandard"
class="size-5 border-gray-300 text-blue-500"
/>
</label>
</div>
}
<button
(click)="submitVote()"
type="button"
class="rounded-md bg-indigo-50 px-3.5 py-2.5 text-sm font-semibold text-indigo-600 shadow-sm hover:bg-indigo-100 w-full"
>
Submit
</button>
} @if (hasError){
<div class="rounded-md bg-red-50 p-4">
<div class="flex">
<div class="flex-shrink-0">
<svg
class="h-5 w-5 text-red-400"
viewBox="0 0 20 20"
fill="currentColor"
aria-hidden="true"
>
<path
fill-rule="evenodd"
d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.28 7.22a.75.75 0 00-1.06 1.06L8.94 10l-1.72 1.72a.75.75 0 101.06 1.06L10 11.06l1.72 1.72a.75.75 0 101.06-1.06L11.06 10l1.72-1.72a.75.75 0 00-1.06-1.06L10 8.94 8.28 7.22z"
clip-rule="evenodd"
/>
</svg>
</div>
<div class="ml-3">
<h3 class="text-sm font-medium text-red-800">An Error Occurred</h3>
<div class="mt-2 text-sm text-red-700">
<p>{{ errorMessage }}</p>
</div>
</div>
</div>
</div>
<button
(click)="goBack()"
type="button"
class="rounded-md bg-indigo-50 px-3.5 py-2.5 text-sm font-semibold text-indigo-600 shadow-sm hover:bg-indigo-100 w-full"
>
Back
</button>
}
</fieldset>
</div>
22 changes: 22 additions & 0 deletions apps/frontend/src/app/features/voting/cast/cast.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ComponentFixture, TestBed } from '@angular/core/testing'

import { CastComponent } from './cast.component'

describe('CastComponent', () => {
let component: CastComponent
let fixture: ComponentFixture<CastComponent>

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [CastComponent],
}).compileComponents()

fixture = TestBed.createComponent(CastComponent)
component = fixture.componentInstance
fixture.detectChanges()
})

it('should create', () => {
expect(component).toBeTruthy()
})
})
Loading