Skip to content

Commit

Permalink
add db and VotingRadio
Browse files Browse the repository at this point in the history
  • Loading branch information
labintsev committed Dec 9, 2024
1 parent faee73e commit cffba96
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 78 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@
},
"packageManager": "yarn@3.2.3",
"devDependencies": {
"@types/bcrypt": "^5",
"husky": "^8.0.1",
"lint-staged": "^13.0.3"
},
"engines": {
"node": ">=18.17.0"
},
"dependencies": {
"bcrypt": "^5.1.1"
}
}
143 changes: 72 additions & 71 deletions packages/nextjs/app/seed/placeholder-data.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,77 @@
// const shareholders = [
// {
// id: '410544b2-4001-4271-9855-fec4b6a6442a',
// name: 'User',
// email: 'user@nextmail.com',
// password: '123456',
// },
// ];
const shareholders = [
{
id: "410544b2-4001-4271-9855-fec4b6a6442a",
name: "User",
email: "user@nextmail.com",
password: "123456",
},
];

// const questions = [
// {
// id: 0,
// question: 'Do you enjoy this voting application?',
// is_active: 1
// },
// {
// id: 1,
// question: 'Who will be Chief Executive Officer in 2025?',
// is_active: 1
// },
// {
// id: 2,
// question: 'Do you approve the financial report?',
// is_active: 1
// }
// ]
const questions = [
{
id: 0,
question: "Do you enjoy this voting application?",
is_active: 1,
},
{
id: 1,
question: "Who will be Chief Executive Officer in 2025?",
is_active: 1,
},
{
id: 2,
question: "Do you approve this financial report?",
is_active: 1,
},
];

// const question_choices = [
// {
// id: 0,
// question_id: questions[0].id,
// choice: 'Yes'
// },
// {
// id: 1,
// question_id: questions[0].id,
// choice: 'No'
// },
// {
// id: 2,
// question_id: questions[1].id,
// choice: 'Donald Duck'
// },
// {
// id: 3,
// question_id: questions[1].id,
// choice: 'John Smith'
// },
// {
// id: 4,
// question_id: questions[1].id,
// choice: 'Billy Bones'
// },
// {
// id: 5,
// question_id: questions[2].id,
// choice: 'Yes'
// },
// {
// id: 6,
// question_id: questions[2].id,
// choice: 'No'
// },
// ]
const question_choices = [
{
id: 0,
question_id: questions[0].id,
choice: "Yes",
},
{
id: 1,
question_id: questions[0].id,
choice: "No",
},
{
id: 2,
question_id: questions[1].id,
choice: "Donald Duck",
},
{
id: 3,
question_id: questions[1].id,
choice: "John Smith",
},
{
id: 4,
question_id: questions[1].id,
choice: "Billy Bones",
},
{
id: 5,
question_id: questions[2].id,
choice: "Yes",
},
{
id: 6,
question_id: questions[2].id,
choice: "No",
},
];

// const shareholder_question_answers = [
// {
// sh_id: shareholders[0].id,
// q_id: question_choices[0].id,
// chice_id: question_choices[0].id,
// answer_time: '2024-12-8'
// }
// ]
const shareholder_question_answers = [
{
id: 0,
sh_id: shareholders[0].id,
q_id: question_choices[0].id,
choice_id: question_choices[0].id,
answer_time: "2024-12-8 12-34",
},
];

