-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalStorageService.ts
More file actions
113 lines (91 loc) · 3.52 KB
/
localStorageService.ts
File metadata and controls
113 lines (91 loc) · 3.52 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { Student, User } from './types';
const STORAGE_KEY_TEACHER_NAME = 'phonicstrack_teacher_name';
const STORAGE_KEY_STUDENTS = 'phonicstrack_students';
const STORAGE_KEY_CURRENT_USER = 'phonicstrack_current_user';
// Simple in-memory store for current user (cleared on browser refresh)
let currentUser: User | null = null;
// Initialize stored data if needed
const initializeStorage = () => {
if (!localStorage.getItem(STORAGE_KEY_STUDENTS)) {
localStorage.setItem(STORAGE_KEY_STUDENTS, JSON.stringify([]));
}
};
export const localStorageService = {
getCurrentUser: (): User | null => {
if (currentUser) return currentUser;
const stored = localStorage.getItem(STORAGE_KEY_CURRENT_USER);
if (stored) {
try {
currentUser = JSON.parse(stored);
return currentUser;
} catch {
return null;
}
}
return null;
},
// Get teacher name
getTeacherName: (): string => {
return localStorage.getItem(STORAGE_KEY_TEACHER_NAME) || 'Teacher';
},
// Update teacher name
updateTeacherName: (name: string): void => {
localStorage.setItem(STORAGE_KEY_TEACHER_NAME, name);
},
// Student methods
getStudentsByUserId: (userId: string): Student[] => {
initializeStorage();
const students = JSON.parse(localStorage.getItem(STORAGE_KEY_STUDENTS) || '[]') as Student[];
return students.filter(s => s.userId === userId);
},
addStudent: (student: Omit<Student, 'id'>): Student => {
initializeStorage();
const students = JSON.parse(localStorage.getItem(STORAGE_KEY_STUDENTS) || '[]') as Student[];
const id = `student_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const newStudent: Student = { ...student, id };
students.push(newStudent);
localStorage.setItem(STORAGE_KEY_STUDENTS, JSON.stringify(students));
return newStudent;
},
updateStudent: (student: Student): Student => {
initializeStorage();
const students = JSON.parse(localStorage.getItem(STORAGE_KEY_STUDENTS) || '[]') as Student[];
const index = students.findIndex(s => s.id === student.id);
if (index === -1) {
throw new Error('Student not found');
}
students[index] = student;
localStorage.setItem(STORAGE_KEY_STUDENTS, JSON.stringify(students));
return student;
},
deleteStudent: (studentId: string): void => {
initializeStorage();
const students = JSON.parse(localStorage.getItem(STORAGE_KEY_STUDENTS) || '[]') as Student[];
const filtered = students.filter(s => s.id !== studentId);
localStorage.setItem(STORAGE_KEY_STUDENTS, JSON.stringify(filtered));
},
// Batch operations
addMultipleStudents: (studentsData: Omit<Student, 'id'>[]): Student[] => {
initializeStorage();
const students = JSON.parse(localStorage.getItem(STORAGE_KEY_STUDENTS) || '[]') as Student[];
const newStudents = studentsData.map(data => ({
...data,
id: `student_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
}));
students.push(...newStudents);
localStorage.setItem(STORAGE_KEY_STUDENTS, JSON.stringify(students));
return newStudents;
},
// Clear all data (for dev/testing)
clearAllData: (): void => {
localStorage.removeItem(STORAGE_KEY_TEACHER_NAME);
localStorage.removeItem(STORAGE_KEY_STUDENTS);
localStorage.removeItem(STORAGE_KEY_CURRENT_USER);
currentUser = null;
},
// Remove only students data (preserve teacher name/current user)
clearStudents: (): void => {
localStorage.removeItem(STORAGE_KEY_STUDENTS);
initializeStorage();
}
};