Skip to content

Commit f34e9f2

Browse files
committed
Improve Performance
1 parent b119273 commit f34e9f2

File tree

5 files changed

+97
-5
lines changed

5 files changed

+97
-5
lines changed

functions/src/models/translation-history.model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import { Timestamp } from 'firebase-admin/firestore';
22

33
export enum TranslationHistoryType {
44
PUBLISHED = 'PUBLISHED',
5+
CREATE = 'CREATE',
6+
UPDATE = 'UPDATE',
7+
DELETE = 'DELETE',
58
}
69

710
export interface TranslationHistory {
811
type: TranslationHistoryType;
912
description?: string;
13+
key?: string;
1014
name?: string;
1115
email?: string;
1216
createdAt: Timestamp;

functions/src/translations.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { logger } from 'firebase-functions/v2';
22
import { HttpsError, onCall } from 'firebase-functions/v2/https';
3-
import { onDocumentCreated } from 'firebase-functions/v2/firestore';
3+
import { onDocumentCreated, onDocumentWritten } from 'firebase-functions/v2/firestore';
44
import { FieldValue, WithFieldValue } from 'firebase-admin/firestore';
55
import { protos } from '@google-cloud/translate';
66
import { canPerform } from './utils/security-utils';
@@ -12,7 +12,7 @@ import { findSpaceById, findTranslations, findTranslationsHistory } from './serv
1212
const translationsPublish = onCall<PublishTranslationsData>(async request => {
1313
logger.info('[translationsPublish] data: ' + JSON.stringify(request.data));
1414
logger.info('[translationsPublish] context.auth: ' + JSON.stringify(request.auth));
15-
const {auth, data } = request
15+
const { auth, data } = request;
1616
const { spaceId } = data;
1717
if (!canPerform(UserPermission.TRANSLATION_PUBLISH, request.auth)) throw new HttpsError('permission-denied', 'permission-denied');
1818
const spaceSnapshot = await findSpaceById(spaceId).get();
@@ -121,7 +121,39 @@ const onTranslationCreate = onDocumentCreated('spaces/{spaceId}/translations/{tr
121121
return;
122122
});
123123

124+
const onTranslationWrite = onDocumentWritten('spaces/{spaceId}/translations/{translationId}', async event => {
125+
logger.info(`[Translation:onWrite] eventId='${event.id}'`);
126+
logger.info(`[Translation:onWrite] params='${JSON.stringify(event.params)}'`);
127+
const { spaceId } = event.params;
128+
129+
// No Data
130+
if (!event.data) return;
131+
const { before, after } = event.data;
132+
const beforeData = before.data() as Translation | undefined;
133+
const afterData = after.data() as Translation | undefined;
134+
const addHistory: WithFieldValue<TranslationHistory> = {
135+
type: TranslationHistoryType.CREATE,
136+
createdAt: FieldValue.serverTimestamp(),
137+
};
138+
if (beforeData && afterData) {
139+
// change
140+
addHistory.type = TranslationHistoryType.UPDATE;
141+
addHistory.key = afterData.name;
142+
} else if (beforeData) {
143+
// delete
144+
addHistory.type = TranslationHistoryType.DELETE;
145+
addHistory.key = beforeData.name;
146+
} else if (afterData) {
147+
// create
148+
addHistory.type = TranslationHistoryType.CREATE;
149+
addHistory.key = afterData.name;
150+
}
151+
await findTranslationsHistory(spaceId).add(addHistory);
152+
return;
153+
});
154+
124155
export const translation = {
125156
publish: translationsPublish,
126157
oncreate: onTranslationCreate,
158+
onwrite: onTranslationWrite,
127159
};

src/app/features/translations/translations.component.html

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@
331331
<ul role="list" class="-mb-8">
332332
@for (item of history$ | async; track item.id; let isLast = $last) {
333333
<li>
334-
<div class="relative pb-8">
334+
<div class="relative pb-5">
335335
@if (!isLast) {
336336
<span class="absolute left-4 top-4 -ml-px h-full w-0.5 bg-gray-200" aria-hidden="true"></span>
337337
}
@@ -353,7 +353,58 @@
353353
</p>
354354
</div>
355355
<div class="whitespace-nowrap text-right text-sm text-gray-500">
356-
<time>{{ item.createdAt?.toDate() | date: 'MMM dd, HH:mm' }}</time>
356+
<time>{{ item.createdAt.toDate() | date: 'MMM dd, HH:mm' }}</time>
357+
</div>
358+
</div>
359+
}
360+
@case ('CREATE') {
361+
<div>
362+
<span class="h-8 w-8 rounded-full bg-green-500 flex items-center justify-center ring-8 ring-white text-white">
363+
<mat-icon>add</mat-icon>
364+
</span>
365+
</div>
366+
<div class="flex min-w-0 flex-1 justify-between space-x-4 pt-1.5">
367+
<div>
368+
<p class="text-sm text-gray-500">
369+
Add <span class="font-medium text-gray-900 break-all">{{ item.key }}</span>
370+
</p>
371+
</div>
372+
<div class="whitespace-nowrap text-right text-sm text-gray-500">
373+
<time>{{ item.createdAt.toDate() | date: 'MMM dd, HH:mm' }}</time>
374+
</div>
375+
</div>
376+
}
377+
@case ('UPDATE') {
378+
<div>
379+
<span class="h-8 w-8 rounded-full bg-green-500 flex items-center justify-center ring-8 ring-white text-white">
380+
<mat-icon>edit</mat-icon>
381+
</span>
382+
</div>
383+
<div class="flex min-w-0 flex-1 justify-between space-x-4 pt-1.5">
384+
<div>
385+
<p class="text-sm text-gray-500">
386+
Edit <span class="font-medium text-gray-900 break-all">{{ item.key }}</span>
387+
</p>
388+
</div>
389+
<div class="whitespace-nowrap text-right text-sm text-gray-500">
390+
<time>{{ item.createdAt.toDate() | date: 'MMM dd, HH:mm' }}</time>
391+
</div>
392+
</div>
393+
}
394+
@case ('DELETE') {
395+
<div>
396+
<span class="h-8 w-8 rounded-full bg-green-500 flex items-center justify-center ring-8 ring-white text-white">
397+
<mat-icon>delete</mat-icon>
398+
</span>
399+
</div>
400+
<div class="flex min-w-0 flex-1 justify-between space-x-4 pt-1.5">
401+
<div>
402+
<p class="text-sm text-gray-500">
403+
Delete <span class="font-medium text-gray-900 break-all">{{ item.key }}</span>
404+
</p>
405+
</div>
406+
<div class="whitespace-nowrap text-right text-sm text-gray-500">
407+
<time>{{ item.createdAt.toDate() | date: 'MMM dd, HH:mm' }}</time>
357408
</div>
358409
</div>
359410
}
@@ -368,7 +419,7 @@
368419
<p class="text-sm text-gray-500">Unknown</p>
369420
</div>
370421
<div class="whitespace-nowrap text-right text-sm text-gray-500">
371-
<time datetime="2020-09-20">{{ item.createdAt?.toDate() | date: 'MMM dd, HH:mm' }}</time>
422+
<time datetime="2020-09-20">{{ item.createdAt.toDate() | date: 'MMM dd, HH:mm' }}</time>
372423
</div>
373424
</div>
374425
}

src/app/features/translations/translations.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export class TranslationsComponent implements OnInit {
115115
});
116116
this.space$ = this.spaceService.findById(this.spaceId).pipe(
117117
tap(space => {
118+
this.selectedSpace = space;
118119
//this.locales = space.locales;
119120
if (this.selectedSearchLocale === '') {
120121
this.selectedSearchLocale = space.localeFallback.id;

src/app/shared/models/translation-history.model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import { Timestamp } from '@angular/fire/firestore';
22

33
export enum TranslationHistoryType {
44
PUBLISHED = 'PUBLISHED',
5+
CREATE = 'CREATE',
6+
UPDATE = 'UPDATE',
7+
DELETE = 'DELETE',
58
}
69

710
export interface TranslationHistory {
811
id: string;
912
type: TranslationHistoryType;
1013
description?: string;
14+
key?: string;
1115
name?: string;
1216
email?: string;
1317
createdAt: Timestamp;

0 commit comments

Comments
 (0)