From 1f17cca7ab37d862c053d2685dcc75437c9c984e Mon Sep 17 00:00:00 2001 From: Sudipto Ghosh Date: Fri, 23 Oct 2020 16:08:57 +0530 Subject: [PATCH 1/5] fix: use siteUrl for all calls from the webapp --- webapp/src/actions/index.js | 2 +- webapp/src/client/client.js | 10 +++------- webapp/src/selectors/index.js | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 webapp/src/selectors/index.js diff --git a/webapp/src/actions/index.js b/webapp/src/actions/index.js index 6c6e423..476d848 100644 --- a/webapp/src/actions/index.js +++ b/webapp/src/actions/index.js @@ -15,7 +15,7 @@ export function getAttributes(userID = '') { let data = []; try { - data = await Client.getAttributes(userID); + data = await Client.getAttributes(getState(), userID); } catch (error) { if (error.status === 404) { dispatch({ diff --git a/webapp/src/client/client.js b/webapp/src/client/client.js index 1507ff2..e04c1ec 100644 --- a/webapp/src/client/client.js +++ b/webapp/src/client/client.js @@ -1,15 +1,11 @@ import {Client4} from 'mattermost-redux/client'; import {ClientError} from 'mattermost-redux/client/client4'; -import {id as pluginId} from '../manifest'; +import {getSiteURL} from '../selectors'; export default class Client { - constructor() { - this.url = `/plugins/${pluginId}/api/v1`; - } - - getAttributes = async (userID = '') => { - return this.doGet(`${this.url}/attributes?user_id=` + userID); + getAttributes = async (state = {}, userID = '') => { + return this.doGet(`${getSiteURL(state)}/attributes?user_id=${userID}`); }; doGet = async (url, body, headers = {}) => { diff --git a/webapp/src/selectors/index.js b/webapp/src/selectors/index.js new file mode 100644 index 0000000..6098559 --- /dev/null +++ b/webapp/src/selectors/index.js @@ -0,0 +1,18 @@ +import {getConfig} from 'mattermost-redux/selectors/entities/general'; + +import {id as pluginId} from '../manifest'; + +export const getSiteURL = (state) => { + const config = getConfig(state); + + let basePath = ''; + if (config && config.SiteURL) { + basePath = new URL(config.SiteURL).pathname; + + if (basePath && basePath[basePath.length - 1] === '/') { + basePath = basePath.substr(0, basePath.length - 1); + } + } + + return `${basePath}/plugins/${pluginId}/api/v1`; +}; From 8c9f647287402081ab0a2c36ab9367ee911d5e18 Mon Sep 17 00:00:00 2001 From: Sudipto Ghosh Date: Mon, 26 Oct 2020 20:46:27 +0530 Subject: [PATCH 2/5] set server route on client initialization --- webapp/src/client/client.js | 10 +++++++--- webapp/src/index.js | 21 +++++++++++++++++++-- webapp/src/selectors/index.js | 18 ------------------ 3 files changed, 26 insertions(+), 23 deletions(-) delete mode 100644 webapp/src/selectors/index.js diff --git a/webapp/src/client/client.js b/webapp/src/client/client.js index e04c1ec..2416e89 100644 --- a/webapp/src/client/client.js +++ b/webapp/src/client/client.js @@ -1,11 +1,15 @@ import {Client4} from 'mattermost-redux/client'; import {ClientError} from 'mattermost-redux/client/client4'; -import {getSiteURL} from '../selectors'; +import {id as pluginId} from './manifest'; export default class Client { - getAttributes = async (state = {}, userID = '') => { - return this.doGet(`${getSiteURL(state)}/attributes?user_id=${userID}`); + setServerRoute(url) { + this.url = `${url}/plugins/${pluginId}`; + } + + getAttributes = async (userID = '') => { + return this.doGet(`${this.url}/api/v1/attributes?user_id=${userID}`); }; doGet = async (url, body, headers = {}) => { diff --git a/webapp/src/index.js b/webapp/src/index.js index 52071d6..a5fede3 100644 --- a/webapp/src/index.js +++ b/webapp/src/index.js @@ -1,16 +1,33 @@ -import {id as pluginId} from './manifest'; +import {getConfig} from 'mattermost-redux/selectors/entities/general'; import UserAttribute from './components/user_attribute'; import CustomAttributesSettings from './components/admin_settings/custom_attribute_settings.jsx'; import Reducer from './reducers'; +import Client from './client'; export default class Plugin { - initialize(registry) { + initialize(registry, store) { registry.registerReducer(Reducer); registry.registerPopoverUserAttributesComponent(UserAttribute); registry.registerAdminConsoleCustomSetting('CustomAttributes', CustomAttributesSettings); + Client.setServerRoute(getServerRoute(store.getState())); } } window.registerPlugin(pluginId, new Plugin()); + +const getServerRoute = (state) => { + const config = getConfig(state); + + let basePath = ''; + if (config && config.SiteURL) { + basePath = new URL(config.SiteURL).pathname; + + if (basePath && basePath[basePath.length - 1] === '/') { + basePath = basePath.substr(0, basePath.length - 1); + } + } + + return basePath; +}; diff --git a/webapp/src/selectors/index.js b/webapp/src/selectors/index.js deleted file mode 100644 index 6098559..0000000 --- a/webapp/src/selectors/index.js +++ /dev/null @@ -1,18 +0,0 @@ -import {getConfig} from 'mattermost-redux/selectors/entities/general'; - -import {id as pluginId} from '../manifest'; - -export const getSiteURL = (state) => { - const config = getConfig(state); - - let basePath = ''; - if (config && config.SiteURL) { - basePath = new URL(config.SiteURL).pathname; - - if (basePath && basePath[basePath.length - 1] === '/') { - basePath = basePath.substr(0, basePath.length - 1); - } - } - - return `${basePath}/plugins/${pluginId}/api/v1`; -}; From 0cf8ba650a4031924dda138f1fab846bc8aa2b08 Mon Sep 17 00:00:00 2001 From: Sudipto Ghosh Date: Mon, 26 Oct 2020 20:51:27 +0530 Subject: [PATCH 3/5] fix wrong import from manifest --- webapp/src/client/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webapp/src/client/client.js b/webapp/src/client/client.js index 2416e89..d9f805a 100644 --- a/webapp/src/client/client.js +++ b/webapp/src/client/client.js @@ -1,7 +1,7 @@ import {Client4} from 'mattermost-redux/client'; import {ClientError} from 'mattermost-redux/client/client4'; -import {id as pluginId} from './manifest'; +import {id as pluginId} from '../manifest'; export default class Client { setServerRoute(url) { From d84e1a66b84fa52c68a1e51860d62fd302016558 Mon Sep 17 00:00:00 2001 From: Sudipto Ghosh Date: Mon, 26 Oct 2020 20:54:30 +0530 Subject: [PATCH 4/5] fix missing import from manifest --- webapp/src/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/webapp/src/index.js b/webapp/src/index.js index a5fede3..cf941eb 100644 --- a/webapp/src/index.js +++ b/webapp/src/index.js @@ -1,5 +1,7 @@ import {getConfig} from 'mattermost-redux/selectors/entities/general'; +import {id as pluginId} from './manifest'; + import UserAttribute from './components/user_attribute'; import CustomAttributesSettings from './components/admin_settings/custom_attribute_settings.jsx'; import Reducer from './reducers'; From e8f0b124c56789c6f295ed0a7a7e2e1f270ef2d2 Mon Sep 17 00:00:00 2001 From: Sudipto Ghosh Date: Wed, 28 Oct 2020 09:18:15 +0530 Subject: [PATCH 5/5] fix call to getAttributes() in actions/index.js --- webapp/src/actions/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webapp/src/actions/index.js b/webapp/src/actions/index.js index 476d848..6c6e423 100644 --- a/webapp/src/actions/index.js +++ b/webapp/src/actions/index.js @@ -15,7 +15,7 @@ export function getAttributes(userID = '') { let data = []; try { - data = await Client.getAttributes(getState(), userID); + data = await Client.getAttributes(userID); } catch (error) { if (error.status === 404) { dispatch({