Skip to content

Commit

Permalink
adds deletion endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
jhmullen committed Oct 11, 2021
1 parent bdee77d commit c0230b8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
31 changes: 27 additions & 4 deletions packages/cms/src/api/cmsRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions packages/cms/src/components/cards/SelectorCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -272,7 +274,8 @@ class SelectorCard extends Component {
}

SelectorCard.defaultProps = {
type: "selector"
type: "selector",
parentType: PARENT_TYPES.PROFILE
};

SelectorCard.contextTypes = {
Expand Down
4 changes: 0 additions & 4 deletions packages/cms/src/components/cards/VariableCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,6 @@ class VariableCard extends Component {
}
}

VariableCard.defaultProps = {
parentType: "profile"
};

VariableCard.contextTypes = {
toast: PropTypes.object
};
Expand Down
7 changes: 3 additions & 4 deletions packages/cms/src/components/interface/Toolbox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ class Toolbox extends Component {
type={this.maybePrepend.bind(this)("generator")}
readOnly={i === 0}
compact={view === "lite"}
parentType={parentType}
usePortalForAlert
/>
)}
Expand All @@ -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
/>
)}
Expand All @@ -302,14 +300,15 @@ class Toolbox extends Component {
<Deck
title="Selectors"
entity={this.maybePrepend.bind(this)("selector")}
description="Profile-wide Selectors."
description={`${parentType === PARENT_TYPES.PROFILE ? "Profile" : "Story"}-wide Selectors.`}
addItem={this.addItem.bind(this, this.maybePrepend.bind(this)("selector"))}
cards={selectors.map(s =>
<SelectorCard
key={s.id}
minData={s}
compact={view === "lite"}
parentType={parentType}
type={this.maybePrepend.bind(this)("selector")}
usePortalForAlert
/>
)}
Expand Down Expand Up @@ -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
});

Expand Down
7 changes: 4 additions & 3 deletions packages/cms/src/db/storysection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = function(sequelize, db) {
type: db.INTEGER,
primaryKey: true,
autoIncrement: true
},
},
slug: {
type: db.STRING,
defaultValue: ""
Expand All @@ -24,7 +24,7 @@ module.exports = function(sequelize, db) {
defaultValue: "TextViz"
},
ordering: db.INTEGER
},
},
{
tableName: "canon_cms_storysection",
freezeTableName: true,
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions packages/cms/src/db/storysection_selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ module.exports = function(sequelize, db) {
primaryKey: true,
autoIncrement: true
},
story_section_id: {
section_id: {
type: db.INTEGER,
onDelete: "cascade",
references: {
model: "canon_cms_storysection",
key: "id"
}
},
selector_id: {
story_selector_id: {
type: db.INTEGER,
onDelete: "cascade",
references: {
Expand Down

0 comments on commit c0230b8

Please sign in to comment.