|
1 |
| -import { useState, useEffect } from "react"; |
2 |
| -import getDbConnection from "../db/dbConnection"; |
| 1 | +import { useState, useEffect } from 'react' |
| 2 | +import getDbConnection from '../db/dbConnection' |
3 | 3 |
|
4 | 4 | const useCategories = () => {
|
5 |
| - const [moreCategories, setMoreCategories] = useState(["Work", "Personal"]); |
| 5 | + const [moreCategories, setMoreCategories] = useState(['Work', 'Personal']) |
6 | 6 |
|
7 | 7 | const getCategories = async () => {
|
| 8 | + let db = null; |
8 | 9 | try {
|
9 |
| - const tags = await getDbConnection().sql("SELECT * FROM tags"); |
10 |
| - const filteredTags = tags.filter((tag) => { |
11 |
| - return tag["name"] !== "Work" && tag["name"] !== "Personal"; |
12 |
| - }); |
13 |
| - setMoreCategories((prevCategories) => [ |
14 |
| - ...prevCategories, |
15 |
| - ...filteredTags.map((tag) => tag.name), |
16 |
| - ]); |
| 10 | + const tags = await db.sql('SELECT * FROM tags') |
| 11 | + const filteredTags = tags.filter(tag => { |
| 12 | + return tag['name'] !== 'Work' && tag['name'] !== 'Personal' |
| 13 | + }) |
| 14 | + setMoreCategories(prevCategories => [...prevCategories, ...filteredTags.map(tag => tag.name)]) |
17 | 15 | } catch (error) {
|
18 |
| - console.error("Error getting tags/categories", error); |
| 16 | + console.error('Error getting tags/categories', error) |
19 | 17 | }
|
20 |
| - }; |
| 18 | + } |
21 | 19 |
|
22 |
| - const addCategory = async (newCategory) => { |
| 20 | + const addCategory = async newCategory => { |
| 21 | + let db = null |
23 | 22 | try {
|
24 |
| - await getDbConnection().sql( |
25 |
| - "INSERT INTO tags (name) VALUES (?) RETURNING *", |
26 |
| - newCategory |
27 |
| - ); |
28 |
| - setMoreCategories((prevCategories) => [...prevCategories, newCategory]); |
| 23 | + db = getDbConnection() |
| 24 | + await db.sql('INSERT INTO tags (name) VALUES (?) RETURNING *', newCategory) |
| 25 | + setMoreCategories(prevCategories => [...prevCategories, newCategory]) |
29 | 26 | } catch (error) {
|
30 |
| - console.error("Error adding category", error); |
| 27 | + console.error('Error adding category', error) |
| 28 | + } finally { |
| 29 | + db?.close(); |
31 | 30 | }
|
32 |
| - }; |
| 31 | + } |
33 | 32 |
|
34 | 33 | const initializeTables = async () => {
|
| 34 | + let db = null |
35 | 35 | try {
|
36 |
| - const createTasksTable = await getDbConnection().sql( |
37 |
| - "CREATE TABLE IF NOT EXISTS tasks (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, isCompleted INT NOT NULL);" |
38 |
| - ); |
| 36 | + db = getDbConnection() |
| 37 | + const createTasksTable = await db.sql( |
| 38 | + 'CREATE TABLE IF NOT EXISTS tasks (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, isCompleted INT NOT NULL);' |
| 39 | + ) |
39 | 40 |
|
40 |
| - const createTagsTable = await getDbConnection().sql( |
41 |
| - "CREATE TABLE IF NOT EXISTS tags (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, UNIQUE(name));" |
42 |
| - ); |
| 41 | + const createTagsTable = await db.sql('CREATE TABLE IF NOT EXISTS tags (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, UNIQUE(name));') |
43 | 42 |
|
44 |
| - const createTagsTasksTable = await getDbConnection().sql( |
45 |
| - "CREATE TABLE IF NOT EXISTS tasks_tags (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, task_id INTEGER NOT NULL, tag_id INTEGER NOT NULL, FOREIGN KEY (task_id) REFERENCES tasks(id), FOREIGN KEY (tag_id) REFERENCES tags(id));" |
46 |
| - ); |
| 43 | + const createTagsTasksTable = await db.sql( |
| 44 | + 'CREATE TABLE IF NOT EXISTS tasks_tags (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, task_id INTEGER NOT NULL, tag_id INTEGER NOT NULL, FOREIGN KEY (task_id) REFERENCES tasks(id), FOREIGN KEY (tag_id) REFERENCES tags(id));' |
| 45 | + ) |
47 | 46 |
|
48 |
| - if ( |
49 |
| - createTasksTable === "OK" && |
50 |
| - createTagsTable === "OK" && |
51 |
| - createTagsTasksTable === "OK" |
52 |
| - ) { |
53 |
| - console.log("Successfully created tables"); |
| 47 | + if (createTasksTable === 'OK' && createTagsTable === 'OK' && createTagsTasksTable === 'OK') { |
| 48 | + console.log('Successfully created tables') |
54 | 49 |
|
55 |
| - await getDbConnection().sql("INSERT OR IGNORE INTO tags (name) VALUES (?)", "Work"); |
56 |
| - await getDbConnection().sql( |
57 |
| - "INSERT OR IGNORE INTO tags (name) VALUES (?)", |
58 |
| - "Personal" |
59 |
| - ); |
60 |
| - getCategories(); |
| 50 | + await db.sql('INSERT OR IGNORE INTO tags (name) VALUES (?)', 'Work') |
| 51 | + await db.sql('INSERT OR IGNORE INTO tags (name) VALUES (?)', 'Personal') |
| 52 | + getCategories() |
61 | 53 | }
|
62 | 54 | } catch (error) {
|
63 |
| - console.error("Error creating tables", error); |
| 55 | + console.error('Error creating tables', error) |
| 56 | + } finally { |
| 57 | + db?.close(); |
64 | 58 | }
|
65 |
| - }; |
| 59 | + } |
66 | 60 |
|
67 | 61 | useEffect(() => {
|
68 |
| - initializeTables(); |
69 |
| - }, []); |
| 62 | + initializeTables() |
| 63 | + }, []) |
70 | 64 |
|
71 | 65 | return {
|
72 | 66 | moreCategories,
|
73 | 67 | addCategory,
|
74 |
| - getCategories, |
75 |
| - }; |
76 |
| -}; |
| 68 | + getCategories |
| 69 | + } |
| 70 | +} |
77 | 71 |
|
78 |
| -export default useCategories; |
| 72 | +export default useCategories |
0 commit comments