diff --git a/packages/cms/src/api/cmsRoute.js b/packages/cms/src/api/cmsRoute.js index 8f2022178..641ec94ea 100644 --- a/packages/cms/src/api/cmsRoute.js +++ b/packages/cms/src/api/cmsRoute.js @@ -55,15 +55,15 @@ const flatSort = (conn, array) => { return array; }; -const bubbleSortSelectors = (conn, selectors) => { +const bubbleSortSelectors = (conn, selectors, accessor = "section_selector") => { selectors = selectors - .map(s => Object.assign({}, s, {ordering: s.section_selector.ordering})) + .map(s => Object.assign({}, s, {ordering: s[accessor].ordering})) .sort(sorter); selectors.forEach((o, i) => { if (o.ordering !== i) { o.ordering = i; - o.section_selector.ordering = i; - conn.update({ordering: i}, {where: {id: o.section_selector.id}}).catch(catcher); + o[accessor].ordering = i; + conn.update({ordering: i}, {where: {id: o[accessor].id}}).catch(catcher); } }); return selectors; @@ -776,6 +776,13 @@ module.exports = function(app) { return res.json({id: row.id, parent_id: row.profile_id, selectors}); }); + app.delete("/api/cms/story_selector/delete", isEnabled, async(req, res) => { + const row = await db.story_selector.findOne({where: {id: req.query.id}}).catch(catcher); + await db.story_selector.destroy({where: {id: req.query.id}}); + const selectors = await db.story_selector.findAll({where: {story_id: row.story_id}}).catch(catcher); + return res.json({id: row.id, parent_id: row.story_id, selectors}); + }); + app.delete("/api/cms/section_selector/delete", isEnabled, async(req, res) => { const {selector_id, section_id} = req.query; // eslint-disable-line camelcase const row = await db.section_selector.findOne({where: {selector_id, section_id}}).catch(catcher); @@ -792,6 +799,22 @@ module.exports = function(app) { return res.json({parent_id: row.section_id, selectors: rows}); }); + app.delete("/api/cms/storysection_selector/delete", isEnabled, async(req, res) => { + const {story_selector_id, storysection_id} = req.query; // eslint-disable-line camelcase + const row = await db.storysection_selector.findOne({where: {story_selector_id, storysection_id}}).catch(catcher); + await db.storysection_selector.update({ordering: sequelize.literal("ordering -1")}, {where: {storysection_id, ordering: {[Op.gt]: row.ordering}}}).catch(catcher); + await db.storysection_selector.destroy({where: {story_selector_id, storysection_id}}); + const reqObj = {where: {id: row.storysection_id}, include: [{association: "selectors"}]}; + let storysection = await db.storysection.findOne(reqObj).catch(catcher); + let rows = []; + if (storysection) { + storysection = storysection.toJSON(); + storysection.selectors = bubbleSortSelectors(db.storysection_selector, storysection.selectors, "storysection_selector"); + rows = storysection.selectors; + } + return res.json({parent_id: row.storysection_id, selectors: rows}); + }); + app.delete("/api/cms/profile/delete", isEnabled, async(req, res) => { const row = await db.profile.findOne({where: {id: req.query.id}}).catch(catcher); await db.profile.update({ordering: sequelize.literal("ordering -1")}, {where: {ordering: {[Op.gt]: row.ordering}}}).catch(catcher); diff --git a/packages/cms/src/components/cards/SelectorCard.jsx b/packages/cms/src/components/cards/SelectorCard.jsx index 5ccbe8d62..2269f55ff 100644 --- a/packages/cms/src/components/cards/SelectorCard.jsx +++ b/packages/cms/src/components/cards/SelectorCard.jsx @@ -17,6 +17,7 @@ import {deleteEntity, duplicateEntity, updateEntity} from "../../actions/profile import {setStatus} from "../../actions/status"; import "./SelectorCard.css"; +import {PARENT_TYPES} from "../../utils/consts/cms"; /** * Card Component for displaying dropdown selectors. Selectors may be singular dropdowns @@ -160,7 +161,8 @@ class SelectorCard extends Component { // define initial card props const cardProps = { type: "selector", - title: "•••" + title: "•••", + allowed: true }; // fill in real card props @@ -272,7 +274,8 @@ class SelectorCard extends Component { } SelectorCard.defaultProps = { - type: "selector" + type: "selector", + parentType: PARENT_TYPES.PROFILE }; SelectorCard.contextTypes = { diff --git a/packages/cms/src/components/cards/VariableCard.jsx b/packages/cms/src/components/cards/VariableCard.jsx index 56fbdfb17..24a9f2a6f 100644 --- a/packages/cms/src/components/cards/VariableCard.jsx +++ b/packages/cms/src/components/cards/VariableCard.jsx @@ -349,10 +349,6 @@ class VariableCard extends Component { } } -VariableCard.defaultProps = { - parentType: "profile" -}; - VariableCard.contextTypes = { toast: PropTypes.object }; diff --git a/packages/cms/src/components/interface/Toolbox.jsx b/packages/cms/src/components/interface/Toolbox.jsx index bfbecc022..770041ee8 100644 --- a/packages/cms/src/components/interface/Toolbox.jsx +++ b/packages/cms/src/components/interface/Toolbox.jsx @@ -269,7 +269,6 @@ class Toolbox extends Component { type={this.maybePrepend.bind(this)("generator")} readOnly={i === 0} compact={view === "lite"} - parentType={parentType} usePortalForAlert /> )} @@ -291,7 +290,6 @@ class Toolbox extends Component { type={this.maybePrepend.bind(this)("materializer")} showReorderButton={materializers[materializers.length - 1].id !== m.id} compact={view === "lite"} - parentType={parentType} usePortalForAlert /> )} @@ -302,7 +300,7 @@ class Toolbox extends Component { )} @@ -347,7 +346,7 @@ Toolbox.defaultProps = { const mapStateToProps = (state, ownProps) => ({ variables: state.cms.variables, status: state.cms.status, - profile: state.cms[ownProps.parentType === "profile" ? "profiles" : "stories"].find(p => p.id === ownProps.id), + profile: state.cms[ownProps.parentType === PARENT_TYPES.PROFILE ? "profiles" : "stories"].find(p => p.id === ownProps.id), formatters: state.cms.formatters }); diff --git a/packages/cms/src/db/storysection.js b/packages/cms/src/db/storysection.js index 58255cbd8..acfbc4618 100644 --- a/packages/cms/src/db/storysection.js +++ b/packages/cms/src/db/storysection.js @@ -6,7 +6,7 @@ module.exports = function(sequelize, db) { type: db.INTEGER, primaryKey: true, autoIncrement: true - }, + }, slug: { type: db.STRING, defaultValue: "" @@ -24,7 +24,7 @@ module.exports = function(sequelize, db) { defaultValue: "TextViz" }, ordering: db.INTEGER - }, + }, { tableName: "canon_cms_storysection", freezeTableName: true, @@ -38,7 +38,8 @@ module.exports = function(sequelize, db) { s.hasMany(models.storysection_stat, {foreignKey: "storysection_id", sourceKey: "id", as: "stats"}); s.hasMany(models.storysection_subtitle, {foreignKey: "storysection_id", sourceKey: "id", as: "subtitles"}); s.hasMany(models.storysection_visualization, {foreignKey: "storysection_id", sourceKey: "id", as: "visualizations"}); - }; + s.belongsToMany(models.story_selector, {through: "storysection_selector", foreignKey: "storysection_id", otherKey: "story_selector_id", as: "selectors"}); + }; return s; diff --git a/packages/cms/src/db/storysection_selector.js b/packages/cms/src/db/storysection_selector.js index 2b7102734..8b45dafb0 100644 --- a/packages/cms/src/db/storysection_selector.js +++ b/packages/cms/src/db/storysection_selector.js @@ -7,7 +7,7 @@ module.exports = function(sequelize, db) { primaryKey: true, autoIncrement: true }, - story_section_id: { + section_id: { type: db.INTEGER, onDelete: "cascade", references: { @@ -15,7 +15,7 @@ module.exports = function(sequelize, db) { key: "id" } }, - selector_id: { + story_selector_id: { type: db.INTEGER, onDelete: "cascade", references: {