Skip to content

Commit

Permalink
Merge pull request #751 from the-deep/feature-bulk-delete
Browse files Browse the repository at this point in the history
remove lead from leadAdd in bulk (in redux)
  • Loading branch information
pprabesh authored Apr 8, 2019
2 parents daf2e62 + 39c0928 commit 6632007
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 16 deletions.
34 changes: 24 additions & 10 deletions src/redux/reducers/siloDomainData/addLeads.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
listToMap,
getElementAround,
getNumbers,
isDefined,
getDefinedElementAround,
} from '@togglecorp/fujs';
import { analyzeErrors } from '@togglecorp/faram';

Expand Down Expand Up @@ -127,9 +128,9 @@ export const addLeadViewLeadSaveAction = ({ leadId, serverId }) => ({
serverId,
});

export const addLeadViewLeadRemoveAction = leadId => ({
export const addLeadViewLeadRemoveAction = leadIds => ({
type: LA__LEAD_REMOVE,
leadId,
leadIds,
});

export const addLeadViewRemoveSavedLeadsAction = () => ({
Expand Down Expand Up @@ -267,20 +268,33 @@ const addLeadViewNextLead = (state) => {
};

const addLeadViewRemoveLead = (state, action) => {
const { addLeadView: { leads } } = state;
const { addLeadView: { leads, activeLeadId } } = state;

const { leadId } = action;
const leadIndex = leads.findIndex(
lead => leadAccessor.getKey(lead) === leadId,
const { leadIds } = action;

const filteredLeads = leads.map(
lead => (leadIds.includes(leadAccessor.getKey(lead)) ? undefined : lead),
);
const finalFilteredLeads = filteredLeads.filter(isDefined);

let newActiveLeadId;
if (finalFilteredLeads.includes(activeLeadId)) {
newActiveLeadId = activeLeadId;
} else {
const leadIndex = leads.findIndex(lead => leadAccessor.getKey(lead) === activeLeadId);
const newActiveLead = getDefinedElementAround(filteredLeads, leadIndex);
if (newActiveLead) {
newActiveLeadId = leadAccessor.getKey(newActiveLead);
}
}

// limiting the newActiveid
const newActiveLead = getElementAround(leads, leadIndex);
const newActiveLeadId = newActiveLead ? leadAccessor.getKey(newActiveLead) : undefined;
// const newActiveLead = getElementAround(leads, leadIndex);
// const newActiveLeadId = newActiveLead ? leadAccessor.getKey(newActiveLead) : undefined;

const settings = {
addLeadView: {
leads: { $splice: [[leadIndex, 1]] },
leads: { $set: finalFilteredLeads },
activeLeadId: { $set: newActiveLeadId },
},
};
Expand Down
86 changes: 84 additions & 2 deletions src/redux/reducers/siloDomainData/addLeads.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ test('should remove lead from top', () => {
],
},
};
const action = addLeadViewLeadRemoveAction(1);
const action = addLeadViewLeadRemoveAction([1]);
const after = {
addLeadView: {
activeLeadId: 2,
Expand All @@ -182,6 +182,88 @@ test('should remove lead from top', () => {
expect(reducers[LA__LEAD_REMOVE](state, action)).toEqual(after);
});

test('should remove lead from middle', () => {
const state = {
addLeadView: {
activeLeadId: 2,
leads: [
{ id: 1 },
{ id: 2 },
{ id: 3 },
{ id: 4 },
{ id: 5 },
],
},
};
const action = addLeadViewLeadRemoveAction([2, 3]);
const after = {
addLeadView: {
activeLeadId: 1,
leads: [
{ id: 1 },
{ id: 4 },
{ id: 5 },
],
},
};
expect(reducers[LA__LEAD_REMOVE](state, action)).toEqual(after);
});

test('should remove lead from middle', () => {
const state = {
addLeadView: {
activeLeadId: 3,
leads: [
{ id: 1 },
{ id: 2 },
{ id: 3 },
{ id: 4 },
{ id: 5 },
],
},
};
const action = addLeadViewLeadRemoveAction([2, 3]);
const after = {
addLeadView: {
activeLeadId: 4,
leads: [
{ id: 1 },
{ id: 4 },
{ id: 5 },
],
},
};
expect(reducers[LA__LEAD_REMOVE](state, action)).toEqual(after);
});

test('should remove lead from middle', () => {
const state = {
addLeadView: {
activeLeadId: 2,
leads: [
{ id: 1 },
{ id: 2 },
{ id: 3 },
{ id: 4 },
{ id: 5 },
],
},
};
const action = addLeadViewLeadRemoveAction([2]);
const after = {
addLeadView: {
activeLeadId: 3,
leads: [
{ id: 1 },
{ id: 3 },
{ id: 4 },
{ id: 5 },
],
},
};
expect(reducers[LA__LEAD_REMOVE](state, action)).toEqual(after);
});

test('should remove lead from bottom', () => {
const state = {
addLeadView: {
Expand All @@ -192,7 +274,7 @@ test('should remove lead from bottom', () => {
],
},
};
const action = addLeadViewLeadRemoveAction(2);
const action = addLeadViewLeadRemoveAction([2]);
const after = {
addLeadView: {
activeLeadId: 1,
Expand Down
8 changes: 4 additions & 4 deletions src/views/LeadAdd/LeadActions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export default class LeadFilter extends React.PureComponent {

removeSelected = (leadId) => {
this.props.uploadCoordinator.remove(leadId);
this.props.addLeadViewLeadRemove(leadId);
this.props.addLeadViewLeadRemove([leadId]);

notify.send({
title: _ts('addLeads.actions', 'leadDiscard'),
Expand All @@ -160,8 +160,8 @@ export default class LeadFilter extends React.PureComponent {
removeFiltered = () => {
this.props.filteredLeadKeys.forEach((leadId) => {
this.props.uploadCoordinator.remove(leadId);
this.props.addLeadViewLeadRemove(leadId);
});
this.props.addLeadViewLeadRemove(this.props.filteredLeadKeys);

notify.send({
title: _ts('addLeads.actions', 'leadsDiscard'),
Expand All @@ -174,8 +174,8 @@ export default class LeadFilter extends React.PureComponent {
removeCompleted = () => {
this.props.completedLeadKeys.forEach((leadId) => {
this.props.uploadCoordinator.remove(leadId);
this.props.addLeadViewLeadRemove(leadId);
});
this.props.addLeadViewLeadRemove(this.props.uploadCoordinator);

notify.send({
title: _ts('addLeads.actions', 'leadsDiscard'),
Expand All @@ -188,8 +188,8 @@ export default class LeadFilter extends React.PureComponent {
removeBulk = () => {
this.props.leadKeys.forEach((leadId) => {
this.props.uploadCoordinator.remove(leadId);
this.props.addLeadViewLeadRemove(leadId);
});
this.props.addLeadViewLeadRemove(this.props.leadKeys);

notify.send({
title: _ts('addLeads.actions', 'leadsDiscard'),
Expand Down

0 comments on commit 6632007

Please sign in to comment.