parent
2c84316fe0
commit
934dcdf39b
@ -0,0 +1,61 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Menu } from 'semantic-ui-react';
|
||||
import { Popup } from '../../lib/custom-ui';
|
||||
import { ListSortTypes } from '../../constants/Enums';
|
||||
|
||||
import styles from './ListSortStep.module.scss';
|
||||
|
||||
const ListSortStep = React.memo(({ onTypeSelect, onBack }) => {
|
||||
const [t] = useTranslation();
|
||||
|
||||
return (
|
||||
<>
|
||||
<Popup.Header onBack={onBack}>
|
||||
{t('common.sortList', {
|
||||
context: 'title',
|
||||
})}
|
||||
</Popup.Header>
|
||||
<Popup.Content>
|
||||
<Menu secondary vertical className={styles.menu}>
|
||||
<Menu.Item
|
||||
className={styles.menuItem}
|
||||
onClick={() => onTypeSelect(ListSortTypes.NAME_ASC)}
|
||||
>
|
||||
{t('common.name')}
|
||||
</Menu.Item>
|
||||
<Menu.Item
|
||||
className={styles.menuItem}
|
||||
onClick={() => onTypeSelect(ListSortTypes.DUE_DATE_ASC)}
|
||||
>
|
||||
{t('common.dueDate')}
|
||||
</Menu.Item>
|
||||
<Menu.Item
|
||||
className={styles.menuItem}
|
||||
onClick={() => onTypeSelect(ListSortTypes.CREATED_AT_ASC)}
|
||||
>
|
||||
{t('common.oldestFirst')}
|
||||
</Menu.Item>
|
||||
<Menu.Item
|
||||
className={styles.menuItem}
|
||||
onClick={() => onTypeSelect(ListSortTypes.CREATED_AT_DESC)}
|
||||
>
|
||||
{t('common.newestFirst')}
|
||||
</Menu.Item>
|
||||
</Menu>
|
||||
</Popup.Content>
|
||||
</>
|
||||
);
|
||||
});
|
||||
|
||||
ListSortStep.propTypes = {
|
||||
onTypeSelect: PropTypes.func.isRequired,
|
||||
onBack: PropTypes.func,
|
||||
};
|
||||
|
||||
ListSortStep.defaultProps = {
|
||||
onBack: undefined,
|
||||
};
|
||||
|
||||
export default ListSortStep;
|
||||
@ -0,0 +1,11 @@
|
||||
:global(#app) {
|
||||
.menu {
|
||||
margin: -7px -12px -5px;
|
||||
width: calc(100% + 24px);
|
||||
}
|
||||
|
||||
.menuItem {
|
||||
margin: 0;
|
||||
padding-left: 14px;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
import ListSortStep from './ListSortStep';
|
||||
|
||||
export default ListSortStep;
|
||||
@ -0,0 +1,65 @@
|
||||
const Errors = {
|
||||
NOT_ENOUGH_RIGHTS: {
|
||||
notEnoughRights: 'Not enough rights',
|
||||
},
|
||||
LIST_NOT_FOUND: {
|
||||
listNotFound: 'List not found',
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
id: {
|
||||
type: 'string',
|
||||
regex: /^[0-9]+$/,
|
||||
required: true,
|
||||
},
|
||||
type: {
|
||||
type: 'string',
|
||||
isIn: Object.values(List.SortTypes),
|
||||
},
|
||||
},
|
||||
|
||||
exits: {
|
||||
notEnoughRights: {
|
||||
responseType: 'forbidden',
|
||||
},
|
||||
listNotFound: {
|
||||
responseType: 'notFound',
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs) {
|
||||
const { currentUser } = this.req;
|
||||
|
||||
const { list } = await sails.helpers.lists
|
||||
.getProjectPath(inputs.id)
|
||||
.intercept('pathNotFound', () => Errors.LIST_NOT_FOUND);
|
||||
|
||||
const boardMembership = await BoardMembership.findOne({
|
||||
boardId: list.boardId,
|
||||
userId: currentUser.id,
|
||||
});
|
||||
|
||||
if (!boardMembership) {
|
||||
throw Errors.LIST_NOT_FOUND;
|
||||
}
|
||||
|
||||
if (boardMembership.role !== BoardMembership.Roles.EDITOR) {
|
||||
throw Errors.NOT_ENOUGH_RIGHTS;
|
||||
}
|
||||
|
||||
const cards = await sails.helpers.lists.sortOne.with({
|
||||
record: list,
|
||||
type: inputs.type,
|
||||
request: this.req,
|
||||
});
|
||||
|
||||
return {
|
||||
item: list,
|
||||
included: {
|
||||
cards,
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
@ -0,0 +1,70 @@
|
||||
const List = require('../../models/List');
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
record: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
type: {
|
||||
type: 'string',
|
||||
isIn: Object.values(List.SortTypes),
|
||||
defaultsTo: List.SortTypes.NAME_ASC,
|
||||
},
|
||||
request: {
|
||||
type: 'ref',
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs) {
|
||||
let cards = await sails.helpers.lists.getCards(inputs.record.id);
|
||||
|
||||
switch (inputs.type) {
|
||||
case List.SortTypes.NAME_ASC:
|
||||
cards.sort((a, b) => a.name.localeCompare(b.name));
|
||||
break;
|
||||
case List.SortTypes.DUE_DATE_ASC:
|
||||
cards.sort((a, b) => {
|
||||
if (a.dueDate === null) return 1;
|
||||
if (b.dueDate === null) return -1;
|
||||
return new Date(a.dueDate) - new Date(b.dueDate);
|
||||
});
|
||||
break;
|
||||
case List.SortTypes.CREATED_AT_ASC:
|
||||
cards.sort((a, b) => new Date(a.createdAt) - new Date(b.createdAt));
|
||||
break;
|
||||
case List.SortTypes.CREATED_AT_DESC:
|
||||
cards.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
|
||||
break;
|
||||
default:
|
||||
throw new Error('Invalid sort type specified');
|
||||
}
|
||||
|
||||
const positions = cards.map((c) => c.position).sort((a, b) => a - b);
|
||||
|
||||
cards = await Promise.all(
|
||||
cards.map(({ id }, index) =>
|
||||
Card.updateOne({
|
||||
id,
|
||||
listId: inputs.record.id,
|
||||
}).set({
|
||||
position: positions[index],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
sails.sockets.broadcast(
|
||||
`board:${inputs.record.boardId}`,
|
||||
'listSort',
|
||||
{
|
||||
item: inputs.record,
|
||||
included: {
|
||||
cards,
|
||||
},
|
||||
},
|
||||
inputs.request,
|
||||
);
|
||||
|
||||
return cards;
|
||||
},
|
||||
};
|
||||
Loading…
Reference in New Issue