-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseCategories.js
More file actions
73 lines (62 loc) · 2.36 KB
/
useCategories.js
File metadata and controls
73 lines (62 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { useState, useEffect } from 'react'
import getDbConnection from '../db/dbConnection'
const useCategories = () => {
const [moreCategories, setMoreCategories] = useState(['Work', 'Personal'])
const getCategories = async () => {
let db = null;
try {
db = getDbConnection();
const tags = await db.sql('SELECT * FROM tags')
const filteredTags = tags.filter(tag => {
return tag['name'] !== 'Work' && tag['name'] !== 'Personal'
})
setMoreCategories(prevCategories => [...prevCategories, ...filteredTags.map(tag => tag.name)])
} catch (error) {
console.error('Error getting tags/categories', error)
}
}
const addCategory = async newCategory => {
let db = null;
try {
db = getDbConnection();
await db.sql('INSERT INTO tags (name) VALUES (?) RETURNING *', newCategory)
setMoreCategories(prevCategories => [...prevCategories, newCategory])
} catch (error) {
console.error('Error adding category', error)
} finally {
db?.close();
}
}
const initializeTables = async () => {
let db = null;
try {
db = getDbConnection();
const createTasksTable = await db.sql(
'CREATE TABLE IF NOT EXISTS tasks (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, isCompleted INT NOT NULL);'
)
const createTagsTable = await db.sql('CREATE TABLE IF NOT EXISTS tags (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, UNIQUE(name));')
const createTagsTasksTable = await db.sql(
'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));'
)
if (createTasksTable === 'OK' && createTagsTable === 'OK' && createTagsTasksTable === 'OK') {
console.log('Successfully created tables')
await db.sql('INSERT OR IGNORE INTO tags (name) VALUES (?)', 'Work')
await db.sql('INSERT OR IGNORE INTO tags (name) VALUES (?)', 'Personal')
getCategories()
}
} catch (error) {
console.error('Error creating tables', error)
} finally {
db?.close();
}
}
useEffect(() => {
initializeTables()
}, [])
return {
moreCategories,
addCategory,
getCategories
}
}
export default useCategories