Challenges in integrating a universal confirmation dialog functionality in Vue.js

I am currently working on implementing a feature for a global confirm dialog.

For example: When a user clicks a "publish" button to publish an article, the confirm dialog will open.

  1. Clicking the publish button triggers the function "openConfirmDialog()".
  2. The confirm dialog is then displayed.
  3. Waiting for the user to click the "confirm" button.
  4. When the confirm button is clicked, the function "onConfirm()" in "confirmDialog.vue" will be triggered.

Questions:
How can I pass and trigger a dynamic action (for example: publishArticle) when the user clicks the "confirm" button?


Component - confirmDialog.vue Cancel button:

  protected onCancel() {
    this.$store.actions.closeConfirmDialog()
  }

Confirm button:

  protected onConfirm() {
    this.$store.actions.proceedConfirmDialog()
  }

confirm dialog vuex module store actions:

  async openConfirmDialog(
    context: ActionContext<ConfirmDialogState, any>,
    payload: SetConfirmDialogPayloadParams
  ) {
    context.commit(types.CONFIRM_DIALOG_SET_CONTENT, payload.content)
  }

Confirm button action:

async proceedConfirmDialog(context: ActionContext<ConfirmDialogState, any>) {}
 

frontend views - demo.vue:

  public openConfirmDialog() {
    this.$store.myActions.openConfirmDialog({
      content: {
        message: 'are you sure?',
      }
    })
  }

publish article vuex module store action:

async pubishArticle(){....}

Answer №1

To implement the button functionality within openConfirmDialog, you can define the buttons inside the function like so:

public openConfirmDialog() {
  this.$store.myActions.openConfirmDialog({
    content: { message: 'Are you sure?' },
    buttons: [
      { text: 'Confirm', onClick: this.onConfirmButtonClicked },
      { text: 'Cancel', onClick: this.onCancelButtonClicked },
    ],

  })
}

Alternatively,

You may choose to emit a custom event when a button is clicked and have the parent component handle it.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adding new information to MongoDB

Greetings to all the wonderful users of stackoverflow! I'm currently encountering an issue involving mongodb and nodejs. I'm attempting to insert data into mongodb, and I have all the necessary data ready in a csv file format. Here's a brie ...

Passing parameters through the URL with Angular's ui-router

Instead of passing parameters when changing pages like this: $state.go('index.my_route', {my_param : value}); Is it possible to pass the parameter in the URL string while still using $state.go? For example: $state.go('index.my_route?my_pa ...

Understanding the [object Object] data in RadListView with NativeScript-vue

How can I display the text from an Observable Array without it showing [object Object]? I attempted to follow the example from https://docs.nativescript.org/vuejs/ns-ui/ListView/getting-started by copying and pasting, but it still displays [object Object] ...

Emailer: Missing Salutation

While attempting to send emails using Node with Nodemailer (https://github.com/nodemailer/nodemailer), the sendMail call from the Nodemailer transporter is throwing an error message of Greeting never received when connected to an Ethereal test email accoun ...

Unable to load routes from ./app.js in the file ./src/routes/index.js

Just dipping my toes into the world of nodejs. I recently moved all my routes from app.js to a separate file located at PROJECT_DIR/src/routes/index.js. However, when I try to open the page in my browser, it displays "Cannot GET /wines". Below are snippets ...

Create geometrical forms using Konva to exhibit on a canvas

How can I incorporate Konva into my Vue.js project to draw shapes on a canvas? I have an array of rectangles that I want to display, but nothing is showing up. <template> <div> <Button id="b1" @click="promp()" style=& ...

Disabling a SELECT OPTION in AG-Grid: A step-by-step guide

Is there a way to disable a dropdown element in AG-Grid using React? I've seen examples like the one on W3Schools where you can disable an option in a dropdown, but I'm not sure how to implement this in AG-Grid. The dropdown I'm working wit ...

What is the best way to acquire the opposing attribute?

There is a specific object I am working with that will always have 2 properties, never more. The object looks like this: var people = { 'John': { ... }, 'Peter': { ... } } In addition, there is a variable var name = 'John&ap ...

execute node cli command: load file before proceeding with another task

I'm working on a setup.js file that needs to be executed/set up for another file. At the moment, the contents of the setup file are just: require("module-alias/register") However, I anticipate that it may become more complex in the future, hence my ...

How to invoke a method in a nested Angular 2 component

Previous solutions to my issue are outdated. I have a header component import { Component, OnInit } from '@angular/core'; import { Router, NavigationEnd } from '@angular/router'; import { Observable } from 'rxjs/Observable'; ...

The code snippet $(this).nextAll("#...").eq(0).text("***") isn't functioning as expected

I am experiencing an issue with the following line of code: $(this).nextAll("#status").eq(0).text("Deleted"). I am trying to insert the text "Deleted" in a <span> tag, but it does not seem to be working... Here is my code: admin.php PHP: $sql = "SE ...

Error in Angular 10/ Typescript: Cannot find property in type 'Object'.ts

Transitioning from Angular 1 to Angular 10 has led me to dive into learning typescript. Please bear with me if there are any errors as I navigate this new territory. While I have a substantial amount of code, here is the main issue at hand. In Angular 1, I ...

Keep things in line with async functions in Node JS

Hello, I am just starting out with NodeJs and I have a question. I am trying to push elements into an array called files based on the order of the urls provided, but it seems like I'm getting a random order instead. Below is the code I've been wo ...

Passing PHP Variables Between Pages

I'm currently working on building a game using html5(phaser js) and I need to create a leaderboard. Here's the code snippet I have: restart_game: function() { // Start the 'main' state, which restarts the game //this.game.time.events ...

Enable my textbox to interpret the html img tag

Is there a way to display emoji images instead of emoji symbols when inserting them into my textbox? For example, can I show the image representation of ':)' instead of just the symbol itself? ...

Customizing Checkbox using CSS (additional assistance required)

I am currently working on implementing Checkbox Four with a custom design. You can check out the example I found on this website: My goal is to enhance this checkbox by incorporating red colored text 'N' when it's unchecked, and blue colore ...

Dynamic translation with Next-i18next

Is there a way to translate content using the next-i18next library without changing the locale with router.push? I have 3 locales: ['en', 'de', 'fr'], with the default set as de. When I attempt something like this: import {us ...

Formatting Numbers in HighCharts Shared Tooltip

My first experience with HighCharts JS has been quite positive. I am currently creating multiple series and using a shared tooltip feature. The numbers displayed in the shared tooltip are accurate, but the formatting is not to my liking. For instance, it s ...

What is the process for adjusting the input value dynamically in reactjs?

I am working on a dynamic time input row and I need to ensure that the values are updated correctly. This is the code snippet I am using: https://codesandbox.io/s/624vq8y7y3 When I run the code, the values in the TimeInput field do not change as expected ...

Is there a way to automatically populate an AngularJS input field?

Attempting to automate filling out a website using JavaScript. The script below: document.getElementsByClassName('form-control')[1].value = "MYNAME"; The input text changes, but upon clicking the submit button it displays as empty. Any as ...