SETTINGS: Redirect to main on save.

This commit is contained in:
punkfairie 2023-05-13 17:32:43 -07:00
parent d6708ed0ee
commit cbfb2b7e2d
5 changed files with 44 additions and 30 deletions

View file

@ -1,50 +1,51 @@
import type { systemEstimatedValue } from '../@types/edsmResponses' import type { systemEstimatedValue } from '../@types/edsmResponses';
const EventEmitter = require('node:events') const EventEmitter = require('node:events');
import { Log } from './Log'
import { System } from './System' import { Log } from './Log';
import { System } from './System';
export class EDSM extends EventEmitter { export class EDSM extends EventEmitter {
static #instance: EDSM static #instance: EDSM;
private constructor() { private constructor() {
super() super();
} }
static connect(): EDSM { static connect(): EDSM {
if (!EDSM.#instance) { if (!EDSM.#instance) {
EDSM.#instance = new EDSM() EDSM.#instance = new EDSM();
} }
return EDSM.#instance return EDSM.#instance;
} }
/* ---------------------------------------------------------------------------- #request ---- */ /* ---------------------------------------------------------------------------- #request ---- */
// Submit a request to EDSM and return the response as an object // Submit a request to EDSM and return the response as an object
static async #request(url: string, options: {[x: string]: string}): Promise<object|undefined> { static async #request(url: string, options: {[x: string]: string}): Promise<object|undefined> {
let data: object|undefined = undefined let data: object|undefined = undefined;
try { try {
const response = await fetch(url + '?' + new URLSearchParams(options)) const response = await fetch(url + '?' + new URLSearchParams(options));
if (!response.ok) { if (!response.ok) {
throw new Error(`Network error - ${response}`) throw new Error(`Network error - ${response}`);
} }
data = await response.json() data = await response.json();
} catch (err) { } catch (err) {
Log.write(`ERROR - EDSM.request(): ${err}`) Log.write(`ERROR - EDSM.request(): ${err}`);
} }
return data return data;
} }
/* ---------------------------------------------------------------------- getSystemValue ---- */ /* ---------------------------------------------------------------------- getSystemValue ---- */
static async getSystemValue(system: System): Promise<systemEstimatedValue|undefined> { static async getSystemValue(system: System): Promise<systemEstimatedValue|undefined> {
const url = 'https://www.edsm.net/api-system-v1/estimated-value' const url = 'https://www.edsm.net/api-system-v1/estimated-value';
const response = await EDSM.#request(url, {systemName: system.name}) const response = await EDSM.#request(url, {systemName: system.name});
return (response as systemEstimatedValue) return (response as systemEstimatedValue);
} }
} }

View file

