update settings

This commit is contained in:
Alex Holliday
2024-09-27 12:13:39 +08:00
parent 488ba717ac
commit 5c4d094da7
6 changed files with 126 additions and 13 deletions
+19 -3
View File
@@ -1,6 +1,16 @@
const LOG_LEVEL = import.meta.env.VITE_APP_LOG_LEVEL;
import store from "../store";
const LOG_LEVEL = import.meta.env.VITE_APP_LOG_LEVEL || "debug";
class Logger {
constructor(logLevel) {
constructor() {
let logLevel = LOG_LEVEL;
this.unsubscribe = store.subscribe(() => {
const state = store.getState();
logLevel = state.settings.logLevel || "debug";
this.updateLogLevel(logLevel);
});
}
updateLogLevel(logLevel) {
const NO_OP = () => {};
if (logLevel === "none") {
@@ -25,6 +35,12 @@ class Logger {
}
this.log = console.log.bind(console);
}
cleanup() {
if (this.unsubscribe) {
this.unsubscribe();
}
}
}
export const logger = new Logger(LOG_LEVEL);
export const logger = new Logger();
+41 -2
View File
@@ -1,10 +1,18 @@
import axios from "axios";
const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL;
const BASE_URL =
import.meta.env.VITE_APP_API_BASE_URL || "http://localhost:5000/api/v1";
import { logger } from "./Logger";
class NetworkService {
constructor(store) {
this.store = store;
this.axiosInstance = axios.create({ baseURL: BASE_URL });
let baseURL = BASE_URL;
this.axiosInstance = axios.create();
this.setBaseUrl(baseURL);
this.unsubscribe = store.subscribe(() => {
const state = store.getState();
baseURL = state.settings.apiBaseUrl || BASE_URL;
this.setBaseUrl(baseURL);
});
this.axiosInstance.interceptors.response.use(
(response) => response,
(error) => {
@@ -17,6 +25,16 @@ class NetworkService {
);
}
setBaseUrl = (url) => {
this.axiosInstance.defaults.baseURL = url;
};
cleanup() {
if (this.unsubscribe) {
this.unsubscribe();
}
}
/**
*
* ************************************
@@ -640,6 +658,27 @@ class NetworkService {
},
});
}
/**
*
* ************************************
* Create a new monitor
* ************************************
*
* @async
* @param {Object} config - The configuration object.
* @param {string} config.authToken - The authorization token to be used in the request header.
* @param {Object} config.settings - The monitor object to be sent in the request body.
* @returns {Promise<AxiosResponse>} The response from the axios POST request.
*/
async updateAppSettings(config) {
return this.axiosInstance.put(`/settings`, config.settings, {
headers: {
Authorization: `Bearer ${config.authToken}`,
"Content-Type": "application/json",
},
});
}
}
export default NetworkService;