In addition to the Angular Material UI components that were used so far, I’ll be using the Dialog and Icon components.
Adding the Angular Material Dialog and Icon Modules
To begin, open the project from where we left off in the last session and log out.
Creating the PostFeed Component
Save the project and open the terminal to create the PostFeed component.
ng generate component pages/PostFeed
Go to the app routing module file and add a route. Use postfeed for the path and the PostFeed component for the component
Go to the App Component TypeScript file and locate the getUserProfile() function.
If we take a look at the run down of our app, when a visitor visits our web site,
If we run the app, nothing will happen, since we are logged out. If we log in, we’ll be directed to the post feed page.
Adding the create post button
Go to the Post Feed component HTML page.
Go to the Post Feed CSS file and add the selector for the post-button. The button will have a fixed position, at the bottom right corner, 2em away.
If we go to our app, the button will appear at the bottom right corner.
Creating the Create Post Component
Let’s add the dialog to create a post. Go to the Post-Feed HTML page and attach a click event to the button. Then go to the Post Feed TypeScript file and define the function.
Post Feed HTML
Post Feed TypeScript
Import MatDialog from Angular Material and then inject it inside the constructor.
import { MatDialog } from '@angular/material/dialog';
constructor(private dialog: MatDialog) { }
Similar to the Bottom Sheet, we can use the Material Dialog object to open a dialog.
In the terminal, create another component call CreatePost and place it in the tools directory.
ng generate component tools/CreatePost
Once the component is created, pass the component inside the open() method.
If we go to the app and click on the add post button, a dialog will show up.
Now let’s add the content for the component.
Adding the content to the Create Post Component
Go to the create post component HTML page.
For the content, add a div element underneath the header element and place a textatrea inside.
If we go the app and open the dialog, we’re almost there. We just need to hide the input element and set the styles for the textarea.
Go to the Create Post component CSS file and add the selectors for photo-upload and textarea.
For photo-upload, set the display to none so it will not be visible.
If we go to our app again, and open the dialog, it will look closer to how we want it. If we click on the icon button, it will open a dialog to choose our image.
Adding the image preview section
The next step is to add the preview section so the chosen image will be displayed.
Go to the create-post HTML page.
Go to the Create Post TypeScript file and define the function for the change event.
selectedImageFile: File;
Inside the onPhotoSelected function, get the selected file by taking the input and accessing the files property.
If we go back to the HTML page, the preview image container element is an image element call post-preview-image.
To stop this code from crashing because no image is selected, add an if statement above the FileReader object and return out of the method when selectedImageFile is null.
Lastly, go to the create-post HTML page and locate the post-preview div. We only want to show the preview if there is a selected image file.