⚠️ WIP migrating tmp to patch

This commit is contained in:
pakintada@gmail.com 2024-02-22 16:04:34 +07:00
parent 89ce1f361c
commit fed315367a
13 changed files with 317 additions and 270 deletions

View file

@ -34,7 +34,19 @@
alt="Tao Bin Logo"
/>
</a>
<!-- Redis Status -->
<div class="p-2 rounded-lg border border-double border-black" [ngStyle]="redisStatus == 'Online'?{'background-color':'greenyellow'}:{'background-color':'tomato'}">
<p class="text-center font-bold">{{redisStatus}}</p>
</div>
</div>
<!-- File Change Status -->
<button *ngIf="showDetectChanges">
<h1 class="text-center font-extrabold text-2xl text-red-500 animate-pulse">Detect Changes! Click</h1>
</button>
<div class="flex items-center">
<div class="flex items-center ml-3">
<div class="flex flex-row">
@ -155,3 +167,5 @@
</div>
</div>
</div>
<!--Modal-->

View file

@ -1,10 +1,15 @@
import { Component, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRoute, RouterModule } from '@angular/router';
import { DatePipe, NgFor, NgIf, NgOptimizedImage } from '@angular/common';
import { CommonModule, DatePipe, NgFor, NgIf, NgOptimizedImage } from '@angular/common';
import { GoogleButtonComponent } from 'src/app/shared/googleButton/googleButton.component';
import { UserService } from '../services/user.service';
import { User } from '../models/user.model';
import { Subject, Subscription, map, share, takeUntil, timer } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { environment } from 'src/environments/environment';
import { RecipeService } from '../services/recipe.service';
import { AsyncStorage } from 'src/app/shared/helpers/asyncStorage';
import { MergeComponent } from "../../features/merge/merge.component";
interface MenuItem {
name: string;
@ -13,17 +18,19 @@ interface MenuItem {
}
@Component({
selector: 'app-layout',
templateUrl: './layout.component.html',
standalone: true,
imports: [
RouterModule,
NgFor,
NgIf,
GoogleButtonComponent,
DatePipe,
NgOptimizedImage,
],
selector: 'app-layout',
templateUrl: './layout.component.html',
standalone: true,
imports: [
RouterModule,
NgFor,
NgIf,
GoogleButtonComponent,
DatePipe,
NgOptimizedImage,
CommonModule,
MergeComponent
]
})
export class LayoutComponent implements OnInit, OnDestroy {
current_department = this._router.snapshot.paramMap.get('department')!;
@ -51,12 +58,17 @@ export class LayoutComponent implements OnInit, OnDestroy {
user: User | null = null;
exit$ = new Subject<void>();
redisStatus:string = "Offline";
showDetectChanges: boolean = false;
constructor(
private _userService: UserService,
private _router: ActivatedRoute
private _router: ActivatedRoute,
private _httpClient: HttpClient,
private _recipeService: RecipeService
) {}
ngOnInit(): void {
async ngOnInit(): Promise<void> {
this._userService.currentUser
.pipe(takeUntil(this.exit$))
.subscribe((user) => (this.user = user));
@ -69,6 +81,32 @@ export class LayoutComponent implements OnInit, OnDestroy {
.subscribe((time) => {
this.date = time;
});
this._httpClient.get(environment.api + "/health/redis").subscribe((status) => {
this.redisStatus = (status as any)["status"];
});
// check if saves existed
this._recipeService.getSavedTmp(
await this._recipeService.getCurrentCountry(),
this._recipeService.getCurrentFile()
).subscribe({
next: async (data: any) => {
if(data != undefined && typeof data === 'object'){
// check if attr exists
if(data.files != null){
this.showDetectChanges = true;
await AsyncStorage.setItem("detectChanges", "true");
} else {
this.showDetectChanges = false;
await AsyncStorage.setItem("detectChanges", "false");
}
} else {
this.showDetectChanges = false;
await AsyncStorage.setItem("detectChanges", "false");
}
}
});
}
ngOnDestroy() {

View file

@ -47,7 +47,10 @@ export class RecipeService {
return this.tmp_files;
}
constructor(private _httpClient: HttpClient, private _route: ActivatedRoute) {}
constructor(
private _httpClient: HttpClient,
private _route: ActivatedRoute
) {}
getRecipesDashboard(
params: any = {
@ -95,8 +98,9 @@ export class RecipeService {
);
}
async getRecipeDetail(productCode: string): Promise<Observable<RecipeDetail>> {
async getRecipeDetail(
productCode: string
): Promise<Observable<RecipeDetail>> {
let asyncCountry = await this.getCurrentCountry(this.department!);
console.log('get detail by asyncCountry', asyncCountry);
@ -115,8 +119,7 @@ export class RecipeService {
async getRecipeDetailMat(
productCode: string
): Promise<Observable<{ result: RecipeDetailMat[]; }>> {
): Promise<Observable<{ result: RecipeDetailMat[] }>> {
let asyncCountry = await this.getCurrentCountry(this.department!);
return this._httpClient.get<{ result: RecipeDetailMat[] }>(
@ -133,10 +136,8 @@ export class RecipeService {
}
getCurrentFile(): string {
// TODO: get default from server
const currentRecipeFile = localStorage.getItem('currentRecipeFile');
if (currentRecipeFile) {
return currentRecipeFile;
@ -150,9 +151,7 @@ export class RecipeService {
}
async getCurrentCountry(department?: string): Promise<string> {
if(department){
if (department) {
// translate back to full name
let fullname = getCountryMapSwitcher(department);
@ -167,7 +166,9 @@ export class RecipeService {
// const currentRecipeCountry = localStorage.getItem('currentRecipeCountry');
const currentRecipeCountry = await AsyncStorage.getItem<string>('currentRecipeCountry');
const currentRecipeCountry = await AsyncStorage.getItem<string>(
'currentRecipeCountry'
);
if (currentRecipeCountry) {
return currentRecipeCountry;
}
@ -275,13 +276,37 @@ export class RecipeService {
);
}
async getRawRecipeOfProductCode(country: string, filename: string, productCode: string): Promise<Observable<{}>> {
async getRawRecipeOfProductCode(
country: string,
filename: string,
productCode: string
): Promise<Observable<{}>> {
return this._httpClient.get<{}>(
environment.api + '/recipes/' + country + '/' + filename + '/' + productCode + '/raw_full',
environment.api +
'/recipes/' +
country +
'/' +
filename +
'/' +
productCode +
'/raw_full',
{
withCredentials: true,
responseType: 'json',
}
);
}
async getPatchListOfCurrentFile(
country: string,
filename: string
): Promise<Observable<any>> {
console.log("try get patches", country, filename);
return this._httpClient.get<any>(
environment.api + '/recipes/patch/get/' + country + '/' + filename ,
{ withCredentials: true, responseType: 'json' }
);
}
}