vendredi 29 décembre 2017

What would be a good design pattern for passing in a type, going through some conditionals, and returning an object

const someFailedAction = (caseIds, growlMessages) => {

    if (caseIds.length > 1) {
        toastr.error(growlMessages[0], errorToastrOptions);
    } else if (isCaseDetailsDisplayed) {
        toastr.error(growlMessages[1], errorToastrOptions);
    } else if (errorParts.fieldIds.length === 0) {
        toastr.error(growlMessages[2], errorToastrOptions);
    } else {
        toastr.error(growlMessages[3], errorToastrOptions);
    }
}

I have a bunch of conditional statements like the one above which are executed on failures of different case actions. Most of the actions have the same if/elseIf structure, but some have an additional elseIf or a subtraction of one or more.

const SomeOtherFailedAction = (caseIds, growlMessages) => {

    if (caseIds.length > 1) {
        toastr.error(growlMessages[0], errorToastrOptions);
    } else if (isCaseDetailsDisplayed) {
        toastr.error(growlMessages[1], errorToastrOptions);
    } else {
        toastr.error(growlMessages[2], errorToastrOptions);
    }

}

I was wondering if there was a good design pattern for which I could just pass in the type and array of messages without having a long, nested, repetitious switch statement.

Aucun commentaire:

Enregistrer un commentaire