@ -62,11 +62,14 @@ export class Settings {
async save(settings: settingsFile): Promise<boolean> { async save(settings: settingsFile): Promise<boolean> {
if (!this.#writing) { if (!this.#writing) {
try { try {
Log.write('Attempting to save changed settings...');
// So we don't try to write again before this one finishes. // So we don't try to write again before this one finishes.
this.#writing = true; this.#writing = true;
await fs.writeFile(this.#file, JSON.stringify(settings)); await fs.writeFile(this.#file, JSON.stringify(settings));
this.#writing = false; this.#writing = false;
Log.write('Settings saved!');
return true; return true;
} catch (err) { } catch (err) {
Log.write(err); Log.write(err);

View file

@ -97,17 +97,17 @@ export class UI {
valuableStyle = 'highlighted'; valuableStyle = 'highlighted';
} }
const row = $('<div>').addClass('row ms-1 me-1'); const row = $('<div>').addClass('row ms-1 me-1 scanned-body');
row.attr('id', body.bodyID); row.attr('id', body.bodyID);
// spacer // spacer
row.appendChild($('<div>').addClass('col-1 system')); row.appendChild($('<div>').addClass('col-1 system spacer'));
// name // name
const name = $('<div>'); const name = $('<div>');
name.addClass(`col-2 text-start system ${chartedStyle} ${valuableStyle}`) name.addClass(`col-2 text-start system ${chartedStyle} ${valuableStyle}`)
name.appendChild($('<i>')).addClass(`flaticon-${body.nameIcon()}`); name.appendChild($('<i>').addClass(`flaticon-${body.nameIcon()}`));
name.appendChild($('<span>')).text(body.simpleName()); name.appendChild($('<span>').text(body.simpleName()));
row.appendChild(name); row.appendChild(name);
// type icon // type icon

View file

@ -38,13 +38,13 @@ journal.watch();
$('#closeBtn').on('click', () => { $('#closeBtn').on('click', () => {
ipcRenderer.send('CLOSE_WINDOW'); ipcRenderer.send('CLOSE_WINDOW');
}) });
/* ----------------------------------------------------------------- settings button handler ---- */ /* ----------------------------------------------------------------- settings button handler ---- */
$('#settingsBtn').on('click', () => { $('#settingsBtn').on('click', () => {
ipcRenderer.send('LOAD_SETTINGS'); ipcRenderer.send('LOAD_SETTINGS');
}) });
/* ------------------------------------------------------------------------- build body list ---- */ /* ------------------------------------------------------------------------- build body list ---- */
@ -56,9 +56,9 @@ journal.once('BUILD_BODY_LIST', () => {
const row = UI.createBodyRow(body); const row = UI.createBodyRow(body);
$('#scans').appendChild(row); $('#scans').appendChild(row);
}) });
} }
}) });
/* ----------------------------------------------------------------- started hyperspace jump ---- */ /* ----------------------------------------------------------------- started hyperspace jump ---- */
@ -75,7 +75,7 @@ journal.on('ENTERED_NEW_SYSTEM', () => {
if ($('#navRoute').children().length !== journal.navRoute.length) { if ($('#navRoute').children().length !== journal.navRoute.length) {
journal.emit('SET_NAV_ROUTE'); journal.emit('SET_NAV_ROUTE');
} }
}) });
/* ---------------------------------------------------------------------- body scan detected ---- */ /* ---------------------------------------------------------------------- body scan detected ---- */
@ -98,7 +98,7 @@ journal.on('BODY_SCANNED', (body, DSS) => {
const row = UI.createBodyRow(body); const row = UI.createBodyRow(body);
$('#scans').appendChild(row); $('#scans').appendChild(row);
} }
}) });
/* --------------------------------------------------------------------------- nav route set ---- */ /* --------------------------------------------------------------------------- nav route set ---- */
@ -116,9 +116,9 @@ journal.on('SET_NAV_ROUTE', () => {
const row = UI.createSystemRow(system); const row = UI.createSystemRow(system);
$('#navRoute').appendChild(row); $('#navRoute').appendChild(row);
} }
}) });
} }
}) });
/* ------------------------------------------------------------------------ system value set ---- */ /* ------------------------------------------------------------------------ system value set ---- */
@ -128,4 +128,4 @@ edsm.on('SYSTEM_APPRAISED', (system) => {
if (systemRow.length > 0) { if (systemRow.length > 0) {
UI.setValue(systemRow, system.estimatedValueMapped); UI.setValue(systemRow, system.estimatedValueMapped);
} }
}) });

View file

@ -33,6 +33,7 @@ $('#maxDistance').attr('value', settings.maxDistance);
$('form').on('submit', async function (event) { $('form').on('submit', async function (event) {
event.preventDefault(); event.preventDefault();
$('.form-error').remove(); $('.form-error').remove();
// TODO disable submit button.
// Retrieve and normalize data. // Retrieve and normalize data.
const formData = new FormData(event.target); const formData = new FormData(event.target);
@ -50,10 +51,15 @@ $('form').on('submit', async function (event) {
if (isNaN(data.maxDistance)) { if (isNaN(data.maxDistance)) {
UI.addFormError('#maxDistance', 'Please enter a number!'); UI.addFormError('#maxDistance', 'Please enter a number!');
errors = true;
} }
// TODO re-enable submit button if errors.
// If no errors, save. // If no errors, save.
if (!errors) { if (!errors) {
// TODO show some sort of saving thing?
let tries = 0; let tries = 0;
do { do {
let result = await settings.save(data); let result = await settings.save(data);
@ -65,5 +71,9 @@ $('form').on('submit', async function (event) {
break; break;
} }
} while (tries < 3); } while (tries < 3);
// Redirect to main window.
ipcRenderer.send('LOAD_MAIN');
} }
}); });