Allow deleted sketches in collections to be removed (fixes #1465)

This commit is contained in:
Andrew Nicolaou 2020-06-29 11:39:35 +02:00
parent e5554cbc60
commit 0e1bb3bd44
5 changed files with 55 additions and 29 deletions

View file

@ -71,41 +71,50 @@ ShareURL.propTypes = {
value: PropTypes.string.isRequired,
};
class CollectionItemRowBase extends React.Component {
handleSketchRemove = () => {
if (window.confirm(`Are you sure you want to remove "${this.props.item.project.name}" from this collection?`)) {
this.props.removeFromCollection(this.props.collection.id, this.props.item.project.id);
const CollectionItemRowBase = ({
collection, item, isOwner, removeFromCollection
}) => {
const projectIsDeleted = item.isDeleted;
const handleSketchRemove = () => {
const name = projectIsDeleted ? 'deleted sketch' : item.project.name;
if (window.confirm(`Are you sure you want to remove "${name}" from this collection?`)) {
removeFromCollection(collection.id, item.projectId);
}
}
};
render() {
const { item } = this.props;
const sketchOwnerUsername = item.project.user.username;
const sketchUrl = `/${item.project.user.username}/sketches/${item.project.id}`;
const name = projectIsDeleted ? <span>Sketch was deleted</span> : (
<Link to={`/${item.project.user.username}/sketches/${item.projectId}`}>
{item.project.name}
</Link>
);
return (
<tr
className="sketches-table__row"
>
<th scope="row">
<Link to={sketchUrl}>
{item.project.name}
</Link>
</th>
<td>{format(new Date(item.createdAt), 'MMM D, YYYY h:mm A')}</td>
<td>{sketchOwnerUsername}</td>
<td className="collection-row__action-column ">
const sketchOwnerUsername = projectIsDeleted ? null : item.project.user.username;
return (
<tr
className={`sketches-table__row ${projectIsDeleted ? 'is-deleted' : ''}`}
>
<th scope="row">
{name}
</th>
<td>{format(new Date(item.createdAt), 'MMM D, YYYY h:mm A')}</td>
<td>{sketchOwnerUsername}</td>
<td className="collection-row__action-column ">
{isOwner &&
<button
className="collection-row__remove-button"
onClick={this.handleSketchRemove}
onClick={handleSketchRemove}
aria-label="Remove sketch from collection"
>
<RemoveIcon focusable="false" aria-hidden="true" />
</button>
</td>
</tr>);
}
}
}
</td>
</tr>);
};
CollectionItemRowBase.propTypes = {
collection: PropTypes.shape({
@ -114,14 +123,17 @@ CollectionItemRowBase.propTypes = {
}).isRequired,
item: PropTypes.shape({
createdAt: PropTypes.string.isRequired,
projectId: PropTypes.string.isRequired,
isDeleted: PropTypes.bool.isRequired,
project: PropTypes.shape({
id: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
user: PropTypes.shape({
username: PropTypes.string.isRequired
})
}),
}).isRequired,
}).isRequired,
isOwner: PropTypes.bool.isRequired,
user: PropTypes.shape({
username: PropTypes.string,
authenticated: PropTypes.bool.isRequired
@ -342,6 +354,7 @@ class Collection extends React.Component {
render() {
const title = this.hasCollection() ? this.getCollectionName() : null;
const isOwner = this.isOwner();
return (
<main className="collection-container" data-has-items={this.hasCollectionItems() ? 'true' : 'false'}>
@ -372,6 +385,7 @@ class Collection extends React.Component {
user={this.props.user}
username={this.getUsername()}
collection={this.props.collection}
isOwner={isOwner}
/>))}
</tbody>
</table>

View file

@ -85,6 +85,10 @@
}
}
.sketches-table__row.is-deleted > * {
font-style: italic;
}
.sketches-table thead {
font-size: #{12 / $base-font-size}rem;
@include themify() {

View file

@ -32,7 +32,7 @@ export default function addProjectToCollection(req, res) {
return null;
}
const projectInCollection = collection.items.find(p => p.project._id === project._id);
const projectInCollection = collection.items.find(p => p.projectId === project._id);
if (projectInCollection) {
sendFailure(404, 'Project already in collection');

View file

@ -23,7 +23,7 @@ export default function addProjectToCollection(req, res) {
return null;
}
const project = collection.items.find(p => p.project._id === projectId);
const project = collection.items.find(p => p.projectId === projectId);
if (project != null) {
project.remove();

View file

@ -15,6 +15,14 @@ collectedProjectSchema.virtual('id').get(function getId() {
return this._id.toHexString();
});
collectedProjectSchema.virtual('projectId').get(function projectId() {
return this.populated('project');
});
collectedProjectSchema.virtual('isDeleted').get(function isDeleted() {
return this.project == null;
});
collectedProjectSchema.set('toJSON', {
virtuals: true
});