Skip to content

Commit

Permalink
Client support for saving/loading setttings
Browse files Browse the repository at this point in the history
Added ability to save and load settings when logged out, using localStorage in the browser.
  • Loading branch information
Neriderc committed Jan 17, 2023
1 parent c2895c1 commit 66c718a
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 71 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ It's all manual currently, please [create an issue](https://github.com/Neriderc/

If you're keen and available to do testing of pull requests or regression testing before releases, please [create an issue](https://github.com/Neriderc/GVExport/issues) and volunteer.

We would also happily accept automated tests if that's your thing.
We would also happily accept automated tests if that's your thing. We have been experimenting with Cypress for functional testing.

# How to report a bug
If you find a security vulnerability, do NOT open an issue. See [SECURITY.md](https://github.com/Neriderc/GVExport/blob/master/SECURITY.md)) for the email address to contact.
Expand Down
21 changes: 18 additions & 3 deletions app/ApiHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function handle($request, $module, $tree) {
case "is_logged_in":
$this->isLoggedIn();
break;
case "get_tree_name":
$this->getTreeName($tree);
break;
default:
$this->response_data['success'] = false;
$this->response_data['json'] = $json_data;
Expand Down Expand Up @@ -84,10 +87,17 @@ public function getSettings($json, $module, $tree, string $json_data): void
public function deleteSettings($json, $tree, string $json_data): void
{
if (isset($json['settings_id']) && ctype_alnum($json['settings_id']) && !in_array($json['settings_id'], [Settings::ID_ALL_SETTINGS, Settings::ID_MAIN_SETTINGS])) {
$settings = new Settings();
$settings->deleteUserSettings($tree, $json['settings_id']);
$this->response_data['success'] = true;
if (Settings::isUserLoggedIn()) {
$settings = new Settings();
$settings->deleteUserSettings($tree, $json['settings_id']);
$this->response_data['success'] = true;
} else {
// Is user is not logged in, we should never have got this far
$this->response_data['success'] = false;
$this->response_data['errorMessage'] = I18N::translate('Invalid');
}
} else {
$this->response_data['success'] = false;
$this->response_data['json'] = $json_data;
$this->response_data['errorMessage'] = I18N::translate('Invalid settings ID');
}
Expand All @@ -102,4 +112,9 @@ private function isLoggedIn()
}
$this->response_data['success'] = true;
}
private function getTreeName($tree)
{
$this->response_data['treeName'] = e($tree->name());
$this->response_data['success'] = true;
}
}
13 changes: 6 additions & 7 deletions app/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,7 @@ public function saveUserSettings($tree, $settings, string $id = self::ID_MAIN_SE

public function deleteUserSettings($tree, $id) {
$id_suffix = ($id === self::ID_MAIN_SETTINGS ? "" : "_" . $id);
if (Auth::user()->id() == self::GUEST_USER_ID) {
// No multi-setting option if user logged-out yet
} else {
if (Settings::isUserLoggedIn()) {
$settings = $this->defaultSettings;
foreach ($settings as $preference => $value) {
if (self::shouldSaveSetting($preference)) {
Expand Down Expand Up @@ -379,14 +377,15 @@ public function getAllSettingsJson($module, $tree)
foreach ($ids as $id_value) {
if ($id_value != "") {
$userSettings = $this->loadUserSettings($module, $tree, $id_value);
$settings_list[$id_value]['name'] = $userSettings['save_settings_name'];
$settings_list[$id_value]['id'] = $id_value;
$settings_list[$id_value]['settings'] = [];
$settings_list[(string) $id_value]['name'] = $userSettings['save_settings_name'];
$settings_list[(string) $id_value]['id'] = $id_value;
$settings = array();
foreach ($this->defaultSettings as $preference => $value) {
if (self::shouldSaveSetting($preference)) {
$settings_list[$id_value]['settings'][$preference] = $userSettings[$preference];
$settings[$preference] = $userSettings[$preference];
}
}
$settings_list[(string) $id_value]['settings'] = json_encode($settings);
}
}
}
Expand Down
Loading

0 comments on commit 66c718a

Please sign in to comment.