Skip to content

Commit

Permalink
BACKUP2
Browse files Browse the repository at this point in the history
  • Loading branch information
kjurl committed Sep 20, 2024
1 parent 1e53eaf commit e517133
Show file tree
Hide file tree
Showing 11 changed files with 489 additions and 0 deletions.
13 changes: 13 additions & 0 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { config } from 'dotenv';
import { defineConfig } from 'drizzle-kit';

config({ path: '.env' });

export default defineConfig({
schema: './src/database/schema/*',
out: './migrations',
dialect: 'postgresql',
dbCredentials: {
url: process.env.DATABASE_URI!,
},
});
6 changes: 6 additions & 0 deletions migrations/0000_kind_puff_adder.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS "netaid-users" (
"id" bigserial PRIMARY KEY NOT NULL,
"name" varchar(100) NOT NULL,
"password" varchar(100) NOT NULL,
"aadhar_no" bigint NOT NULL
);
49 changes: 49 additions & 0 deletions migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"id": "1c1f1af6-4823-4063-b7a8-210b14ad9aea",
"prevId": "00000000-0000-0000-0000-000000000000",
"version": "5",
"dialect": "pg",
"tables": {
"netaid-users": {
"name": "netaid-users",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "bigserial",
"primaryKey": true,
"notNull": true
},
"name": {
"name": "name",
"type": "varchar(100)",
"primaryKey": false,
"notNull": true
},
"password": {
"name": "password",
"type": "varchar(100)",
"primaryKey": false,
"notNull": true
},
"aadhar_no": {
"name": "aadhar_no",
"type": "bigint",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"schemas": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
13 changes: 13 additions & 0 deletions migrations/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "5",
"dialect": "pg",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1726718821752,
"tag": "0000_kind_puff_adder",
"breakpoints": true
}
]
}
119 changes: 119 additions & 0 deletions src/components/netaid/authButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { createSignal } from 'solid-js';
import { client } from "@/utils/trpc";
import { z } from 'zod';

const AuthModal = () => {
let modalButton!: HTMLButtonElement;
let modalDialog!: HTMLDialogElement;

const [isLogin, setIsLogin] = createSignal(true);
const [formData, setFormData] = createSignal({ name: '', password: '', aadhar_no: '' });
const [user, setUser] = createSignal<{ name: string } | null>(null);


const handleInputChange = (e: Event) => {
const target = e.target as HTMLInputElement;
setFormData({ ...formData(), [target.name]: target.value });
};

const handleSubmit = async (e: Event) => {
e.preventDefault();
const data = formData();

try {
if (isLogin()) {
const login_res = await client.netaid.login.mutate({
username: data.name,
password: data.password,
});
if (login_res) alert('Login successful!');
else alert('Login unsuccessful')
} else {
const register_res = await client.netaid.register.mutate({
name: data.name,
password: data.password,
aadhar_no: Number(data.aadhar_no),
});
if (register_res) alert('Registration successful!');
else alert('Registration unsuccessful')
}
} catch (error: any) {
alert(error.message || 'An error occurred');
}
};

return (
<>
<button ref={modalButton} class="btn" onclick={() => {
setFormData({ name: '', password: '', aadhar_no: '' });
modalDialog.showModal()
}} disabled={!!user()}>
{user() ? `Logged in as ${user()!.name}` : 'Login/Register'}
</button>
<dialog ref={modalDialog} class="modal modal-bottom sm:modal-middle">
<div class="modal-box">
<form onSubmit={handleSubmit} class="p-4 bg-base-200 rounded-lg shadow-md">
<h3 class="text-lg font-bold">{isLogin() ? 'Login' : 'Register'}</h3>
<div class="form-control mb-4">
<label class="label">
<span class="label-text">Name</span>
</label>

<input
type="text"
name="name"
placeholder="Name"
value={formData().name}
onInput={handleInputChange}
required
class="input input-bordered"
/>
</div>
<div class="form-control mb-4">
<label class="label">
<span class="label-text">Password</span>
</label>

<input
type="password"
name="password"
placeholder="Password"
value={formData().password}
onInput={handleInputChange}
required
class="input input-bordered"
/>
</div>
{!isLogin() && (
<div class="form-control mb-4">
<label class="label">
<span class="label-text">Aadhar Number</span>
</label>

<input
type="text"
name="aadhar_no"
placeholder="Aadhar Number"
value={formData().aadhar_no}
onInput={handleInputChange}
required
class="input input-bordered"
/>
</div>
)}
<button type="submit" class="btn btn-primary w-full mt-4">{isLogin() ? 'Login' : 'Register'}</button>
</form>
<button onClick={() => setIsLogin(!isLogin())} class="btn btn-link mt-2 w-full">
Switch to {isLogin() ? 'Register' : 'Login'}
</button>
</div>
<form method="dialog" class="modal-backdrop">
<button>Close</button>
</form>
</dialog >
</>
);
};

export default AuthModal;

104 changes: 104 additions & 0 deletions src/components/netaid/getAll.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { createSignal, createEffect } from 'solid-js';
import { client } from "@/utils/trpc";

const UserTableModal = () => {
let modalRef!: HTMLDialogElement;
const [users, setUsers] = createSignal<{ id: string; name: string; aadhar_no: string }[]>([]);
const [isOpen, setIsOpen] = createSignal(false);
const [markedForDeletion, setMarkedForDeletion] = createSignal<string[]>([]);
const [adminPassword, setAdminPassword] = createSignal('');


const fetchUsers = async () => {
try {
const userData = await client.netaid.getAllUsers.query(); // Adjust to your query method
setUsers(userData);
} catch (error) {
console.error("Error fetching users:", error);
}
};

const closeModal = () => {
setIsOpen(false);
modalRef.close();
};

const toggleMarkForDeletion = (userId: string) => {
setMarkedForDeletion((prev) => {
if (prev.includes(userId)) {
return prev.filter((id) => id !== userId);
} else {
return [...prev, userId];
}
});
};


const deleteMarkedUsers = async () => {
if (adminPassword() === "admin") {
try {
// await client.netaid.deleteUsers.mutate(markedForDeletion()); // Adjust to your delete method
setMarkedForDeletion([]); // Clear the list after deletion
closeModal();
alert('Selected users deleted successfully!');
} catch (error) {
console.error("Error deleting users:", error);
alert('Failed to delete users.');
}
} else {
alert('Please enter the correct admin password.');
}
};




return (
<div>
<button class="btn" onClick={async () => {
await fetchUsers();
setIsOpen(true);
modalRef.showModal();
}}>Show Users</button>

<dialog ref={modalRef} class="modal">
<div class="modal-box">
<h3 class="text-lg font-bold">User List</h3>
<table class="table w-full mt-4">
<thead>
<tr>
<th>User ID</th>
<th>Name</th>
<th>Aadhar Number</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{users().map(user => (
<tr key={user.id}>
<td>{user.id}</td>
<td>{user.name}</td>
<td>{user.aadhar_no}</td>
<td>
<button
class="btn btn-xs"
onClick={() => toggleMarkForDeletion(user.id)}>
{markedForDeletion().includes(user.id) ? 'o' : '-'}
</button>
</td>
</tr>
))}

</tbody>
</table>
<div class="modal-action">
<button class="btn" onClick={closeModal}>Close</button>
</div>
</div>
</dialog>
</div>
);
};

export default UserTableModal;

5 changes: 5 additions & 0 deletions src/context/netaid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createClient } from '@supabase/supabase-js';

const supabaseUrl = 'https://izetyjbsztttkcwwswag.supabase.co';
const supabaseKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Iml6ZXR5amJzenR0dGtjd3dzd2FnIiwicm9sZSI6ImFub24iLCJpYXQiOjE2Nzk1OTY2NjEsImV4cCI6MTk5NTE3MjY2MX0.UYLgLfFSlE9qX3105mKHLfkNj75qh67Mk9HQm9szRwk"!;
export const supabase = createClient(supabaseUrl, supabaseKey);
10 changes: 10 additions & 0 deletions src/database/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { drizzle } from 'drizzle-orm/postgres-js'
import * as schema from './schema/netaid'
import postgres from 'postgres'

console.log()
const connectionString = import.meta.env.DATABASE_URI!

// Disable prefetch as it is not supported for "Transaction" pool mode
const client = postgres(connectionString, { prepare: false })
export const db = drizzle(client , {schema});
37 changes: 37 additions & 0 deletions src/database/schema/netaid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { pgTable, bigserial, varchar, bigint, text, uuid, integer, date, foreignKey } from 'drizzle-orm/pg-core';

export const userIDTable = pgTable('netaid-users', {
id: bigserial('id', { mode: 'number' }).primaryKey(),
name: varchar('name', { length: 100 }).notNull(),
password: varchar('password', { length: 100 }).notNull(),
aadhar_no: bigint('aadhar_no', { mode: 'number' }).notNull(),
});

// export const agenciesTable = pgTable('netaid-agencies', {
// name: varchar('name', { length: 100 }).notNull(),
// category: text('category').notNull(),
// address: varchar('address', { length: 200 }).notNull(),
// phone_number: bigint('phone_number', { mode: 'number' }).notNull(),
// email: varchar('email', { length: 100 }).notNull(),
// area: text('area').notNull(),
// agency_id: bigint('agency_ID', { mode: 'number' }).notNull(),
// aid: uuid('aid').notNull(),
// meta: integer('meta').notNull(),
// });
//
// export const metaTable = pgTable('netaid-meta', {
// aid: foreignKey("aid", agenciesTable),
// location: text('location').notNull(),
// resources_available: text('resources_available').notNull(),
// last_activity: date('last_activity').notNull(),
// });
//
// export const userInfoTable = pgTable('netaid-user_info', {
// user_id: foreignKey('user_id', userIDTable),
// state: text('state').notNull(),
// city: text('city').notNull(),
// district: text('district').notNull(),
// postal_address: bigint('postal_address', { mode: 'number' }).notNull(),
// address: varchar('address', { length: 200 }).notNull(),
// });
//
Loading

0 comments on commit e517133

Please sign in to comment.