N
N
nivaech2020-09-08 12:04:25
JavaScript
nivaech, 2020-09-08 12:04:25

How to replace if steam locomotive?

There are four query functions that are passed into one component and are responsible for deleting a specific type of question.

const handleBuyerQuestionDelete = questionId => {
    dispatch(OrganizationsActions.deleteEventCustomQuestionRequest(organizationId, eventId, questionId));
  };

  const handleAllAttendeesQuestionDelete = questionId => {
    dispatch(EventsActions.deleteAllAttendeesCustomQuestionRequest(organizationId, eventId, questionId));
  };

  const handleTicketQuestionDelete = (ticketId, questionId) => {
    dispatch(EventsActions.deleteTicketCustomQuestionRequest(organizationId, eventId, ticketId, questionId));
  };

  const handleAddonQuestionDelete = (addonId, questionId) => {
    dispatch(EventsActions.deleteAddonCustomQuestionRequest(organizationId, eventId, addonId, questionId));
  };

<QuestionList
                    label={t('screens.createEventQuestions.misc.questionList')}
                    questions={questions}
                    onAddQuestions={handleAddEventQuestions}
                    onQuestionRequiredChange={onEventQuestionRequiredChange}
                    onQuestionDelete={handleBuyerQuestionDelete}
                  />


They receive common data, but the last two requests receive additional parameters - addonId and ticketId , which are already passed to the functions in the children component.
In the final component, there is a condition for the execution of one of the four functions, depending on the data that comes to the component. And it would be nice to write the condition elegantly, because for now it looks like this:

const handleDeleteQuestion = () => {
    if (addonId) {
      onQuestionDelete(addonId, id);
    } else if (ticketId) {
      onQuestionDelete(ticketId, id);
    } else {
      onQuestionDelete(id);
    }
  };

        <IconButton className={classes.deleteButton} color="secondary" onClick={handleDeleteQuestion}>
          <DeleteIcon fontSize="small" />
        </IconButton>


This condition works, but looks bad. How to rewrite the condition so that it both works and looks elegant at the same time?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2020-09-08
@nivaech

onQuestionDelete(addinId || ticketId || id, addinId || ticketId ? id : undefined)

but I would change the onQuestionDelete signature to be
onQuestionDelete(id, addinId || ticketId)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question