Skip to content

Commit 46ffefb

Browse files
committed
Add anonymous option for votes
1 parent a19db3b commit 46ffefb

7 files changed

Lines changed: 30 additions & 6 deletions

File tree

backend/api/src/create-vote.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { tryCatch } from 'common/util/try-catch'
66

77
export const createVote: APIHandler<
88
'create-vote'
9-
> = async ({ title, description }, auth) => {
9+
> = async ({ title, description, isAnonymous }, auth) => {
1010
const creator = await getUser(auth.uid)
1111
if (!creator) throw new APIError(401, 'Your account was not found')
1212

@@ -17,6 +17,7 @@ export const createVote: APIHandler<
1717
creator_id: creator.id,
1818
title,
1919
description,
20+
is_anonymous: isAnonymous,
2021
})
2122
)
2223

backend/supabase/vote_results.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ DROP INDEX IF EXISTS idx_vote_results_vote_choice;
3434
CREATE INDEX idx_vote_results_vote_choice ON vote_results (vote_id, choice);
3535

3636

37+
drop function if exists get_votes_with_results;
3738
create or replace function get_votes_with_results()
3839
returns table (
3940
id BIGINT,
4041
title text,
4142
description jsonb,
4243
created_time timestamptz,
4344
creator_id TEXT,
45+
is_anonymous boolean,
4446
votes_for int,
4547
votes_against int,
4648
votes_abstain int,
@@ -53,6 +55,7 @@ SELECT
5355
v.description,
5456
v.created_time,
5557
v.creator_id,
58+
v.is_anonymous,
5659
COALESCE(SUM(CASE WHEN r.choice = 1 THEN 1 ELSE 0 END), 0) AS votes_for,
5760
COALESCE(SUM(CASE WHEN r.choice = -1 THEN 1 ELSE 0 END), 0) AS votes_against,
5861
COALESCE(SUM(CASE WHEN r.choice = 0 THEN 1 ELSE 0 END), 0) AS votes_abstain,

backend/supabase/votes.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CREATE TABLE IF NOT EXISTS votes (
33
created_time TIMESTAMPTZ DEFAULT now() NOT NULL,
44
creator_id TEXT NOT NULL,
55
title TEXT NOT NULL,
6+
is_anonymous BOOLEAN NOT NULL,
67
description JSONB
78
);
89

common/src/api/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ export const API = (_apiTypeCheck = {
520520
returns: {} as any,
521521
props: z.object({
522522
title: z.string().min(1),
523+
isAnonymous: z.boolean(),
523524
description: contentSchema,
524525
}),
525526
},

common/src/supabase/schema.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type Database = {
1010
// Allows to automatically instantiate createClient with right options
1111
// instead of createClient<Database, { PostgrestVersion: 'XX' }>(URL, KEY)
1212
__InternalSupabase: {
13-
PostgrestVersion: '13.0.5'
13+
PostgrestVersion: '13.0.4'
1414
}
1515
public: {
1616
Tables: {
@@ -471,7 +471,7 @@ export type Database = {
471471
geodb_city_id?: string | null
472472
has_kids?: number | null
473473
height_in_inches?: number | null
474-
id?: never
474+
id?: number
475475
is_smoker?: boolean | null
476476
is_vegetarian_or_vegan?: boolean | null
477477
last_modification_time?: string
@@ -519,7 +519,7 @@ export type Database = {
519519
geodb_city_id?: string | null
520520
has_kids?: number | null
521521
height_in_inches?: number | null
522-
id?: never
522+
id?: number
523523
is_smoker?: boolean | null
524524
is_vegetarian_or_vegan?: boolean | null
525525
last_modification_time?: string
@@ -748,20 +748,23 @@ export type Database = {
748748
creator_id: string
749749
description: Json | null
750750
id: number
751+
is_anonymous: boolean | null
751752
title: string
752753
}
753754
Insert: {
754755
created_time?: string
755756
creator_id: string
756757
description?: Json | null
757758
id?: never
759+
is_anonymous?: boolean | null
758760
title: string
759761
}
760762
Update: {
761763
created_time?: string
762764
creator_id?: string
763765
description?: Json | null
764766
id?: never
767+
is_anonymous?: boolean | null
765768
title?: string
766769
}
767770
Relationships: [
@@ -814,6 +817,7 @@ export type Database = {
814817
creator_id: string
815818
description: Json
816819
id: number
820+
is_anonymous: boolean
817821
priority: number
818822
title: string
819823
votes_abstain: number
@@ -874,7 +878,7 @@ export type Database = {
874878
Returns: Json
875879
}
876880
ts_to_millis: {
877-
Args: { ts: string }
881+
Args: { ts: string } | { ts: string }
878882
Returns: number
879883
}
880884
}

web/components/votes/vote-info.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export function VoteComponent() {
2525

2626
const [title, setTitle] = useState<string>('')
2727
const [editor, setEditor] = useState<any>(null)
28+
const [isAnonymous, setIsAnonymous] = useState<boolean>(false)
2829

2930
const hideButton = title.length == 0
3031

@@ -53,6 +54,16 @@ export function VoteComponent() {
5354
<VoteCreator
5455
onEditor={(e) => setEditor(e)}
5556
/>
57+
<Row className="mx-2 mb-2 items-center gap-2 text-sm text-gray-500">
58+
<input
59+
type="checkbox"
60+
id="anonymous"
61+
checked={isAnonymous}
62+
onChange={(e) => setIsAnonymous(e.target.checked)}
63+
className="h-4 w-4 rounded-md border-gray-300 text-blue-600 focus:ring-blue-500"
64+
/>
65+
<label htmlFor="anonymous">Anonymous?</label>
66+
</Row>
5667
{!hideButton && (
5768
<Row className="right-1 justify-between gap-2">
5869
<Button
@@ -61,11 +72,14 @@ export function VoteComponent() {
6172
const data = {
6273
title: title,
6374
description: editor.getJSON() as JSONContent,
75+
isAnonymous: isAnonymous,
6476
};
6577
const newVote = await api('create-vote', data).catch(() => {
6678
toast.error('Failed to create vote — try again or contact us...')
6779
})
6880
if (!newVote) return
81+
setTitle('')
82+
editor.commands.clearContent()
6983
toast.success('Vote created')
7084
console.debug('Vote created', newVote)
7185
refreshVotes()

web/components/votes/vote-item.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function VoteItem(props: {
3535
</Col>
3636
<Row className={'gap-2 mt-2 items-center justify-between w-full customlink'}>
3737
{!!vote.priority ? <div>Priority: {((vote.priority / vote.votes_for / 3) * 100).toFixed(0)}%</div> : <p></p>}
38-
{creator?.username && <Link href={`/${creator.username}`} className="customlink">{creator.username}</Link>}
38+
{!vote.is_anonymous && creator?.username && <Link href={`/${creator.username}`} className="customlink">{creator.username}</Link>}
3939
</Row>
4040
<VoteButtons
4141
voteId={vote.id}

0 commit comments

Comments
 (0)