Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dashboard) - add translations frontend and db #211

Merged
merged 9 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/picsa-apps/dashboard/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ export class AppComponent implements AfterViewInit {
// label: 'Monitoring Forms',
// href: '/monitoring-forms',
// },
// {
// label: 'Translations',
// href: '/translations',
// },
{
label: 'Translations',
href: '/translations',
},
];

globalLinks: INavLink[] = [
Expand Down
4 changes: 4 additions & 0 deletions apps/picsa-apps/dashboard/src/app/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ export const appRoutes: Route[] = [
path: 'climate-data',
loadChildren: () => import('./modules/climate-data/climate-data.module').then((m) => m.ClimateDataModule),
},
{
path: 'translations',
loadChildren: () => import('./modules/translations/translations.module').then((m) => m.TranslationsPageModule),
},
];
2 changes: 1 addition & 1 deletion apps/picsa-apps/dashboard/src/app/material.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const matModules = [
MatStepperModule,
MatTableModule,
MatTabsModule,
MatToolbarModule,
MatToolbarModule
];

@NgModule({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<div class="page-content">
<h2>Edit Translations</h2>
@if(translationRow){
<form class="form-content">
<div class="form-data">
<label>Date of Creation</label>
<div>{{ translationRow.created_at | date: 'mediumDate' }}</div>
chrismclarke marked this conversation as resolved.
Show resolved Hide resolved
</div>

<div class="form-data">
<label>English</label>
<input matInput [(ngModel)]="translationRow.en" name="en" />
</div>

<div class="form-data">
<label>mw_ny</label>
<input matInput [(ngModel)]="translationRow.mw_ny" name="mw_ny" />
</div>

<div class="form-data">
<label>sw</label>
<input matInput [(ngModel)]="translationRow.sw" name="sw" />
</div>
<div class="form-data">
<label>tg</label>
<input matInput [(ngModel)]="translationRow.tg" name="tg" />
</div>
<div class="form-data">
<label>zm_ny</label>
<input matInput [(ngModel)]="translationRow.zm_ny" name="zm_ny" />
</div>
<button mat-raised-button color="primary" class="submitButton" (click)="submitForm()">Update</button>
</form>
} @else if(dataLoadError) {
<div>{{dataLoadError}}</div>
} @else {
<div>Loading...</div>
}
@if(editActionFeedbackMessage){
<div>{{editActionFeedbackMessage}}</div>
}
@if(translationRow){
<h2>Delete this Translation</h2>
<button mat-raised-button color="primary" class="deleteButton" (click)="openTranslationDeleteDialog()">Delete this
chrismclarke marked this conversation as resolved.
Show resolved Hide resolved
translation <mat-icon color="white">delete</mat-icon></button>
}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.form-content{
display: flex;
flex-direction: column;
gap: 1.4rem;
}
.submitButton{
width: 7rem;
margin-bottom: 1rem;
}
.deleteButton{
width: 15rem;
}
.form-data{
display: flex;
flex-direction: column;
gap: 0.5rem;

}
input {
padding: 8px;
color: var(--color-primary);
outline-color: var(--color-primary);
max-width: 300px;
display: block;
font-size: 0.9rem;
white-space: pre-wrap;
word-wrap: break-word;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { TranslationsEditComponent } from './translations-edit.component';

describe('TranslationsEditComponent', () => {
let component: TranslationsEditComponent;
let fixture: ComponentFixture<TranslationsEditComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [TranslationsEditComponent],
}).compileComponents();

fixture = TestBed.createComponent(TranslationsEditComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ActivatedRoute,Router } from '@angular/router';
import { RouterModule } from '@angular/router';
// eslint-disable-next-line @nx/enforce-module-boundaries
import type { Database } from '@picsa/server-types';

import { DashboardMaterialModule } from '../../../../material.module';
// import { DashboardResourcesStorageLinkComponent } from '../../components/storage-link/storage-link.component';
import { TranslationDashboardService } from '../../translations.service';

type ITranslationEntry = Database['public']['Tables']['translations']['Row'];

@Component({
selector: 'dashboard-translations-edit',
standalone: true,
imports: [CommonModule, DashboardMaterialModule, FormsModule, ReactiveFormsModule,RouterModule],
templateUrl: './translations-edit.component.html',
styleUrls: ['./translations-edit.component.scss'],
})
export class TranslationsEditComponent {
translationRow: ITranslationEntry;
dataLoadError: string;
editActionFeedbackMessage: string;
constructor(private service: TranslationDashboardService, private route: ActivatedRoute, private router: Router) {
this.service.ready();
this.route.params.subscribe((params) => {
Copy link
Collaborator

@chrismclarke chrismclarke Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit(non-blocking) - Any time you create a subscription you are using browser memory. So when the subscription is no longer required you should remove it. This is commonly done by linking the subscription to ngOnInit and ngOnDestroy methods, e.g.
https://blog.bitsrc.io/6-ways-to-unsubscribe-from-observables-in-angular-ab912819a78f.

Would be good to tidy this up next time working on the code

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, will surely tidy up next time

const id = params['id'];
this.service
.getTranslationById(id)
.then((data) => {
this.translationRow = data;
})
.catch((error) => {
console.error('Error fetching translation:', error);
this.dataLoadError = 'Failed to fetch translation.';
});
});
}

submitForm() {
this.service
.updateTranslationById(this.translationRow.id, this.translationRow)
.then((data) => {
if (data === 'Updated successfully') {
this.editActionFeedbackMessage = 'Updated successfully';
}
})
.catch((error) => {
console.error('Error editing translation:', error);
this.editActionFeedbackMessage = 'Failed to edit translation.';
});
}
deleteTranslation(id:number){
this.service.deleteTranslationById(id).then((data) => {
if (data === 'Deleted Successfully') {
this.router.navigate([`/translations`]);
}
})
.catch((error) => {
console.error('Error deleting translation:', error);
});
}
async openTranslationDeleteDialog() {
//failed attempt to use a shared delete model
this.deleteTranslation(this.translationRow.id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<div class="page-content">
<div style="display: flex; align-items: center">
<h2 style="flex: 1">Translations</h2>
<button mat-raised-button color="primary" routerLink="/translations/new">Add New Translation</button>
</div>
@if(service.translations){
<table mat-table [dataSource]="service.translations">
chrismclarke marked this conversation as resolved.
Show resolved Hide resolved
<!-- 'tool' Column -->
<ng-container matColumnDef="tool" clickable>
<th mat-header-cell *matHeaderCellDef>Tool</th>
<td mat-cell *matCellDef="let element">{{ element.tool }}</td>
</ng-container>

<!-- 'context' Column -->
<ng-container matColumnDef="context" clickable>
<th mat-header-cell *matHeaderCellDef>Context</th>
<td mat-cell *matCellDef="let element">{{ element.context }}</td>
</ng-container>

<!-- 'en' Column -->
<ng-container matColumnDef="en" clickable>
<th mat-header-cell *matHeaderCellDef>en</th>
<td mat-cell *matCellDef="let element">{{ element.en }}</td>
</ng-container>

<!-- 'mw_ny' Column -->
<ng-container matColumnDef="mw_ny">
<th mat-header-cell *matHeaderCellDef>mw_ny</th>
<td mat-cell *matCellDef="let element">{{ element.mw_ny }}</td>
</ng-container>

<!-- 'zm_ny' Column -->
<ng-container matColumnDef="zm_ny">
<th mat-header-cell *matHeaderCellDef>zm_ny</th>
<td mat-cell *matCellDef="let element">{{ element.zm_ny }}</td>
</ng-container>

<!-- 'ke_sw' Column -->
<ng-container matColumnDef="sw">
<th mat-header-cell *matHeaderCellDef>ke_sw</th>
<td mat-cell *matCellDef="let element">{{ element.ke_sw }}</td>
</ng-container>

<!-- 'tg' Column -->
<ng-container matColumnDef="tg">
<th mat-header-cell *matHeaderCellDef>tj_tg</th>
<td mat-cell *matCellDef="let element">{{ element.tj_tg }}</td>
</ng-container>

<!-- 'created_at' Column -->
<ng-container matColumnDef="created_at">
<th mat-header-cell *matHeaderCellDef>Date</th>
<td mat-cell *matCellDef="let element">{{ element.created_at | date: 'mediumDate' }}</td>
</ng-container>

<!-- Table Rows -->
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;" (click)="goToRecord(row)"></tr>
</table>
}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
table.resources-table {
max-height: 50vh;
display: block;
overflow: auto;
}
tr.resource-row {
cursor: pointer;
&:hover {
background: whitesmoke;
}
}
th {
font-weight: bold;
}

td {
cursor: pointer;
}

.disallow-click{
pointer-events: none;
cursor: not-allowed;
}
.allow-click{
pointer-events:all;
cursor: pointer;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { TranslationsPageComponent } from './translations.page';

describe('TranslationsPageComponent', () => {
let component: TranslationsPageComponent;
let fixture: ComponentFixture<TranslationsPageComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [TranslationsPageComponent],
}).compileComponents();

fixture = TestBed.createComponent(TranslationsPageComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { CommonModule } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { RouterModule } from '@angular/router';
import { Router } from '@angular/router';
// eslint-disable-next-line @nx/enforce-module-boundaries
import { Database } from '@picsa/server-types';

import { DashboardMaterialModule } from '../../../../material.module';
import { TranslationDashboardService } from '../../translations.service';

export type ITranslationRow = Database['public']['Tables']['translations']['Row'];
@Component({
selector: 'dashboard-translations-page',
standalone: true,
imports: [CommonModule, DashboardMaterialModule, RouterModule],
templateUrl: './translations.page.html',
styleUrls: ['./translations.page.scss'],
})
export class TranslationsPageComponent implements OnInit {
displayedColumns: string[] = ['tool', 'context', 'en', 'mw_ny', 'zm_ny', 'sw', 'tg', 'created_at'];

constructor(public service: TranslationDashboardService, private router: Router) {}
ngOnInit(): void {
this.service.ready();
this.refreshTranslations();
}

goToRecord(row: ITranslationRow) {
this.router.navigate([`/translations`, row.id]);
}

refreshTranslations() {
this.service.listTranslations().catch((error) => {
console.error('Error fetching translations:', error);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<div class="page-content">
<h2>New Translations</h2>

<form class="form-content">

<div class="form-data">
<label>English</label>
<input matInput [(ngModel)]="en" name="en" />
</div>

<div class="form-data">
<label>mw_ny</label>
<input matInput [(ngModel)]="mw_ny" name="mw_ny" />
</div>

<div class="form-data">
<label>sw</label>
<input matInput [(ngModel)]="sw" name="sw" />
</div>
<div class="form-data">
<label>tg</label>
<input matInput [(ngModel)]="tg" name="tg" />
</div>
<div class="form-data">
<label>zm_ny</label>
<input matInput [(ngModel)]="zm_ny" name="zm_ny" />
</div>
<button mat-raised-button color="primary" class="submitButton" (click)="submitForm()">Submit</button>
</form>

@if(editActionFeedbackMessage){
chrismclarke marked this conversation as resolved.
Show resolved Hide resolved
<div>{{editActionFeedbackMessage}}</div>
}
</div>
Loading
Loading