Skip to content

Commit

Permalink
feat: add sonner toast component
Browse files Browse the repository at this point in the history
  • Loading branch information
lannodev committed Aug 8, 2024
1 parent 2b53d6f commit 017e40f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
26 changes: 24 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"angular-svg-icon": "^13.0.0",
"apexcharts": "^3.35.3",
"ng-apexcharts": "^1.7.1",
"ngx-sonner": "^2.0.1",
"rxjs": "~7.4.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.2"
Expand Down Expand Up @@ -77,4 +78,4 @@
"path": "./node_modules/cz-conventional-changelog"
}
}
}
}
1 change: 1 addition & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<div>
<router-outlet></router-outlet>
<app-responsive-helper></app-responsive-helper>
<ngx-sonner-toaster [theme]="themeService.isDark ? 'dark' : 'light'"/>
</div>
3 changes: 2 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { ThemeService } from './core/services/theme.service';
import { RouterOutlet } from '@angular/router';
import { NgClass } from '@angular/common';
import { ResponsiveHelperComponent } from './shared/components/responsive-helper/responsive-helper.component';
import { NgxSonnerToaster } from 'ngx-sonner';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
standalone: true,
imports: [NgClass, RouterOutlet, ResponsiveHelperComponent],
imports: [NgClass, RouterOutlet, ResponsiveHelperComponent, NgxSonnerToaster],
})
export class AppComponent {
title = 'Angular Tailwind';
Expand Down
4 changes: 4 additions & 0 deletions src/app/modules/uikit/pages/table/table.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ <h3 class="font-semibold text-foreground">Team Members</h3>
<tbody>
@for (user of users(); track $index) {
<tr class="hover:bg-card/50" app-table-row [user]="user"></tr>
} @empty {
<tr>
<td class="py-4 text-center text-sm" colspan="7">No users found</td>
</tr>
}
</tbody>
</table>
Expand Down
19 changes: 17 additions & 2 deletions src/app/modules/uikit/pages/table/table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { TableHeaderComponent } from './components/table-header/table-header.com
import { TableFooterComponent } from './components/table-footer/table-footer.component';
import { TableRowComponent } from './components/table-row/table-row.component';
import { TableActionComponent } from './components/table-action/table-action.component';
import { toast } from 'ngx-sonner';

@Component({
selector: 'app-table',
Expand All @@ -26,8 +27,9 @@ export class TableComponent implements OnInit {
users = signal<User[]>([]);

constructor(private http: HttpClient) {
this.http.get<User[]>('https://freetestapi.com/api/v1/users?limit=8').subscribe((data) => {
this.users.set(data);
this.http.get<User[]>('https://freetestapi.com/api/v1/users?limit=8').subscribe({
next: (data) => this.users.set(data),
error: (error) => this.handleRequestError(error),
});
}

Expand All @@ -39,5 +41,18 @@ export class TableComponent implements OnInit {
});
}

private handleRequestError(error: any) {
const msg = 'An error occurred while fetching users';
toast.error(msg, {
position: 'bottom-right',
description: error.message,
action: {
label: 'Undo',
onClick: () => console.log('Action!'),
},
actionButtonStyle: 'background-color:#DC2626; color:white;',
});
}

ngOnInit() {}
}

0 comments on commit 017e40f

Please sign in to comment.