Building a Debate App: Part 4

By akohad Apr26,2023

[ad_1]

Decentralization / Social media

Implementing voting

git clone https://github.com/jeremyorme/debate.git
cd debate
git checkout release/0.0.2
ion-card {
margin: 0 0 1px 0;
}
import './App.css';
                <IonCard>
<IonGrid>
<IonRow>
<IonCol>
<IonInput placeholder="What do you think?" value={description} onIonChange={e => updateDescription(e.detail.value)} />
</IonCol>
<IonCol size="auto">
<IonButton size="small" fill="clear" disabled={description.length == 0} onClick={() => addMessage()}>
<IonIcon icon={arrowForwardSharp} />
</IonButton>
</IonCol>
</IonRow>
</IonGrid>
</IonCard>

Storing votes

    // Votes

private _votes: CollectionManager<IVote> = new CollectionManager<IVote>();

async loadVotes(debateId: string) {
if (!this._db)
return;

const collectionName = 'debate-' + debateId + '-votes';
this._votes.init(await this._db.collection(collectionName, {
publicAccess: AccessRights.ReadAnyWriteOwn,
conflictResolution: ConflictResolution.LastWriteWins
}));
}


votes(): IVote[] {
return this._votes.entries() || [];
}

onVotes(callback: () => void) {
return this._votes.onUpdated(callback);
}

addVote(message: IVote) {
if (this._publicKey)
this._votes.addEntry({ ...message, _id: this._publicKey });
}

    ownVoteDirection(): VoteDirection {
if (!this._publicKey)
return VoteDirection.Undecided;
return this._votes.entry(this._publicKey)?.direction || VoteDirection.Undecided;
}
    private _publicKey: string | null = null;

async init(db: IDb, publicKey: string) {
this._db = db;
this._publicKey = publicKey;
await this._loadDebates();
}

        const publicKey = dbClient.publicKey();
if (!publicKey)
return;

appData.init(db, publicKey);

Voting in the UI

    useEffect(() => {
appData.loadMessages(id, side);
appData.loadVotes(id);
return appData.onDebatesUpdated(() => {
setDebateTitle(appData.debateTitle(id));
appData.loadMessages(id, side);
appData.loadVotes(id);
});
}, []);
    useEffect(() => {
return appData.onVotes(() => {
setOwnVoteDirection(appData.ownVoteDirection());
});
}, []);
    const [ownVoteDirection, setOwnVoteDirection] = useState(appData.ownVoteDirection());
                    <IonButtons slot="end">
{side == 'against' ? <IonButton slot="icon-only" onClick={() => updateOwnVoteDirection(VoteDirection.Against)}>
<IonIcon icon={ownVoteDirection == VoteDirection.Against ? thumbsDownSharp : thumbsDownOutline} />
</IonButton> : <IonButton slot="icon-only" onClick={() => updateOwnVoteDirection(VoteDirection.For)}>
<IonIcon icon={ownVoteDirection == VoteDirection.For ? thumbsUpSharp : thumbsUpOutline} />
</IonButton>}
</IonButtons>
    const updateOwnVoteDirection = (newDirection: VoteDirection) => {
const direction = newDirection != ownVoteDirection ? newDirection : VoteDirection.Undecided;
const vote: IVote = {
...dbEntryDefaults,
direction
};
appData.addVote(vote);
setOwnVoteDirection(direction);
}
git clone https://github.com/jeremyorme/debate.git
cd debate
git checkout release/0.0.3

[ad_2]

Source link

By akohad

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *