Skip to content

Commit 512fbb8

Browse files
[csLib] Now capable of writing the plugin config ! (#469)
Co-authored-by: DogmaDragon <[email protected]>
1 parent 2deddca commit 512fbb8

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

plugins/CommunityScriptsUILibrary/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ All the following functions are exposed under `window.csLib` and `csLib`
4949
*/
5050
```
5151

52+
## setConfiguration
53+
```js
54+
/**
55+
* Set configuration of a plugin in the server via GraphQL
56+
* @param {string} pluginId - The ID of the plugin as it is registered in the server
57+
* @param {*} values - The configuration object with the values you want to save in the server
58+
* @returns {Object} - The configuration object of the plugin as it is stored in the server after update
59+
*
60+
* @example
61+
* // fetch config from the server
62+
* const config = await getConfiguration('CommunityScriptsUIPlugin', defaultConfig);
63+
* // config = { theme: 'dark' }
64+
* // update the config based on user input
65+
* // config = { theme: 'light' }
66+
* // save config in the server
67+
* await setConfiguration('CommunityScriptsUIPlugin', config);
68+
* }
69+
*/
70+
```
71+
5272
## waitForElement
5373
```js
5474
/**

plugins/CommunityScriptsUILibrary/cs-ui-lib.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,35 @@
5454
return response.configuration.plugins?.[pluginId] ?? fallback;
5555
};
5656

57+
/**
58+
* Set configuration of a plugin in the server via GraphQL
59+
* @param {string} pluginId - The ID of the plugin as it is registered in the server
60+
* @param {*} values - The configuration object with the values you want to save in the server
61+
* @returns {Object} - The configuration object of the plugin as it is stored in the server after update
62+
*
63+
* @example
64+
* // fetch config from the server
65+
* const config = await getConfiguration('CommunityScriptsUIPlugin', defaultConfig);
66+
* // config = { theme: 'dark' }
67+
* // update the config based on user input
68+
* // config = { theme: 'light' }
69+
* // save config in the server
70+
* await setConfiguration('CommunityScriptsUIPlugin', config);
71+
* }
72+
*/
73+
const setConfiguration = async (pluginId, values) => {
74+
const query = `mutation ConfigurePlugin($pluginId: ID!, $input: Map!) { configurePlugin(plugin_id: $pluginId, input: $input) }`;
75+
const queryBody = {
76+
query: query,
77+
variables: {
78+
pluginId: pluginId,
79+
input: values,
80+
},
81+
};
82+
const response = await csLib.callGQL({ ...queryBody });
83+
return response.configurePlugin;
84+
};
85+
5786
/**
5887
* Waits for an element to be available in the DOM and runs the callback function once it is
5988
* @param {string} selector - The CSS selector of the element to wait for
@@ -105,6 +134,7 @@
105134
baseURL,
106135
callGQL,
107136
getConfiguration,
137+
setConfiguration,
108138
waitForElement,
109139
PathElementListener,
110140
};

0 commit comments

Comments
 (0)