From 3825c7556bbb70967799ad698c0220f5b94cfb6c Mon Sep 17 00:00:00 2001 From: NPS Agent Date: Mon, 11 May 2026 13:42:26 +0930 Subject: [PATCH] first run after refactor of back and frontends --- api.js | 105 +++++++++++++++++++++++++++++++++++++++++++++++++ fix_screens.py | 16 ++++++++ 2 files changed, 121 insertions(+) create mode 100644 api.js create mode 100644 fix_screens.py diff --git a/api.js b/api.js new file mode 100644 index 0000000..621f33f --- /dev/null +++ b/api.js @@ -0,0 +1,105 @@ +class ApiService { + constructor() { + this.baseUrl = `http://${window.location.hostname}:24024`; + this.token = localStorage.getItem('dashy_token'); + this.subscribers = new Set(); + } + + subscribe(fn) { + this.subscribers.add(fn); + return () => this.subscribers.delete(fn); + } + + notify() { + for (const fn of this.subscribers) fn(); + } + + async request(endpoint, options = {}) { + const headers = { + 'Content-Type': 'application/json', + ...options.headers, + }; + if (this.token) { + headers['Authorization'] = `Bearer ${this.token}`; + } + + const response = await fetch(`${this.baseUrl}${endpoint}`, { + ...options, + headers, + }); + + if (!response.ok) { + if (response.status === 401) { + this.logout(); + } + throw new Error(`API Error: ${response.statusText}`); + } + + return response.json(); + } + + async login(id, password) { + const response = await fetch(`${this.baseUrl}/token`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ id, password }), + }); + + if (!response.ok) { + throw new Error('Login failed'); + } + + const data = await response.json(); + this.token = data.access_token; + localStorage.setItem('dashy_token', this.token); + this.notify(); + return data; + } + + logout() { + this.token = null; + localStorage.removeItem('dashy_token'); + this.notify(); + } + + async getUsers() { + return this.request('/users'); + } + + async getTasks() { + return this.request('/tasks'); + } + + async getAudit() { + return this.request('/audit'); + } + + async createTask(taskData) { + const data = await this.request('/tasks', { + method: 'POST', + body: JSON.stringify(taskData), + }); + this.notify(); + return data; + } + + async updateTask(id, updates) { + const data = await this.request(`/tasks/${id}`, { + method: 'PATCH', + body: JSON.stringify(updates), + }); + this.notify(); + return data; + } + + async addAudit(auditData) { + const data = await this.request('/audit', { + method: 'POST', + body: JSON.stringify(auditData), + }); + this.notify(); + return data; + } +} + +window.api = new ApiService(); diff --git a/fix_screens.py b/fix_screens.py new file mode 100644 index 0000000..3d36176 --- /dev/null +++ b/fix_screens.py @@ -0,0 +1,16 @@ +with open('screens.jsx', 'r') as f: + lines = f.readlines() + +out = [] +for line in lines: + if line.startswith('function DatabaseInspector'): + break + out.append(line) + +out.append('Object.assign(window, {\n') +out.append(' LoginScreen, TopBar, OverviewScreen, UserScreen, AddTaskModal, Modal, TaskDetail, AuditScreen, HeadsUp, BrandMark,\n') +out.append(' SettingsScreen,\n') +out.append('});\n') + +with open('screens.jsx', 'w') as f: + f.writelines(out)