SETTINGS: Redirect to main on save.
This commit is contained in:
parent
d6708ed0ee
commit
cbfb2b7e2d
5 changed files with 44 additions and 30 deletions
|
@ -1,50 +1,51 @@
|
|||
import type { systemEstimatedValue } from '../@types/edsmResponses'
|
||||
import type { systemEstimatedValue } from '../@types/edsmResponses';
|
||||
|
||||
const EventEmitter = require('node:events')
|
||||
import { Log } from './Log'
|
||||
import { System } from './System'
|
||||
const EventEmitter = require('node:events');
|
||||
|
||||
import { Log } from './Log';
|
||||
import { System } from './System';
|
||||
|
||||
export class EDSM extends EventEmitter {
|
||||
static #instance: EDSM
|
||||
static #instance: EDSM;
|
||||
|
||||
private constructor() {
|
||||
super()
|
||||
super();
|
||||
}
|
||||
|
||||
static connect(): EDSM {
|
||||
if (!EDSM.#instance) {
|
||||
EDSM.#instance = new EDSM()
|
||||
EDSM.#instance = new EDSM();
|
||||
}
|
||||
|
||||
return EDSM.#instance
|
||||
return EDSM.#instance;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------------- #request ---- */
|
||||
|
||||
// Submit a request to EDSM and return the response as an object
|
||||
static async #request(url: string, options: {[x: string]: string}): Promise<object|undefined> {
|
||||
let data: object|undefined = undefined
|
||||
let data: object|undefined = undefined;
|
||||
|
||||
try {
|
||||
const response = await fetch(url + '?' + new URLSearchParams(options))
|
||||
const response = await fetch(url + '?' + new URLSearchParams(options));
|
||||
|
||||
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) {
|
||||
Log.write(`ERROR - EDSM.request(): ${err}`)
|
||||
Log.write(`ERROR - EDSM.request(): ${err}`);
|
||||
}
|
||||
|
||||
return data
|
||||
return data;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- getSystemValue ---- */
|
||||
|
||||
static async getSystemValue(system: System): Promise<systemEstimatedValue|undefined> {
|
||||
const url = 'https://www.edsm.net/api-system-v1/estimated-value'
|
||||
const response = await EDSM.#request(url, {systemName: system.name})
|
||||
return (response as systemEstimatedValue)
|
||||
const url = 'https://www.edsm.net/api-system-v1/estimated-value';
|
||||
const response = await EDSM.#request(url, {systemName: system.name});
|
||||
return (response as systemEstimatedValue);
|
||||
}
|
||||
}
|
|
@ -62,11 +62,14 @@ export class Settings {
|
|||
async save(settings: settingsFile): Promise<boolean> {
|
||||
if (!this.#writing) {
|
||||
try {
|
||||
Log.write('Attempting to save changed settings...');
|
||||
|
||||
// So we don't try to write again before this one finishes.
|
||||
this.#writing = true;
|
||||
await fs.writeFile(this.#file, JSON.stringify(settings));
|
||||
this.#writing = false;
|
||||
|
||||
Log.write('Settings saved!');
|
||||
return true;
|
||||
} catch (err) {
|
||||
Log.write(err);
|
||||
|
|
|
@ -97,17 +97,17 @@ export class UI {
|
|||
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);
|
||||
|
||||
// spacer
|
||||
row.appendChild($('<div>').addClass('col-1 system'));
|
||||
row.appendChild($('<div>').addClass('col-1 system spacer'));
|
||||
|
||||
// name
|
||||
const name = $('<div>');
|
||||
name.addClass(`col-2 text-start system ${chartedStyle} ${valuableStyle}`)
|
||||
name.appendChild($('<i>')).addClass(`flaticon-${body.nameIcon()}`);
|
||||
name.appendChild($('<span>')).text(body.simpleName());
|
||||
name.appendChild($('<i>').addClass(`flaticon-${body.nameIcon()}`));
|
||||
name.appendChild($('<span>').text(body.simpleName()));
|
||||
row.appendChild(name);
|
||||
|
||||
// type icon
|
||||
|
|
|
@ -38,13 +38,13 @@ journal.watch();
|
|||
|
||||
$('#closeBtn').on('click', () => {
|
||||
ipcRenderer.send('CLOSE_WINDOW');
|
||||
})
|
||||
});
|
||||
|
||||
/* ----------------------------------------------------------------- settings button handler ---- */
|
||||
|
||||
$('#settingsBtn').on('click', () => {
|
||||
ipcRenderer.send('LOAD_SETTINGS');
|
||||
})
|
||||
});
|
||||
|
||||
/* ------------------------------------------------------------------------- build body list ---- */
|
||||
|
||||
|
@ -56,9 +56,9 @@ journal.once('BUILD_BODY_LIST', () => {
|
|||
const row = UI.createBodyRow(body);
|
||||
|
||||
$('#scans').appendChild(row);
|
||||
})
|
||||
});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
/* ----------------------------------------------------------------- started hyperspace jump ---- */
|
||||
|
||||
|
@ -75,7 +75,7 @@ journal.on('ENTERED_NEW_SYSTEM', () => {
|
|||
if ($('#navRoute').children().length !== journal.navRoute.length) {
|
||||
journal.emit('SET_NAV_ROUTE');
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
/* ---------------------------------------------------------------------- body scan detected ---- */
|
||||
|
||||
|
@ -98,7 +98,7 @@ journal.on('BODY_SCANNED', (body, DSS) => {
|
|||
const row = UI.createBodyRow(body);
|
||||
$('#scans').appendChild(row);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
/* --------------------------------------------------------------------------- nav route set ---- */
|
||||
|
||||
|
@ -116,9 +116,9 @@ journal.on('SET_NAV_ROUTE', () => {
|
|||
const row = UI.createSystemRow(system);
|
||||
$('#navRoute').appendChild(row);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
/* ------------------------------------------------------------------------ system value set ---- */
|
||||
|
||||
|
@ -128,4 +128,4 @@ edsm.on('SYSTEM_APPRAISED', (system) => {
|
|||
if (systemRow.length > 0) {
|
||||
UI.setValue(systemRow, system.estimatedValueMapped);
|
||||
}
|
||||
})
|
||||
});
|
|
@ -33,6 +33,7 @@ $('#maxDistance').attr('value', settings.maxDistance);
|
|||
$('form').on('submit', async function (event) {
|
||||
event.preventDefault();
|
||||
$('.form-error').remove();
|
||||
// TODO disable submit button.
|
||||
|
||||
// Retrieve and normalize data.
|
||||
const formData = new FormData(event.target);
|
||||
|
@ -50,10 +51,15 @@ $('form').on('submit', async function (event) {
|
|||
|
||||
if (isNaN(data.maxDistance)) {
|
||||
UI.addFormError('#maxDistance', 'Please enter a number!');
|
||||
errors = true;
|
||||
}
|
||||
|
||||
// TODO re-enable submit button if errors.
|
||||
|
||||
// If no errors, save.
|
||||
if (!errors) {
|
||||
// TODO show some sort of saving thing?
|
||||
|
||||
let tries = 0;
|
||||
do {
|
||||
let result = await settings.save(data);
|
||||
|
@ -65,5 +71,9 @@ $('form').on('submit', async function (event) {
|
|||
break;
|
||||
}
|
||||
} while (tries < 3);
|
||||
|
||||
// Redirect to main window.
|
||||
ipcRenderer.send('LOAD_MAIN');
|
||||
}
|
||||
|
||||
});
|
Loading…
Reference in a new issue