What is the process for verifying a particular user in AngularJS?

I'm new to AngularJS and I'm a bit confused about the concepts of GET, PUT requests. I am currently working on an app where I display a list of users on one page, and on another page, I have a form with three buttons. My main focus is on the "Confirm" button. What I want to achieve with the confirm button is to validate a specific user by calling PUT /users/{userId}/confirm. I'm stuck at this point and need some guidance. I have successfully implemented the GET request to fetch the users from the backend. However, implementing the PUT request is proving to be challenging.

This is the current code snippet: services

 app.factory('people', ['$http', function($http) {
    var userInfo = {  
    getUserInfo: function () {
   return $http.get('https://admin/v1/unconfirmed_users');


     },




   };
   return userInfo;
  }]);

 app.factory('people', function($http){

 var services = {};
  services.getUserInfo = function() {

    return $http.put('https://users/{userId}/confirm');
  };
 return services;


})

App.js

  var app = angular.module("Portal", ['ngRoute',  'ui.bootstrap' ]);


  app.controller('MyCtrl', function($scope) {


     $scope.inactive = true;

     $scope.confirmedAction = function(person) {

     $scope.userInfo.users.splice(person.id, 1);


      location.href = '#/user';



        person.data = "true";
       console.log(person.data);
        console.log(person);





    };

  });


app.directive('confirmClick', ['$q', 'dialogModal', function($q, dialogModal) 
  {
    return {
        link: function (scope, element, attrs) {
            // ngClick won't wait for our modal confirmation window to resolve,
            // so we will grab the other values in the ngClick attribute, which
            // will continue after the modal resolves.
            // modify the confirmClick() action so we don't perform it again
            // looks for either confirmClick() or confirmClick('are you sure?')
            var ngClick = attrs.ngClick.replace('confirmClick()', 'true')
                .replace('confirmClick(', 'confirmClick(true,');

            // setup a confirmation action on the scope
            scope.confirmClick = function(msg) {
                // if the msg was set to true, then return it (this is a workaround to make our dialog work)
                if (msg===true) {
                    return true;
                }
                // msg can be passed directly to confirmClick('Are you sure you want to confirm?')
                // in ng-click
                // or through the confirm-click attribute on the
                // <a confirm-click="Are you sure you want to confirm?"></a>
                msg = msg || attrs.confirmClick || 'Are you sure you want to confirm?';
                // open a dialog modal, and then continue ngClick actions if it's confirmed
                dialogModal(msg).result.then(function() {
                    scope.$eval(ngClick);
                });
                // return false to stop the current ng-click flow and wait for our modal answer
                return false;
            };
        }
    }
}])

/*
 Modal confirmation dialog window with the UI Bootstrap Modal service.
 This is a basic modal that can display a message with yes or no buttons.
 It returns a promise that is resolved or rejected based on yes/no clicks.
 The following settings can be passed:

 message         the message to pass to the modal body
 title           (optional) title for modal window
 okButton        text for YES button. set false to not include button
 cancelButton    text for NO button. ste false to not include button

 */
.service('dialogModal', ['$modal', function($modal) {
    return function (message, title, okButton, cancelButton) {
        // setup default values for buttons
        // if a button value is set to false, then that button won't be included
        cancelButton = cancelButton===false ? false : (cancelButton || 'No');
        okButton = okButton ===false ? false : (okButton || 'Yes');

        // setup the Controller to watch the click
        var ModalInstanceCtrl = function ($scope, $modalInstance, settings) {
            // add settings to scope
            angular.extend($scope, settings);
            // yes button clicked
            $scope.ok = function () {
                $modalInstance.close(true);
            };
            // no button clicked
            $scope.cancel = function () {
                $modalInstance.dismiss('cancel');
            };
        };

        // open modal and return the instance (which will resolve the promise on ok/cancel clicks)
        var modalInstance = $modal.open({
            template: '<div class="dialog-modal"> \
              <div class="modal-header" ng-show="modalTitle"> \
                  <h3 class="modal-title">{{modalTitle}}</h3> \
              </div> \
              <div class="modal-body">{{modalBody}}</div> \
              <div class="modal-footer"> \
                  <button class="btn btn-primary" ng-click="ok()" ng-show="okButton">{{okButton}}</button> \
                  <button class="btn btn-warning" ng-click="cancel()" ng-show="cancelButton">{{cancelButton}}</button> \
              </div> \
          </div>',
            controller: ModalInstanceCtrl,
            resolve: {
                settings: function() {
                    return {
                        modalTitle: title,
                        modalBody: message,
                        okButton: okButton,
                        cancelButton: cancelButton
                    };
                }
            }
        });
        // return the modal instance
        return modalInstance;
    }
}])

 //Our routes
  app.config(function ($routeProvider) {
  $routeProvider
  .when("/user", {
    controller: "HomeController",
    templateUrl: "partials/home.html"
  })
  .when("/user/:id", {
    controller: "UserController",
    templateUrl: "partials/about.html"

})
.otherwise({
    redirectTo: '/user'

  });

  });

Answer №1

app.factory('staff', function ($http) {
    var service = {};
    service.fetchEmployeeData = function () {
        return $http.get('https://admin/v1/employee_data');
    };
    service.confirmEmployee = function (employeeId) {
        return $http.put('https://employees/' + employeeId + '/confirm', {});
    };
    return service;
});

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

