Skip to content

Commit

Permalink
fix(canned responses): default to alphabetical sorting (#1647)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajohn25 authored Aug 1, 2023
1 parent b7238f0 commit 7d53908
Showing 1 changed file with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,87 @@ class CampaignCannedResponsesForm extends React.Component<InnerProps, State> {
}
};

cannedResponseTitleSort = (
{ title: titleFirst }: CannedResponse,
{ title: titleSecond }: CannedResponse
) => {
if (titleFirst > titleSecond) {
return 1;
}
if (titleFirst < titleSecond) {
return -1;
}
return 0;
};

cannedResponseArraysEqual = (
cannedResponses1: CannedResponse[],
cannedResponses2: CannedResponse[]
) =>
cannedResponses1.length === cannedResponses2.length &&
cannedResponses1.every(
(cr, idx) => cr.title === cannedResponses2[idx].title
);

mapToTitleDisplayOrder = (cannedResponses: CannedResponse[]) => {
return cannedResponses.map((cannedResponse, idx) => {
return { ...cannedResponse, displayOrder: idx };
});
};

handleOnSaveResponse = (response: CannedResponse) => {
const newId = Math.random()
.toString(36)
.replace(/[^a-zA-Z1-9]+/g, "");

const { cannedResponses } = this.pendingCannedResponses();
const cannedResponsesToAdd = this.state.cannedResponsesToAdd.concat({
const cannedResponsesAlphabetical = cannedResponses.sort(
this.cannedResponseTitleSort
);

let newCannedResponse = {
...response,
id: newId,
displayOrder: cannedResponses.length + 1
});
};

if (
this.cannedResponseArraysEqual(
cannedResponses,
cannedResponsesAlphabetical
)
) {
const newCannedResponses = cannedResponses.concat({
...newCannedResponse
});
newCannedResponses.sort(this.cannedResponseTitleSort);
const newCannedResponsesWithOrder = this.mapToTitleDisplayOrder(
newCannedResponses
);

const newCannedResponseWithOrder = newCannedResponsesWithOrder.find(
({ id }) => id === newId
);
if (newCannedResponseWithOrder)
newCannedResponse = newCannedResponseWithOrder;

const updatedCannedResponses = newCannedResponsesWithOrder.filter(
({ id }) => id !== newId
);

this.setState({
editedCannedResponses: unionBy(
updatedCannedResponses,
this.state.editedCannedResponses,
"id"
) as CannedResponse[]
});
}

const cannedResponsesToAdd = this.state.cannedResponsesToAdd.concat(
newCannedResponse
);

this.setState({ cannedResponsesToAdd, shouldShowEditor: false });
};

Expand Down

0 comments on commit 7d53908

Please sign in to comment.