Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions client/modules/IDE/actions/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ export function getProjects(username) {
dispatch(stopLoader());
})
.catch((error) => {
const { response } = error;
dispatch({
type: ActionTypes.ERROR,
error: response.data
error: error?.response?.data
});
dispatch(stopLoader());
});
Expand Down
194 changes: 60 additions & 134 deletions client/modules/IDE/components/AddToCollectionSketchList.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import PropTypes from 'prop-types';
import React from 'react';
import React, { useEffect, useState } from 'react';
import { Helmet } from 'react-helmet';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { withTranslation } from 'react-i18next';
// import find from 'lodash/find';
import * as ProjectsActions from '../actions/projects';
import * as CollectionsActions from '../actions/collections';
import * as ToastActions from '../actions/toast';
import * as SortingActions from '../actions/sorting';
import { useDispatch, useSelector } from 'react-redux';
import { useTranslation } from 'react-i18next';
import { addToCollection, removeFromCollection } from '../actions/collections';
import { getProjects } from '../actions/projects';
import getSortedSketches from '../selectors/projects';
import Loader from '../../App/components/loader';
import QuickAddList from './QuickAddList';
Expand All @@ -17,149 +13,79 @@ import {
QuickAddWrapper
} from './AddToCollectionList';

class SketchList extends React.Component {
constructor(props) {
super(props);
this.props.getProjects(this.props.username);
const AddToCollectionSketchList = ({ collection }) => {
const { t } = useTranslation();

this.state = {
isInitialDataLoad: true
};
}
const dispatch = useDispatch();

componentWillReceiveProps(nextProps) {
if (
this.props.sketches !== nextProps.sketches &&
Array.isArray(nextProps.sketches)
) {
this.setState({
isInitialDataLoad: false
});
}
}
const username = useSelector((state) => state.user.username);

getSketchesTitle() {
if (this.props.username === this.props.user.username) {
return this.props.t('AddToCollectionSketchList.Title');
}
return this.props.t('AddToCollectionSketchList.AnothersTitle', {
anotheruser: this.props.username
});
}
const sketches = useSelector(getSortedSketches);

handleCollectionAdd = (sketch) => {
this.props.addToCollection(this.props.collection.id, sketch.id);
};

handleCollectionRemove = (sketch) => {
this.props.removeFromCollection(this.props.collection.id, sketch.id);
};
// TODO: improve loading state
const loading = useSelector((state) => state.loading);
const [hasLoadedData, setHasLoadedData] = useState(false);
const showLoader = loading && !hasLoadedData;

inCollection = (sketch) =>
this.props.collection.items.find((item) =>
item.isDeleted ? false : item.project.id === sketch.id
) != null;
useEffect(() => {
dispatch(getProjects(username)).then(() => setHasLoadedData(true));
}, [dispatch, username]);

render() {
const hasSketches = this.props.sketches.length > 0;
const sketchesWithAddedStatus = this.props.sketches.map((sketch) => ({
...sketch,
isAdded: this.inCollection(sketch),
url: `/${this.props.username}/sketches/${sketch.id}`
}));
const handleCollectionAdd = (sketch) => {
dispatch(addToCollection(collection.id, sketch.id));
};

let content = null;
const handleCollectionRemove = (sketch) => {
dispatch(removeFromCollection(collection.id, sketch.id));
};

if (this.props.loading && this.state.isInitialDataLoad) {
content = <Loader />;
} else if (hasSketches) {
content = (
<QuickAddList
items={sketchesWithAddedStatus}
onAdd={this.handleCollectionAdd}
onRemove={this.handleCollectionRemove}
/>
);
} else {
content = this.props.t('AddToCollectionSketchList.NoCollections');
const sketchesWithAddedStatus = sketches.map((sketch) => ({
...sketch,
url: `/${username}/sketches/${sketch.id}`,
isAdded: collection.items.some(
(item) => item.projectId === sketch.id && !item.isDeleted
)
}));

const getContent = () => {
if (showLoader) {
return <Loader />;
} else if (sketches.length === 0) {
// TODO: shouldn't it be NoSketches? -Linda
return t('AddToCollectionSketchList.NoCollections');
}

return (
<CollectionAddSketchWrapper>
<QuickAddWrapper>
<Helmet>
<title>{this.getSketchesTitle()}</title>
</Helmet>
{content}
</QuickAddWrapper>
</CollectionAddSketchWrapper>
<QuickAddList
items={sketchesWithAddedStatus}
onAdd={handleCollectionAdd}
onRemove={handleCollectionRemove}
/>
);
}
}
};

SketchList.propTypes = {
user: PropTypes.shape({
username: PropTypes.string,
authenticated: PropTypes.bool.isRequired
}).isRequired,
getProjects: PropTypes.func.isRequired,
sketches: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
createdAt: PropTypes.string.isRequired,
updatedAt: PropTypes.string.isRequired
})
).isRequired,
return (
<CollectionAddSketchWrapper>
<QuickAddWrapper>
<Helmet>
<title>{t('AddToCollectionSketchList.Title')}</title>
</Helmet>
{getContent()}
</QuickAddWrapper>
</CollectionAddSketchWrapper>
);
};

AddToCollectionSketchList.propTypes = {
collection: PropTypes.shape({
id: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
items: PropTypes.arrayOf(
PropTypes.shape({
project: PropTypes.shape({
id: PropTypes.string.isRequired
})
projectId: PropTypes.string.isRequired,
isDeleted: PropTypes.bool
})
)
}).isRequired,
username: PropTypes.string,
loading: PropTypes.bool.isRequired,
sorting: PropTypes.shape({
field: PropTypes.string.isRequired,
direction: PropTypes.string.isRequired
}).isRequired,
addToCollection: PropTypes.func.isRequired,
removeFromCollection: PropTypes.func.isRequired,
t: PropTypes.func.isRequired
};

SketchList.defaultProps = {
username: undefined
}).isRequired
};

function mapStateToProps(state) {
return {
user: state.user,
sketches: getSortedSketches(state),
sorting: state.sorting,
loading: state.loading,
project: state.project
};
}

function mapDispatchToProps(dispatch) {
return bindActionCreators(
Object.assign(
{},
ProjectsActions,
CollectionsActions,
ToastActions,
SortingActions
),
dispatch
);
}

export default withTranslation()(
connect(mapStateToProps, mapDispatchToProps)(SketchList)
);
export default AddToCollectionSketchList;
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ class CollectionList extends React.Component {
isFixedHeight
>
<AddToCollectionSketchList
username={this.props.username}
collection={find(this.props.collections, {
id: this.state.addingSketchesToCollectionId
})}
Expand Down
1 change: 0 additions & 1 deletion client/modules/User/components/Collection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,6 @@ class Collection extends React.Component {
isFixedHeight
>
<AddToCollectionSketchList
username={this.props.username}
collection={this.props.collection}
/>
</Overlay>
Expand Down