DEV Community

Discussion on: What is the best way to manage the messages shown to users?

Collapse
microtot profile image
MicroTot • Edited on

Can I get a snippet of your registration function so I have something to work with?
I don't know if this is a good practice but you can use the .subscribe() method:

In your imports:

import { HttpClient } from "@angular/common/http";
import { MatSnackBar } from "@angular/material/snack-bar";
Enter fullscreen mode Exit fullscreen mode

In your constructor:

constructor(private http: HttpClient, private snack: MatSnackBar,)
Enter fullscreen mode Exit fullscreen mode

Your function e.g. user creates a post/logs in:

login(){
    this.http.post( "your API endpoint", your_data ).subscribe((res:any) => (
      console.log(res),
      this.snack.open("Post created successfully", res);
    ), 
    (error:any) => (
      this.snack.open("Error Creating post", error);
    ))
  }
Enter fullscreen mode Exit fullscreen mode
Collapse
joaomarcos160 profile image
João Marcos Author

Thanks, but I meant the messages themselves, for example, I could put them all in a messages.json and it would be something like this:

{
     success: "Success!",
     fail: "Something went wrong",
     create_success: "Created successfully"
}
Enter fullscreen mode Exit fullscreen mode

But I don't know if making this file is a good practice, or leaving everything hard code is good, but thanks anyway :)