Control the data options available in the autosuggest textbox

I have implemented an autosuggest feature in a text box with predefined values. When typing the first letter in the textbox, a dropdown displays all related values. Is there a way to limit the number of displayed related values to 20? This is what my cu ...

Begin the input box with some text

Currently, I am trying to incorporate a form field that automatically adds "http://" when clicked or typed into by a user. Below is the code snippet I have been using: <script type="text/javascript"> var input = $( "#website" ); input.val( input ...

What is the method for implementing type notation with `React.useState`?

Currently working with React 16.8.3 and hooks, I am trying to implement React.useState type Mode = 'confirm' | 'deny' type Option = Number | null const [mode, setMode] = React.useState('confirm') const [option, setOption] ...

Struggle with registering fonts in Canvas using JavaScript

I've been struggling to add a custom font to my canvas for hosting the bot. Even though I'm not encountering any errors, the font fails to display on the host. Below is the code snippet: const { AttachmentBuilder } = require('discord.js&apos ...

How can Firebase and Ionic be used to customize the password reset template for sending verification emails and more?

I'm facing an issue with firebase's auth templates not supporting my native language. Is there a way to customize the password reset template to also handle verification and email address change emails? ...

Function cascading

I'm currently facing a slight confusion with the got module, available at https://www.npmjs.com/package/got. The sequence and structure of functions within this module has got me puzzled. It's clear that you can chain listeners and functions toge ...

Is it necessary to use callbacks when using mongoose's findbyid with express to retrieve an object from the database? Are callbacks still important in modern JavaScript frameworks?

I'm currently exploring the express local library tutorial on MDN docs and wanted to try out returning an object without relying on a callback method. When I provide the request object parameter for the id to the findById mongoose method like this va ...

What is the best way to retrieve the initial element from a map containing certain data?

I am attempting to retrieve the first image path directory from an API that contains an Image so I can assign the value to the Image source and display the initial image. However, when using fl[0], all values are returned. Below is the code snippet: {useL ...

Slide your finger upwards to shut down the mobile navbar in bootstrap

Hey there, I'm currently developing a website using Bootstrap framework v3.3.4. I have a mobile navbar that opens when I click a toggle button, but I would like it to slide up to close the navigation instead. Here is an image showing the issue Do y ...

Designing a login system with MEAN stack architecture?

I am currently in the process of building a web application using the MEAN stack (MongoDB, Express, AngularJS, and node.js). Specifically, I am working on implementing a login system and securing certain routes in my Angular app so that they are only acces ...

Having trouble getting NPM environment variables to function properly on a Windows system?

I have a confusion in my package.json file where I am attempting to compile less code using versioning. Here is an example of what I am trying to achieve: "scripts" { ... "build:css": "lessc --source-map css/index.less build/$npm_package_name.$npm_package ...

Ways to implement material-ui button design on an HTML-native button

I am using pure-react-carousel which provides me an unstyled HTML button (ButtonBack). I would like to customize its style using material-ui. Trying to nest buttons within buttons is considered not allowed. An approach that works is manually assigning th ...

The Angular application is only functional after the page has been refreshed

I am currently working on a Wordpress website that includes a page featuring an Angular application. Initially, everything was functioning properly. However, I recently encountered an issue where the Angular app fails to load when I access the page through ...

JavaScript Equivalent of Declaration in TypeScript

In my Next JS application, I encountered a situation where a line of code relies on a variable from a script src in the app.tsx page. Here's how it looks: app.tsx: <script src="https://js.stripe.com/v3/"></script> config.ts: de ...

Generating an array of objects using Jquery from XML data

I need assistance with extracting data from XML text nodes stored in a variable and then creating an array of objects using jQuery. The XML data I have is as follows: var header = ['name', 'data1', 'data2']; var data = &apos ...

Tips for receiving responses when data is posted to a URL in Node.js using `req.body.url`

How can I retrieve data from the URL provided in req.body.url using Postman's collection tool? In my code snippet, I have defined a key as "URL" and the corresponding value as a live URL. You can refer to the screenshot below for more details: const ...

issue with eval() function

I am attempting to convert a JSON string from my .php file using the eval() function, but it is not working. The browser console shows a SyntaxError: expected expression, got '<'... However, when I comment out the line where eval() is used an ...

Get all inputs with the same text using Vue 3 composition API and a single ref, or access multiple inputs with the

One of the challenges I'm facing is managing multiple instances of a component called InputWithValidation within a form. I need to check if all instances are valid at once. For a single instance of InputWithValidation, I can easily verify its validit ...

The Optimal Approach for Importing Libraries across Multiple Files

I have two files - one containing the main code execution, and the other solely consisting of a class. For instance: File_1: const _ = require('underscore'), CoolClass = require('CoolClass'); _.map(//something) Files_2: const _ = ...

Embedding controller variables into the view using Angular binding in HTML

Looking to connect a variable from the controller to the view: In the Index HTML file: <body ng-app="widget" ng-controller="WidgetCtrl"> <div ng-view class="chatWidget"></div> </body> In the controller: $scope.displaySched ...