Skip to content

Commit

Permalink
fix data display
Browse files Browse the repository at this point in the history
  • Loading branch information
khalifan-kfan committed Jan 3, 2024
1 parent e02dc68 commit 189f2ec
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -1,48 +1,33 @@
import { CommonModule } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
import { FormBuilder, FormControl, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
// eslint-disable-next-line @nx/enforce-module-boundaries
import type { Database } from '@picsa/server-types';
import { PICSAFormValidators } from '@picsa/shared/modules/forms/validators';
//import {
// IUploadResult,
// SupabaseStoragePickerDirective,
// SupabaseUploadComponent,
// } from '@picsa/shared/services/core/supabase';
// import { IStorageEntry } from '@picsa/shared/services/core/supabase/services/supabase-storage.service';
import type { Database } from '@picsa/server-types';
import { PICSAFormValidators } from '@picsa/shared/modules/forms/validators';

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

// type IResourceEntry = Database['public']['Tables']['resources']['Row'];

@Component({
selector: 'dashboard-resource-create',
selector: 'dashboard-translations-edit',
standalone: true,
imports: [
CommonModule,
// DashboardMaterialModule,
// DashboardResourcesStorageLinkComponent,
// FormsModule,
// ReactiveFormsModule,
// SupabaseStoragePickerDirective,
// SupabaseUploadComponent,
DashboardMaterialModule,
FormsModule,
ReactiveFormsModule,
],
templateUrl: './translations-edit.component.html',
styleUrls: ['./translations-edit.component.scss'],
})
export class TranslationsEditComponent {
constructor(
// private service: ResourcesDashboardService,
private service: TranslationDashboardService,
private route: ActivatedRoute
) {}







}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,47 @@
<div style="display: flex">
<h2 style="flex: 1">Translations</h2>
</div>
<table mat-table [dataSource]="service.translations">

<!-- '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>

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

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

<!-- '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
Expand Up @@ -12,3 +12,7 @@ tr.resource-row {
th {
font-weight: bold;
}

td {
cursor: pointer;
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
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 { DashboardResourcesStorageLinkComponent } from '../../components';
import { ResourcesDashboardService } from '../../translations.service';
import { TranslationDashboardService } from '../../translations.service';

export type ITranslationRow = Database['public']['Tables']['translations']['Row'];
@Component({
selector: 'dashboard-resources-page',
selector: 'dashboard-translations-page',
standalone: true,
imports: [CommonModule, DashboardMaterialModule, RouterModule],
templateUrl: './translations.page.html',
styleUrls: ['./translations.page.scss'],
})
export class TranslationsPageComponent implements OnInit {
//public displayedColumns = ['title', 'storage_file'];
constructor(public service: ResourcesDashboardService) {}
displayedColumns: string[] = [ 'en', 'mw_ny', 'sw', 'tg', 'zm_ny','created_at',];
constructor(public service: TranslationDashboardService, private router: Router) {}
ngOnInit(): void {
this.service.ready();

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

}
}
Original file line number Diff line number Diff line change
@@ -1,113 +1,41 @@
import { Injectable, signal } from '@angular/core';
import { Injectable } from '@angular/core';
// eslint-disable-next-line @nx/enforce-module-boundaries
import { Database } from '@picsa/server-types';
import { PicsaAsyncService } from '@picsa/shared/services/asyncService.service';
import { PicsaNotificationService } from '@picsa/shared/services/core/notification.service';
// import { PicsaNotificationService } from '@picsa/shared/services/core/notification.service';
import { SupabaseService } from '@picsa/shared/services/core/supabase';
import { IStorageEntry } from '@picsa/shared/services/core/supabase/services/supabase-storage.service';
import { arrayToHashmap } from '@picsa/utils';

export type ITranslationRow = Database['public']['Tables']['translations']['Row'];

export interface IResourceStorageEntry extends IStorageEntry {
/** Url generated when upload to public bucket (will always be populated, even if bucket not public) */
publicUrl: string;
}

export type IResourceEntry = Database['public']['Tables']['resources']['Row'];

@Injectable({ providedIn: 'root' })
export class ResourcesDashboardService extends PicsaAsyncService {
private storageFiles: IResourceStorageEntry[] = [];
public storageFilesHashmap: Record<string, IResourceStorageEntry> = {};
public readonly resources = signal<IResourceEntry[]>([]);
export class TranslationDashboardService extends PicsaAsyncService {

public translations: ITranslationRow[] = [];

public get table() {
return this.supabaseService.db.table('resources');
return this.supabaseService.db.table('translations');
}

constructor(private supabaseService: SupabaseService, private notificationService: PicsaNotificationService) {
constructor(private supabaseService: SupabaseService) {
super();
}

public override async init() {
await this.supabaseService.ready();
await this.listStorageFiles();
await this.listResources();
}

/** Retrieve storage db meta for a file */
public async getStorageFileById(id: string) {
// Refresh storage file cache if id not found
if (!this.storageFilesHashmap[id]) {
await this.listStorageFiles();
}
return this.storageFilesHashmap[id];
}

/**
*
* TODO - only enable super admin/local dev
* TODO - remove when no longer required
*/
public async migrateHardcodedResources() {
// NOTE - assumes storage files manually uploaded

// eslint-disable-next-line @nx/enforce-module-boundaries
const { DB_COLLECTION_ENTRIES, DB_FILE_ENTRIES, DB_LINK_ENTRIES } = await import(
'@picsa/resources/src/app/data/index'
);
console.log({ DB_COLLECTION_ENTRIES, DB_FILE_ENTRIES, DB_LINK_ENTRIES });
const ref = this.supabaseService.db.table('resources');
const uploaded: unknown[] = [];
const missing: unknown[] = [];

for (const fileEntry of Object.values(DB_FILE_ENTRIES)) {
const { type, description, url } = fileEntry;
// extract pathname from firebase url
const { pathname } = new URL(url);
const storagePath = decodeURI(pathname).replace(/%2F/g, '/').replace('/v0/b/picsa-apps.appspot.com/o/', '');
// check for equivalent storage file
const storageFile = this.storageFiles.find((file) => file.name === storagePath);
if (storageFile) {
const dbEntry: Database['public']['Tables']['resources']['Insert'] = {
description,
type,

storage_file: storageFile.id,
};
const { error } = await ref.upsert(dbEntry, { ignoreDuplicates: false });

if (error) {
console.error(error);
}
uploaded.push(fileEntry);
} else {
missing.push(fileEntry);
}
}
console.log({ uploaded, missing });
if (missing.length > 0) {
this.notificationService.showUserNotification({
matIcon: 'error',
message: `${missing.length} files missing from storage`,
});
}
await this.listTranslations();
}

private async listStorageFiles() {
const storageFiles = await this.supabaseService.storage.list('resources');
this.storageFiles = storageFiles.map((file) => ({
...file,
publicUrl: this.supabaseService.storage.getPublicLink(file.bucket_id as string, file.name as string),
}));
this.storageFilesHashmap = arrayToHashmap(this.storageFiles, 'id');
console.log('storage files', this.storageFilesHashmap);
}

private async listResources() {
const { data, error } = await this.supabaseService.db.table('resources').select<'*', IResourceEntry>('*');
private async listTranslations() {
const { data, error } = await this.supabaseService.db.table('translations').select<'*', ITranslationRow>('*');
if (error) {
throw error;
}
this.resources.set(data);
this.translations = data || [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
create table
public.translations (
id bigint generated by default as identity,
created_at timestamp with time zone not null default now(),
en text null,
mw_ny text null,
sw text null,
tg text null,
zm_ny text null,
constraint translations_pkey primary key (id)
) tablespace pg_default;
30 changes: 30 additions & 0 deletions apps/picsa-server/supabase/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,36 @@ export interface Database {
}
Relationships: []
}
translations: {
Row: {
created_at: string
en: string | null
id: number
mw_ny: string | null
sw: string | null
tg: string | null
zm_ny: string | null
}
Insert: {
created_at?: string
en?: string | null
id?: number
mw_ny?: string | null
sw?: string | null
tg?: string | null
zm_ny?: string | null
}
Update: {
created_at?: string
en?: string | null
id?: number
mw_ny?: string | null
sw?: string | null
tg?: string | null
zm_ny?: string | null
}
Relationships: []
}
}
Views: {
storage_objects: {
Expand Down
24 changes: 12 additions & 12 deletions apps/picsa-tools/resources-tool/src/assets/resources/contents.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"covers/facebook.svg": {
"relativePath": "covers/facebook.svg",
"size_kb": 1.1,
"md5Checksum": "8626bc9119bd6b8bc0fa04e8a20b6e74",
"modifiedTime": "2022-09-28T20:19:52.986Z"
"md5Checksum": "a09b2f44f6b44b5b6e29aedba9f92029",
"modifiedTime": "2023-07-10T22:11:12.359Z"
},
"covers/gap.jpg": {
"relativePath": "covers/gap.jpg",
Expand All @@ -61,9 +61,9 @@
},
"covers/gender-equality.svg": {
"relativePath": "covers/gender-equality.svg",
"size_kb": 5.5,
"md5Checksum": "0b6599c4d8b3a87b787910b18a9d8851",
"modifiedTime": "2022-11-01T20:27:18.286Z"
"size_kb": 5.4,
"md5Checksum": "95af38f37c2179d4ad0c5288cbb236ed",
"modifiedTime": "2023-07-10T22:11:12.359Z"
},
"covers/gras-nelk.jpg": {
"relativePath": "covers/gras-nelk.jpg",
Expand Down Expand Up @@ -110,8 +110,8 @@
"covers/spreadsheet.svg": {
"relativePath": "covers/spreadsheet.svg",
"size_kb": 2.8,
"md5Checksum": "a03a4bfb0a1791fda8e232a8b032715b",
"modifiedTime": "2022-09-30T09:35:53.529Z"
"md5Checksum": "0a1bc91188bbc03fda0a7cb2bc1aa6ce",
"modifiedTime": "2023-07-10T22:11:12.360Z"
},
"covers/twitter.png": {
"relativePath": "covers/twitter.png",
Expand All @@ -121,15 +121,15 @@
},
"covers/videos.svg": {
"relativePath": "covers/videos.svg",
"size_kb": 3.5,
"md5Checksum": "9bee23e2f74d07bd0cdd27bdc286e777",
"modifiedTime": "2023-09-25T18:01:08.322Z"
"size_kb": 3.4,
"md5Checksum": "37516a0c19e20344d02960c6368dde62",
"modifiedTime": "2023-11-16T07:09:42.068Z"
},
"covers/weather.svg": {
"relativePath": "covers/weather.svg",
"size_kb": 3.1,
"md5Checksum": "db07c7d20d9520124c251c810c5817fd",
"modifiedTime": "2022-09-28T19:22:58.064Z"
"md5Checksum": "ca87f24916d258b8b8b5dd8343d4305b",
"modifiedTime": "2023-07-10T22:11:12.360Z"
},
"covers/whatsapp.svg": {
"relativePath": "covers/whatsapp.svg",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class SupabaseStorageFilePickerComponent {
const entries = await this.storage.list(this.storageBucketName, this.storageFolderPath);
this.fileEntries = entries
// filter out metadata files which have filename starting with `.`
.filter(({ name }) => !name?.split('/').pop()?.startsWith('.'))
.filter(({ name }) => name && !name.split('/').pop()?.startsWith('.'))
.sort((a, b) => {
if (!b.name) return 1;
if (!a.name) return -1;
Expand Down
Loading

0 comments on commit 189f2ec

Please sign in to comment.