Fullstack Hub

[Count: 1]

In earlier parts, we created the Contact Us page to get the user feedback, the user message is only sent in an email to the site admin, in this part, we will also save the message in the database and create the Admin page to manage them, we will use the same Data Grid control we created in the previous part and this time we will only provide the delete message functionality.

Introduction

In this part, we will create the admin section for the Contact Us page where we can view the user messages and delete them. This would be a short article as compared to the previous one and you would see the same Data Grid in action that we created in the previous part and used in Menu Mangement

Let’s Start

It is strongly recommended to read all previous parts before moving to this one, I can’t promise you would understand all the steps I am going to list down without knowledge of previous parts. 

  1. In early parts, the Contact Us page was only sending an email to the admin, in this part, we will also save it in the database and create the GET, POST and DELETE APIs, there would be no PUT API since it doesn’t make sense to update the user message. So let’s update the APIs in the node.js Server application.
  2. Before jumping into the coding, run the mazharncoweb Client application by ng server -o command and server application by pm2 start environment.json --env development command. Make sure everything is working fine with no issue. Keep the website running so that you can see the live updates.
  3. In the Server application, edit the server -> routes -> contact.js and replace its contents with the following:
JS
  1. Here, you can see we add the GET API that is loading all the messages from the database without a filter. The POST API is extended to have database insertion logic along with the send email method. The DELETE API is first looking for a record from the database against ID sent from the Client application and deleting the record by ID.  
  2. Make sure server -> models -> contactMdl.js has all database fields and looks like the following: 
JS
  1. That’s all on the server-side, run the command on the server terminal: pm2 restart mazharnco  --update-env to update the changes, to make sure your updates are working, test the http://localhost:3000/api/contact URL in Postman (we learned how to use the Postman in previous parts)
  2. Now move to Client application mazharncoweb, let’s do the small modification and update the Contact Interface name from Contact to IContact, not required but I like to keep the things according to conventions, edit the src -> app -> model -> contact.ts and replace its content with the following: 
TypeScript
  1. Next, let’s create the UserMessage admin component to view and delete the messages. We don’t need ManageMesssage component since we are not adding and updating the user messages. In the client application terminal (right click on a mazharncoweb folder and select the option Open in Terminal) and run the command: ng g c admin/UserMessage
  2. Edit the app -> admin -> user-message -> user-message.component.css and add the following CSS in it: 
mat-card
{
  margin: 0px;
  padding: 0px;
}

mat-card-noshadow{ 
    background: #ECECF4;
    box-shadow: none !important;
}

mat-card-content{
  margin: 0px;
  padding: 0px;
}

.header
{
background: #E90C0C;
border-bottom: 5px solid #790606;
height: 50px;
padding-left: 5px;
}

.subheader
{
height: 40px;
background: #5B5C60;
}

.subheader_title{
    vertical-align:baseline;
    padding-top: 5px;
    padding-bottom: 0px;
    padding-left: 5px;
    font-size: 13pt;
    font-family: Arial, Helvetica, sans-serif;
    color: #C8CCD2;
}

.header_title{
    vertical-align:baseline;
    padding-top: 10px;
    padding-bottom: 0px;
    padding-left: 5px;
    font-size: 16pt;
    font-family: Arial, Helvetica, sans-serif;
    color: #FFFFFF;
}

.card_cntnt
{
    padding: 15px;
    padding-bottom: 15px;
}

.card_footer
{
    text-align: left;
    padding-left: 40px;
}

.frm-ctrl {
    width: 90%;
}

.icon_align
{
    vertical-align: middle;
}
.phone_cntnt
{
    font-weight: bold;
}
  1. Nothing new, it is the same old CSS we are using in previous parts components, you can change the color, font size and style according to your convenience.
  2. Edit the app -> admin -> user-message -> user-message.component.html and replace its content with the following:
<div>
  <mat-card>
    <mat-card-header class="header">
      <mat-card-title class="header_title"><i class="material-icons">message</i> Message Management</mat-card-title>
    </mat-card-header>
    <mat-card-content class="card_cntnt">
      <div>
        <app-datagrid
        [displayedColumns]="displayedColumns"
        [filterPlaceholder]="filterPlaceholder"
        [data]="data"
        (btnclick)="deleteContact($event)"
        ></app-datagrid>
      </div>
    </mat-card-content>
  </mat-card>
</div>
<button [routerLink]="['../../admin']" mat-button color="primary">Back to Menu</button>
  1. In the above HTML, we are using the Data Grid component created in the previous part, you can see it is almost the same as MenuManagement component, this is a great example of reusability. 
  2. Edit the app -> admin -> user-message -> user-message.component.ts and replace its content with the following:
TypeScript
  1. In the above code, the loadData() function is calling the contact GET API we created in the initial steps of this article and tested in Postman, the result is saved in data a variable that we are passing as an argument to Data Grid component (check the HTML). The displayColumns variable has all the fields we want to display on data Grid along with the Delete button at the end. This is also an argument to Data Grid component. The deleteContact() method is calling the Delete API we created earlier and removing the selected record from the database, you can see we are passing the record ID as an argument to Delete API. This ID is being returned by gridaction objects from Data Grid, you can see the in user-message.component.html, the deleteComponent() is an output event that is triggered when the user clicks on the Delete button in the Data Grid. 
  2. The last step is to add the newly created UserMassage component route in the app-routing.module.ts, edit the src -> app -> app-routing.module.ts file and replace its content with the following:
TypeScript
  1. We added the admin/usermsg path to UserMessageComponent page we created. That’s it. Run the application if it not already running, go to URL: http://localhost:4200/admin and click the User Messages link, you should land to Message Management page, test the page by deleting the existing message if any, go to http://localhost:4200/contact and create new messages, make sure they appear in newly created Message Management page and you are able to delete them. 

Yaseer Mumtaz

Leave a Reply

Your email address will not be published. Required fields are marked *