2016-02-25 00:45:56 -08:00
|
|
|
import yaml from 'js-yaml';
|
2016-11-11 12:17:01 +01:00
|
|
|
import { set, defaultsDeep } from 'lodash';
|
2016-05-30 16:55:32 -07:00
|
|
|
import { currentBackend } from '../backends/backend';
|
|
|
|
import { authenticate } from '../actions/auth';
|
2016-06-10 00:16:01 -03:00
|
|
|
import * as MediaProxy from '../valueObjects/MediaProxy';
|
2016-11-11 12:17:01 +01:00
|
|
|
import * as publishModes from '../constants/publishModes';
|
2016-02-25 00:45:56 -08:00
|
|
|
|
2016-02-25 12:31:21 -08:00
|
|
|
export const CONFIG_REQUEST = 'CONFIG_REQUEST';
|
|
|
|
export const CONFIG_SUCCESS = 'CONFIG_SUCCESS';
|
|
|
|
export const CONFIG_FAILURE = 'CONFIG_FAILURE';
|
2016-02-25 00:45:56 -08:00
|
|
|
|
2016-11-11 12:17:01 +01:00
|
|
|
const defaults = {
|
|
|
|
publish_mode: publishModes.SIMPLE,
|
|
|
|
};
|
|
|
|
|
|
|
|
export function applyDefaults(config) {
|
|
|
|
if (!('media_folder' in config)) {
|
2016-11-29 16:16:56 -02:00
|
|
|
throw new Error('Error in configuration file: A `media_folder` wasn\'t found. Check your config.yml file.');
|
2016-11-11 12:17:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure there is a public folder
|
|
|
|
set(defaults,
|
|
|
|
'public_folder',
|
|
|
|
config.media_folder.charAt(0) === '/' ? config.media_folder : `/${ config.media_folder }`);
|
|
|
|
|
|
|
|
return defaultsDeep(config, defaults);
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseConfig(data) {
|
|
|
|
const config = yaml.safeLoad(data);
|
|
|
|
if (typeof CMS_ENV === 'string' && config[CMS_ENV]) {
|
|
|
|
// TODO: Add tests and refactor
|
|
|
|
for (const key in config[CMS_ENV]) { // eslint-disable-line no-restricted-syntax
|
|
|
|
if (config[CMS_ENV].hasOwnProperty(key)) { // eslint-disable-line no-prototype-builtins
|
|
|
|
config[key] = config[CMS_ENV][key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-25 00:45:56 -08:00
|
|
|
export function configLoaded(config) {
|
|
|
|
return {
|
2016-02-25 12:31:21 -08:00
|
|
|
type: CONFIG_SUCCESS,
|
2016-11-11 12:17:01 +01:00
|
|
|
payload: config,
|
2016-02-25 00:45:56 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function configLoading() {
|
|
|
|
return {
|
2016-11-11 12:17:01 +01:00
|
|
|
type: CONFIG_REQUEST,
|
2016-02-25 00:45:56 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function configFailed(err) {
|
|
|
|
return {
|
2016-02-25 12:31:21 -08:00
|
|
|
type: CONFIG_FAILURE,
|
2016-02-25 00:45:56 -08:00
|
|
|
error: 'Error loading config',
|
2016-11-11 12:17:01 +01:00
|
|
|
payload: err,
|
2016-02-25 00:45:56 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-06-06 21:53:22 -03:00
|
|
|
export function configDidLoad(config) {
|
|
|
|
return (dispatch) => {
|
2016-06-10 00:16:01 -03:00
|
|
|
MediaProxy.setConfig(config);
|
2016-06-06 21:53:22 -03:00
|
|
|
dispatch(configLoaded(config));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-11 12:17:01 +01:00
|
|
|
export function loadConfig() {
|
2016-02-25 00:45:56 -08:00
|
|
|
if (window.CMS_CONFIG) {
|
2016-06-06 21:53:22 -03:00
|
|
|
return configDidLoad(window.CMS_CONFIG);
|
2016-02-25 00:45:56 -08:00
|
|
|
}
|
2016-11-11 12:17:01 +01:00
|
|
|
return (dispatch) => {
|
2016-02-25 00:45:56 -08:00
|
|
|
dispatch(configLoading());
|
|
|
|
|
2016-07-20 15:51:49 -03:00
|
|
|
fetch('config.yml').then((response) => {
|
2016-02-25 00:45:56 -08:00
|
|
|
if (response.status !== 200) {
|
2016-11-11 12:17:01 +01:00
|
|
|
throw new Error(`Failed to load config.yml (${ response.status })`);
|
2016-02-25 00:45:56 -08:00
|
|
|
}
|
|
|
|
|
2016-11-11 12:17:01 +01:00
|
|
|
response
|
|
|
|
.text()
|
|
|
|
.then(parseConfig)
|
|
|
|
.then(applyDefaults)
|
|
|
|
.then((config) => {
|
|
|
|
dispatch(configDidLoad(config));
|
|
|
|
const backend = currentBackend(config);
|
|
|
|
const user = backend && backend.currentUser();
|
|
|
|
if (user) dispatch(authenticate(user));
|
|
|
|
});
|
2016-02-25 00:45:56 -08:00
|
|
|
}).catch((err) => {
|
|
|
|
dispatch(configFailed(err));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|