Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Functions.config* to a module
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
- Loading branch information
1 parent
5348e09
commit 75dbb2c
Showing
5 changed files
with
111 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import $ from 'jquery'; | ||
import { ajaxShowMessage } from '../ajax-message.js'; | ||
import { CommonParams } from '../common.js'; | ||
|
||
/** | ||
* Sets a configuration value. | ||
* | ||
* A configuration value may be set in both browser's local storage and | ||
* remotely in server's configuration table. | ||
* | ||
* NOTE: Depending on server's configuration, the configuration table may be or | ||
* not persistent. | ||
* | ||
* @param {string} key Configuration key. | ||
* @param {object} value Configuration value. | ||
* | ||
* @return {void} | ||
*/ | ||
export function setConfigValue (key, value) { | ||
// Updating value in local storage. | ||
var serialized = JSON.stringify(value); | ||
localStorage.setItem(key, serialized); | ||
|
||
$.ajax({ | ||
url: 'index.php?route=/config/set', | ||
type: 'POST', | ||
dataType: 'json', | ||
data: { | ||
'ajax_request': true, | ||
key: key, | ||
server: CommonParams.get('server'), | ||
value: serialized, | ||
}, | ||
success: function (data) { | ||
if (data.success !== true) { | ||
// Try to find a message to display | ||
if (data.error || data.message || false) { | ||
ajaxShowMessage(data.error || data.message); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Gets a configuration value. A configuration value will be searched in | ||
* browser's local storage first and if not found, a call to the server will be | ||
* made. | ||
* | ||
* If value should not be cached and the up-to-date configuration value from | ||
* right from the server is required, the third parameter should be `false`. | ||
* | ||
* @param {string} key Configuration key. | ||
* @param {boolean} cached Configuration type. | ||
* @param {Function} successCallback The callback to call after the value is successfully received | ||
* @param {Function} failureCallback The callback to call when the value can not be received | ||
* | ||
* @return {void} | ||
*/ | ||
export function getConfigValue (key, cached, successCallback, failureCallback) { | ||
var isCached = (typeof cached !== 'undefined') ? cached : true; | ||
var value = localStorage.getItem(key); | ||
if (isCached && value !== undefined && value !== null) { | ||
return JSON.parse(value); | ||
} | ||
|
||
// Result not found in local storage or ignored. | ||
// Hitting the server. | ||
$.ajax({ | ||
url: 'index.php?route=/config/get', | ||
type: 'POST', | ||
dataType: 'json', | ||
data: { | ||
'ajax_request': true, | ||
server: CommonParams.get('server'), | ||
key: key | ||
}, | ||
success: function (data) { | ||
if (data.success !== true) { | ||
// Try to find a message to display | ||
if (data.error || data.message || false) { | ||
ajaxShowMessage(data.error || data.message); | ||
} | ||
|
||
// Call the callback if it is defined | ||
if (typeof failureCallback === 'function') { | ||
failureCallback(); | ||
} | ||
|
||
// return here, exit non success mode | ||
return; | ||
} | ||
|
||
// Updating value in local storage. | ||
localStorage.setItem(key, JSON.stringify(data.value)); | ||
// Call the callback if it is defined | ||
if (typeof successCallback === 'function') { | ||
// Feed it the value previously saved like on async mode | ||
successCallback(JSON.parse(localStorage.getItem(key))); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters