-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor code to have api and database module
- Loading branch information
Showing
4 changed files
with
122 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { db } from "@/db/local-storage" | ||
import { Result } from "@/schemas/result-schema" | ||
|
||
export function addResult() { | ||
const currentData = db.getData() | ||
const maxId = Math.max(...currentData.map(item => item.id), -1) | ||
const newResult: Result = { | ||
id: maxId + 1, | ||
unitCode: "", | ||
creditPoints: 6, | ||
mark: 0, | ||
grade: "N", | ||
} | ||
return db.setData([...currentData, newResult]) | ||
} | ||
|
||
export function updateResult(id: number, updatedResult: Result) { | ||
const currentData = db.getData() | ||
const newData = currentData.map(item => | ||
item.id === id ? { ...updatedResult, id } : item | ||
) | ||
return db.setData(newData) | ||
} | ||
|
||
export function deleteResult(id: number) { | ||
const currentData = db.getData() | ||
const newData = currentData.filter(item => item.id !== id) | ||
return db.setData(newData) | ||
} | ||
|
||
export function restoreResult(itemToRestore: Result) { | ||
const currentData = db.getData() | ||
if (currentData.some(item => item.id === itemToRestore.id)) { | ||
return currentData | ||
} | ||
|
||
const insertIndex = currentData.findIndex(item => item.id > itemToRestore.id) | ||
const newData = [...currentData] | ||
if (insertIndex === -1) { | ||
newData.push(itemToRestore) | ||
} else { | ||
newData.splice(insertIndex, 0, itemToRestore) | ||
} | ||
return db.setData(newData) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Result } from "@/schemas/result-schema" | ||
import { STORAGE_KEYS } from "@/constants/storage-keys" | ||
|
||
const initialData: Result[] = [ | ||
{ | ||
id: 0, | ||
unitCode: "ECE2072", | ||
creditPoints: 6, | ||
mark: 50, | ||
grade: "P", | ||
}, | ||
{ | ||
id: 1, | ||
unitCode: "FIT2107", | ||
creditPoints: 6, | ||
mark: 64, | ||
grade: "C", | ||
}, | ||
{ | ||
id: 2, | ||
unitCode: "FIT3170", | ||
creditPoints: 12, | ||
mark: 77, | ||
grade: "D", | ||
}, | ||
{ | ||
id: 3, | ||
unitCode: "FIT2095", | ||
creditPoints: 6, | ||
mark: 99, | ||
grade: "HD", | ||
}, | ||
] | ||
|
||
export function localStorage<T>(key: string, initialValue: T) { | ||
const getData = (): T => { | ||
try { | ||
const item = window.localStorage.getItem(key) | ||
return item ? JSON.parse(item) as T : initialValue | ||
} catch (error) { | ||
console.warn(`Error reading localStorage key "${key}":`, error) | ||
return initialValue | ||
} | ||
} | ||
|
||
const setData = (value: T): T => { | ||
try { | ||
window.localStorage.setItem(key, JSON.stringify(value)) | ||
return value | ||
} catch (error) { | ||
console.warn(`Error setting localStorage key "${key}":`, error) | ||
return value | ||
} | ||
} | ||
|
||
return { getData, setData } | ||
} | ||
|
||
export const db = localStorage<Result[]>(STORAGE_KEYS.RESULTS, initialData) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters