Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
JS / Vue JS - Redundant if statement for api calls

I have the following function in which the if statement feels a bit redundant as I'm repeating my calls for sendProfileData twice. Can someone guide me as to how I can make this cleaner, please?

 function editDetails(formData) {
    const initialImage = teacherProfile.value.profilePictureUrl.substring(teacherProfile.value.profilePictureUrl.indexOf("/teacher"))
      if (!formData.fileUploader) {
        sendProfileData(formData, initialImage).then(response => {
          if (response) {
            // After the successful API call, go back to the teachers profile
            router.push({name: ROUTE_NAMES_TEACHER_PROFILE.TEACHER_PROFILE});
          }
        })
      } else {
        uploadProfilePicture(formData.fileUploader).then(response => {
          if (response) {
            sendProfileData(formData, response.filename).then(response => {
              if (response) {
                // After the successful API call, go back to the teachers profile
                router.push({name: ROUTE_NAMES_TEACHER_PROFILE.TEACHER_PROFILE});
              }
            })
          }
        })
      }
    }

// Dispatch action
    function sendProfileData(data, imageUrl) {
      return store.dispatch(SET_PROFILE_DATA, {data, imageUrl});
    }

    function uploadProfilePicture(file) {
      return store.dispatch(UPLOAD_PROFILE_IMAGE, file);
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'd move it to its own function

function uploadProfile(file) {
  sendProfileData(formData, file).then(response => {
      if (response) {
          // After the successful API call, go back to the teachers profile
          router.push({name: ROUTE_NAMES_TEACHER_PROFILE.TEACHER_PROFILE});
      }
  })
}

then call it from two spots

if (!formData.fileUploader) {
  uploadProfile(initialImage)
...
uploadProfilePicture(formData.fileUploader).then(response => {
  if (response) {
    uploadProfile(response.filename)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda