-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
744 additions
and
21 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
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,111 @@ | ||
import { create } from 'zustand' | ||
import { | ||
persist, | ||
createJSONStorage, | ||
subscribeWithSelector, | ||
} from 'zustand/middleware' | ||
import { MichelsonMap } from '@taquito/taquito' | ||
import { POLLS_CONTRACT} from '@constants' | ||
import { Tezos, useUserStore } from './userStore' | ||
import { useModalStore } from './modalStore' | ||
import { stringToHex } from '@utils/string' | ||
|
||
type OperationReturn = Promise<string | undefined> | ||
|
||
interface PollsState { | ||
/** Votes in an existing poll */ | ||
votePoll: (pollId: string, option: number, maxCheckpoints: number | null, callback?: any) => OperationReturn | ||
/** Creates a new poll */ | ||
createPoll: (question: string, descriptionIpfsPath: string, voteWeightMethod: string, votePeriod: string, options: string[], callback?: any) => OperationReturn | ||
} | ||
|
||
export const usePollsStore = create<PollsState>()( | ||
subscribeWithSelector( | ||
persist( | ||
(set, get) => ({ | ||
votePoll: async (pollId, option, maxCheckpoints, callback) => { | ||
const handleOp = useUserStore.getState().handleOp | ||
const showError = useModalStore.getState().showError | ||
const step = useModalStore.getState().step | ||
|
||
const modalTitle = 'Vote teia poll' | ||
step(modalTitle, 'Waiting for confirmation', true) | ||
|
||
try { | ||
const contract = await Tezos.wallet.at(POLLS_CONTRACT) | ||
|
||
const parameters = { | ||
poll_id: parseInt(pollId), | ||
option: option, | ||
max_checkpoints: maxCheckpoints | ||
} | ||
const batch = contract.methodsObject.vote(parameters) | ||
const opHash = await handleOp(batch, modalTitle) | ||
|
||
callback?.() | ||
|
||
return opHash | ||
} catch (e) { | ||
showError(modalTitle, e) | ||
} | ||
}, | ||
createPoll: async (question, descriptionIpfsPath, voteWeightMethod, votePeriod, options, callback) => { | ||
const handleOp = useUserStore.getState().handleOp | ||
const show = useModalStore.getState().show | ||
const showError = useModalStore.getState().showError | ||
const step = useModalStore.getState().step | ||
|
||
const modalTitle = 'Create teia poll' | ||
|
||
if (!question || question.length < 10) { | ||
show( | ||
modalTitle, | ||
'The poll question must be at least 10 characters long' | ||
) | ||
return | ||
} | ||
|
||
if (options.length < 2) { | ||
show( | ||
modalTitle, | ||
'The poll should have at least 2 options to vote' | ||
) | ||
return | ||
} | ||
|
||
step(modalTitle, 'Waiting for confirmation', true) | ||
|
||
try { | ||
const contract = await Tezos.wallet.at(POLLS_CONTRACT) | ||
|
||
const parameters = { | ||
question: stringToHex(question), | ||
description: descriptionIpfsPath === '' | ||
? stringToHex('') | ||
: stringToHex(`ipfs://${descriptionIpfsPath}`), | ||
options: MichelsonMap.fromLiteral( | ||
Object.fromEntries( | ||
options.map((option, index) => [index, stringToHex(option)]) | ||
) | ||
), | ||
vote_weight_method: { [voteWeightMethod]: [['unit']] }, | ||
vote_period: votePeriod | ||
} | ||
const batch = contract.methodsObject.create_poll(parameters) | ||
const opHash = await handleOp(batch, modalTitle) | ||
|
||
callback?.() | ||
|
||
return opHash | ||
} catch (e) { | ||
showError(modalTitle, e) | ||
} | ||
}, | ||
}), | ||
{ | ||
name: 'polls', | ||
storage: createJSONStorage(() => localStorage), // or sessionStorage? | ||
} | ||
) | ||
) | ||
) |
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 |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
|
||
.headline { | ||
text-align: center; | ||
margin-bottom: 1em 0; | ||
|
||
> p { | ||
margin: 1em 0; | ||
|
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
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,29 @@ | ||
import { Outlet } from 'react-router-dom' | ||
import { Page } from '@atoms/layout' | ||
import { Tabs } from '@atoms/tab' | ||
import styles from '@style' | ||
|
||
const TABS = [ | ||
{ | ||
title: 'Polls', | ||
to: '', | ||
}, | ||
{ | ||
title: 'Create', | ||
to: 'create', | ||
}, | ||
] | ||
|
||
export function TeiaPolls() { | ||
return ( | ||
<Page title="Teia polls"> | ||
<div className={styles.container}> | ||
<h1 className={styles.headline}>Teia polls</h1> | ||
|
||
<Tabs tabs={TABS} /> | ||
|
||
<Outlet /> | ||
</div> | ||
</Page> | ||
) | ||
} |
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,8 @@ | ||
.container { | ||
padding-bottom: 60px; | ||
width: 100%; | ||
} | ||
|
||
.headline { | ||
text-align: center; | ||
} |
Oops, something went wrong.