From 8ec0e6e82a01dff8e00cf3f6ed8493823c0f28c4 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 1 Apr 2020 15:05:52 -0500 Subject: [PATCH] Import Instance into store --- app/gabsocial/actions/instance.js | 29 +++++++++++++++++++++++++++ app/gabsocial/containers/gabsocial.js | 4 +++- app/gabsocial/reducers/index.js | 2 ++ app/gabsocial/reducers/instance.js | 13 ++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 app/gabsocial/actions/instance.js create mode 100644 app/gabsocial/reducers/instance.js diff --git a/app/gabsocial/actions/instance.js b/app/gabsocial/actions/instance.js new file mode 100644 index 000000000..3a828d64a --- /dev/null +++ b/app/gabsocial/actions/instance.js @@ -0,0 +1,29 @@ +import api from '../api'; + +export const INSTANCE_IMPORT = 'INSTANCE_IMPORT'; +export const INSTANCE_FAIL = 'INSTANCE_FAIL'; + +export function fetchInstance() { + return (dispatch, getState) => { + api(getState).get(`/api/v1/instance`).then(response => { + dispatch(importInstance(response.data)); + }).catch(error => { + dispatch(instanceFail(error)); + }); + }; +} + +export function importInstance(instance) { + return { + type: INSTANCE_IMPORT, + instance + }; +} + +export function instanceFail(error) { + return { + type: INSTANCE_FAIL, + error, + skipAlert: true, + }; +}; diff --git a/app/gabsocial/containers/gabsocial.js b/app/gabsocial/containers/gabsocial.js index a54b09b90..ab99f0b80 100644 --- a/app/gabsocial/containers/gabsocial.js +++ b/app/gabsocial/containers/gabsocial.js @@ -17,6 +17,7 @@ import { getLocale } from '../locales'; import initialState from '../initial_state'; import { me } from '../initial_state'; import ErrorBoundary from '../components/error_boundary'; +import { fetchInstance } from 'gabsocial/actions/instance'; import { fetchSoapboxConfig } from 'gabsocial/actions/soapbox'; const { localeData, messages } = getLocale(); @@ -26,8 +27,9 @@ export const store = configureStore(); const hydrateAction = hydrateStore(initialState); store.dispatch(hydrateAction); -store.dispatch(fetchCustomEmojis()); +store.dispatch(fetchInstance()); store.dispatch(fetchSoapboxConfig()); +store.dispatch(fetchCustomEmojis()); const mapStateToProps = (state) => { const account = state.getIn(['accounts', me]); diff --git a/app/gabsocial/reducers/index.js b/app/gabsocial/reducers/index.js index d42916c9e..c61bd578e 100644 --- a/app/gabsocial/reducers/index.js +++ b/app/gabsocial/reducers/index.js @@ -39,6 +39,7 @@ import group_editor from './group_editor'; import sidebar from './sidebar'; import patron from './patron'; import soapbox from './soapbox'; +import instance from './instance'; const reducers = { dropdown_menu, @@ -81,6 +82,7 @@ const reducers = { sidebar, patron, soapbox, + instance, }; export default combineReducers(reducers); diff --git a/app/gabsocial/reducers/instance.js b/app/gabsocial/reducers/instance.js new file mode 100644 index 000000000..8e91fc4e4 --- /dev/null +++ b/app/gabsocial/reducers/instance.js @@ -0,0 +1,13 @@ +import { INSTANCE_IMPORT } from '../actions/instance'; +import { Map as ImmutableMap, fromJS } from 'immutable'; + +const initialState = ImmutableMap(); + +export default function instance(state = initialState, action) { + switch(action.type) { + case INSTANCE_IMPORT: + return ImmutableMap(fromJS(action.instance)); + default: + return state; + } +};