Preload: dispatch preload action

This commit is contained in:
Alex Gleason 2020-08-24 15:23:05 -05:00
parent 31d007eeb1
commit 57340496bc
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 27 additions and 4 deletions

View file

@ -0,0 +1,25 @@
import { mapValues } from 'lodash';
const PRELOAD_IMPORT = 'PRELOAD_IMPORT';
// https://git.pleroma.social/pleroma/pleroma-fe/-/merge_requests/1176/diffs
const decodeUTF8Base64 = (data) => {
const rawData = atob(data);
const array = Uint8Array.from(rawData.split('').map((char) => char.charCodeAt(0)));
const text = new TextDecoder().decode(array);
return text;
};
const decodeData = data =>
mapValues(data, base64string =>
JSON.parse(decodeUTF8Base64(base64string)));
export function preload() {
const element = document.getElementById('initial-results');
const data = element ? JSON.parse(element.textContent) : {};
return {
type: PRELOAD_IMPORT,
data: decodeData(data),
};
}

View file

@ -14,9 +14,8 @@ import { ScrollContext } from 'react-router-scroll-4';
import UI from '../features/ui';
// import Introduction from '../features/introduction';
import { fetchCustomEmojis } from '../actions/custom_emojis';
import { hydrateStore } from '../actions/store';
import { preload } from '../actions/preload';
import { IntlProvider } from 'react-intl';
import initialState from '../initial_state';
import ErrorBoundary from '../components/error_boundary';
import { fetchInstance } from 'soapbox/actions/instance';
import { fetchSoapboxConfig } from 'soapbox/actions/soapbox';
@ -30,9 +29,8 @@ import messages from 'soapbox/locales/messages';
const validLocale = locale => Object.keys(messages).includes(locale);
export const store = configureStore();
const hydrateAction = hydrateStore(initialState);
store.dispatch(hydrateAction);
store.dispatch(preload());
store.dispatch(fetchMe());
store.dispatch(fetchInstance());
store.dispatch(fetchSoapboxConfig());