const users = [
{
Expand Down Expand Up @@ -216,4 +217,4 @@ const revenue = [
{ month: "Dec", revenue: 4800 },
];

export { users, customers, invoices, revenue };
export { users, customers, invoices, revenue, shareholders, questions, question_choices, shareholder_question_answers };
114 changes: 113 additions & 1 deletion packages/nextjs/app/seed/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,117 @@
import { customers, invoices, revenue, users } from "./placeholder-data";
import {
customers,
invoices,
question_choices,
questions,
revenue,
shareholder_question_answers,
shareholders,
users,
} from "./placeholder-data";
import { db } from "@vercel/postgres";
import bcrypt from "bcrypt";

const client = await db.connect();

async function seedShareholders() {
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
await client.sql`
CREATE TABLE IF NOT EXISTS shareholders (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
);
`;

const insertedShareholders = await Promise.all(
shareholders.map(async sholder => {
const hashedPassword = await bcrypt.hash(sholder.password, 10);
return client.sql`
INSERT INTO shareholders (id, name, email, password)
VALUES (${sholder.id}, ${sholder.name}, ${sholder.email}, ${hashedPassword})
ON CONFLICT (id) DO NOTHING;
`;
}),
);

return insertedShareholders;
}

async function seedQuestions() {
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;

await client.sql`
CREATE TABLE IF NOT EXISTS questions (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
question VARCHAR(255) NOT NULL,
is_active INT NOT NULL
);
`;

const insertedQuestions = await Promise.all(
questions.map(
question => client.sql`
INSERT INTO questions (id, question, is_active)
VALUES (${question.id}, ${question.question}, ${question.is_active} )
ON CONFLICT (id) DO NOTHING;
`,
),
);

return insertedQuestions;
}

async function seedChoices() {
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;

await client.sql`
CREATE TABLE IF NOT EXISTS choices (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
question_id UUID NOT NULL,
choice VARCHAR(255) NOT NULL,
);
`;

const insertedChoices = await Promise.all(
question_choices.map(
choice => client.sql`
INSERT INTO choices (id, question_id, choice)
VALUES (${choice.id}, ${choice.question_id}, ${choice.choice} )
ON CONFLICT (id) DO NOTHING;
`,
),
);

return insertedChoices;
}

async function seedShareholderAnswers() {
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;

await client.sql`
CREATE TABLE IF NOT EXISTS answers (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
sh_id UUID NOT NULL,
question_id UUID NOT NULL,
choice_id VARCHAR(255) NOT NULL,
answer_time TIMESTAMP NOT NULL
);
`;

const insertedAnswers = await Promise.all(
shareholder_question_answers.map(
answer => client.sql`
INSERT INTO answers (id, sh_id, question_id, choice_id, answer_time)
VALUES (${answer.id}, ${answer.q_id}, ${answer.choice_id}, to_timestamp(${Date.now()} / 1000.0) )
ON CONFLICT (id) DO NOTHING;
`,
),
);

return insertedAnswers;
}

async function seedUsers() {
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
await client.sql`
Expand Down Expand Up @@ -108,6 +216,10 @@ export async function GET() {
// });
try {
await client.sql`BEGIN`;
await seedShareholders();
await seedQuestions();
await seedChoices();
await seedShareholderAnswers();
await seedUsers();
await seedCustomers();
await seedInvoices();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React, { useState } from "react";

const Voting = () => {
const UpDownVoting = () => {
// Initialize state to keep track of vote counts
const [voteCount, setVoteCount] = useState(0);

Expand Down Expand Up @@ -34,4 +34,4 @@ const Voting = () => {
);
};

export default Voting;
export default UpDownVoting;
52 changes: 52 additions & 0 deletions packages/nextjs/app/voting/_components/VotingRadio.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use client";

import React, { useState } from "react";

const questions = [
{
id: 0,
question: "Do you enjoy this voting application?",
answers: [
{ title: "Yes", score: 0 },
{ title: "No", score: 1 },
],
},
];

function VotingRadio() {
const [checkBox, setCheckBox] = useState();

const handleInputChange = (score: any) => {
setCheckBox(score);
};

return (
// <-- Here you need to return something from component
<>
<h1>{questions[0].question}</h1>
{questions[0].answers.map(({ title, score }: any) => (
<label key={score}>
<div>
<input
type="radio"
value={score}
name={score}
onChange={() => handleInputChange(score)}
checked={score === checkBox}
/>
{title}
</div>
</label>
))}
<button
onClick={() => {
console.log(checkBox);
}}
>
Vote!
</button>
</>
);
}

export default VotingRadio;
4 changes: 2 additions & 2 deletions packages/nextjs/app/voting/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Voting from "./_components/Voting";
import VotingRadio from "./_components/VotingRadio";

export default function Page() {
return (
<>
<Voting />
<VotingRadio />
</>
);
}
Loading

0 comments on commit cffba96

Please sign in to comment.