add file select for multiple country

This commit is contained in:
Kenta420 2023-10-20 17:05:24 +07:00
parent e5eee656d5
commit 652ecbbf1f
9 changed files with 294 additions and 47 deletions

View file

@ -12,8 +12,15 @@ interface RecipeParams {
search: string;
}
interface RecipeFiles {
[key: string]: string[];
}
@Injectable({ providedIn: 'root' })
export class RecipeService {
private countries: string[] = [];
private recipeFiles: RecipeFiles = {};
constructor(private _httpClient: HttpClient) {}
getRecipes(
@ -74,10 +81,29 @@ export class RecipeService {
});
}
getRecipeVersions(): Observable<string[]> {
return this._httpClient.get<string[]>(
environment.api + '/recipes/versions',
{ withCredentials: true, responseType: 'json' }
);
getRecipeCountries(): Observable<string[]> {
return this._httpClient
.get<string[]>(environment.api + '/recipes/versions', {
withCredentials: true,
responseType: 'json',
})
.pipe(tap((countries) => (this.countries = countries)));
}
getRecipeFiles(country: string): Observable<string[]> {
return this._httpClient
.get<string[]>(environment.api + '/recipes/versions/' + country, {
withCredentials: true,
responseType: 'json',
})
.pipe(tap((files) => (this.recipeFiles[country] = files)));
}
getRecipeFileCountries(): string[] {
return this.countries;
}
getRecipeFileNames(country: string): string[] {
return this.recipeFiles[country];
}
}

View file

@ -109,6 +109,10 @@ export class RecipeDetailsComponent implements OnInit {
(recipe) => recipe.materialPathId
);
if (originalRecipeDetail.recipe.recipes != null) {
return;
}
this._materialService.getMaterialCodes(ids).subscribe((data) => {
this.originalRecipeDetail.next({
...originalRecipeDetail,

View file

@ -5,7 +5,7 @@
<table *ngIf="isLoaded" class="table">
<caption class="p-5 text-lg font-semibold text-left text-gray-900">
<div class="divide-y divide-solid divide-gray-400">
<div class="flex flex-row py-3 justify-between">
<div class="flex flex-row py-3 justify-between items-center">
<div class="flex flex-col">
<span
>Recipe Version {{ recipes?.MachineSetting?.configNumber }} |
@ -13,7 +13,73 @@
>
</div>
<div class="flex flex-col ml-5">
<div class="dropdown dropdown-end">
<button
class="btn bg-primary btn-md"
onclick="select_file_modal.showModal()"
>
<span class="text-base text-gray-700">เลือก Recipe ไฟล์</span>
</button>
<dialog id="select_file_modal" class="modal">
<div
class="modal-box max-w-[600px] overflow-visible flex flex-col justify-center items-center gap-5"
>
<h3 class="font-bold text-lg">เลือก Recipe ไฟล์</h3>
<div class="flex flex-row gap-5">
<div class="dropdown dropdown-end">
<input
type="text"
tabindex="0"
placeholder="เลือก Recipe File"
class="input input-bordered input-sm w-full max-w-xs"
(input)="setRecipeVersion($event)"
(focus)="getRecipeVersions()"
/>
<div
class="dropdown-content z-[1000] min-w-[200px] max-h-[500px] overflow-y-auto"
>
<ul
tabindex="0"
class="menu p-2 shadow bg-base-100 rounded-box w-auto"
>
<li *ngFor="let recipeVersion of recipeVersions">
<a (click)="loadRecipe(recipeVersion)">{{
recipeVersion
}}</a>
</li>
</ul>
</div>
</div>
<div class="dropdown dropdown-end">
<input
type="text"
tabindex="0"
placeholder="เลือก Recipe File"
class="input input-bordered input-sm w-full max-w-xs"
(input)="setRecipeVersion($event)"
(focus)="getRecipeVersions()"
/>
<div
class="dropdown-content z-[1000] min-w-[200px] max-h-[500px] overflow-y-auto"
>
<ul
tabindex="0"
class="menu p-2 shadow bg-base-100 rounded-box w-auto"
>
<li *ngFor="let recipeVersion of recipeVersions">
<a (click)="loadRecipe(recipeVersion)">{{
recipeVersion
}}</a>
</li>
</ul>
</div>
</div>
</div>
</div>
<form method="dialog" class="modal-backdrop">
<button>close</button>
</form>
</dialog>
<!-- <div class="dropdown dropdown-end">
<input
type="text"
tabindex="0"
@ -34,7 +100,7 @@
</li>
</ul>
</div>
</div>
</div> -->
</div>
<div class="flex flex-col ml-auto">
<span class=""

View file

@ -10,6 +10,11 @@ import { BehaviorSubject } from 'rxjs';
import * as lodash from 'lodash';
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
interface RecipeFileFilter {
country: string | null;
fileName: string | null;
}
@Component({
selector: 'app-recipes',
standalone: true,
@ -19,7 +24,8 @@ import { ActivatedRoute, Router, RouterLink } from '@angular/router';
export class DashboardComponent implements OnInit {
recipes: Recipe | null = null;
recipes01: Recipe01[] | null = null;
recipeVersions: string[] = [];
recipeFileCountries: string[] = [];
recipeFileNames: string[] = [];
fileName: string = '';
tableHeads: string[] = [
@ -31,10 +37,13 @@ export class DashboardComponent implements OnInit {
];
private offset = 0;
private take = 20;
private recipeVersionData: string[] = [];
recipeVersion: BehaviorSubject<string> = new BehaviorSubject<string>('');
recipeVersion$ = this.recipeVersion.asObservable();
recipeFileFilter: BehaviorSubject<RecipeFileFilter> =
new BehaviorSubject<RecipeFileFilter>({
country: null,
fileName: null,
});
recipeFileFilter$ = this.recipeFileFilter.asObservable();
isLoaded: boolean = false;
isLoadMore: boolean = false;
@ -111,11 +120,16 @@ export class DashboardComponent implements OnInit {
this.isHasMore = hasMore;
});
this.recipeVersion$.subscribe((version) => {
if (version)
this.recipeVersions = lodash.filter(this.recipeVersionData, (v) =>
v.includes(version)
);
this.recipeFileFilter$.subscribe((version) => {
this.recipeFileCountries = lodash.filter(
this._recipeService.getRecipeFileCountries(),
(v) => {
if (version.country) {
return v.includes(version.country);
}
return true;
}
);
});
}
@ -189,9 +203,9 @@ export class DashboardComponent implements OnInit {
getRecipeVersions() {
if (this.recipeVersionData.length > 0) return;
this._recipeService.getRecipeVersions().subscribe((versions) => {
this._recipeService.getRecipeCountries().subscribe((versions) => {
this.recipeVersionData = versions;
this.recipeVersions = versions;
this.recipeFileCountries = versions;
});
}

View file

@ -1,5 +1,4 @@
export const environment = {
production: false,
api: 'http://localhost:8080',
wsapi: 'ws://localhost:8080'
};

@ -1 +1 @@
Subproject commit bb7e2e1d3ce239ac877ed0ecdad934eef4f2513d
Subproject commit 5adec80644b88c732a06331bfa002427bf0a84da

View file

@ -2,11 +2,12 @@ package data
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"recipe-manager/helpers"
"recipe-manager/models"
"sort"
"time"
)
@ -40,44 +41,34 @@ type RecipeWithTimeStamps struct {
type Data struct {
CurrentVersion string
AllVersions []string
AllRecipeFiles map[string][]helpers.RecipePath
currentRecipe *models.Recipe
recipeMap map[string]RecipeWithTimeStamps
Countries []helpers.CountryName
}
func NewData() *Data {
files, err := filepath.Glob("cofffeemachineConfig/coffeethai02_*.json")
if err != nil {
log.Panic("Error when scan recipe files:", err)
countries := []helpers.CountryName{{
CountryID: "thai",
CountryName: "Thailand",
}, {
CountryID: "mys",
CountryName: "Malaysia",
}, {
CountryID: "aus",
CountryName: "Australia",
},
}
sort.Slice(files, func(i, j int) bool {
file1, err := os.Stat(files[i])
if err != nil {
log.Panic("Error when get file info:", err)
}
file2, err := os.Stat(files[j])
if err != nil {
log.Panic("Error when get file info:", err)
}
return file1.ModTime().After(file2.ModTime())
})
for i := 0; i < len(files); i++ {
files[i] = filepath.Base(files[i])
}
allRecipeFiles := helpers.ScanRecipeFiles(countries)
defaultVersion := "coffeethai02_580.json"
defaultRecipe := readFile(defaultVersion)
return &Data{
CurrentVersion: defaultVersion,
AllVersions: files,
AllRecipeFiles: allRecipeFiles,
currentRecipe: defaultRecipe,
recipeMap: map[string]RecipeWithTimeStamps{
defaultVersion: {
@ -85,6 +76,7 @@ func NewData() *Data {
TimeStamps: time.Now().Unix(),
},
},
Countries: countries,
}
}
@ -225,3 +217,21 @@ func (d *Data) GetMaterialCode(ids []uint64, version string) []models.MaterialCo
return resultFilter
}
func (d *Data) GetCountryNameByID(countryID string) (string, error) {
for _, country := range d.Countries {
if country.CountryID == countryID {
return country.CountryName, nil
}
}
return "", fmt.Errorf("country ID: %s not found", countryID)
}
func (d *Data) GetCountryIDByName(countryName string) (string, error) {
for _, country := range d.Countries {
if country.CountryName == countryName {
return country.CountryID, nil
}
}
return "", fmt.Errorf("country name: %s not found", countryName)
}

View file

@ -0,0 +1,101 @@
package helpers
import (
"encoding/json"
"log"
"os"
"path/filepath"
"recipe-manager/models"
"sort"
)
func ListFile(rootPath string) []string {
files, err := filepath.Glob(rootPath)
if err != nil {
log.Panic("Error when scan recipe files:", err)
}
return files
}
func ReadFile(filePath string) (string, error) {
file, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("Error when open file: %s", err)
return "", err
}
return string(file), nil
}
func ReadRecipeFile(filePath string) models.Recipe {
file, err := os.Open(filePath)
if err != nil {
log.Fatalf("Error when open file: %s", err)
return models.Recipe{}
}
defer file.Close()
var data models.Recipe
err = json.NewDecoder(file).Decode(&data)
if err != nil {
log.Fatalf("Error when decode file: %s", err)
return models.Recipe{}
}
return data
}
type RecipePath struct {
Name string `json:"name"`
Path string `json:"path"`
}
type CountryName struct {
CountryID string `json:"countryId"`
CountryName string `json:"countryName"`
}
func ScanRecipeFiles(countries []CountryName) map[string][]RecipePath {
recipeFiles := map[string][]RecipePath{}
for _, country := range countries {
var files []string
if country.CountryID == "thai" {
files = ListFile("cofffeemachineConfig/coffeethai02_*.json")
} else {
files = ListFile("cofffeemachineConfig/" + country.CountryID + "/coffeethai02_*.json")
}
sort.Slice(files, func(i, j int) bool {
file1, err := os.Stat(files[i])
if err != nil {
log.Panic("Error when get file info:", err)
}
file2, err := os.Stat(files[j])
if err != nil {
log.Panic("Error when get file info:", err)
}
return file1.ModTime().After(file2.ModTime())
})
for i := 0; i < len(files); i++ {
if _, ok := recipeFiles[country.CountryID]; !ok {
recipeFiles[country.CountryID] = []RecipePath{}
}
recipeFiles[country.CountryID] = append(recipeFiles[country.CountryID], RecipePath{
Name: filepath.Base(files[i]),
Path: files[i],
})
}
}
return recipeFiles
}

View file

@ -2,6 +2,7 @@ package routers
import (
"encoding/json"
"fmt"
"net/http"
"recipe-manager/data"
"recipe-manager/models"
@ -123,7 +124,33 @@ func (rr *RecipeRouter) Route(r chi.Router) {
})
r.Get("/versions", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(rr.data.AllVersions)
w.Header().Add("Content-Type", "application/json")
// get key from map
keys := []string{}
for k := range rr.data.AllRecipeFiles {
countryName, err := rr.data.GetCountryNameByID(k)
if err != nil {
continue
}
keys = append(keys, countryName)
}
json.NewEncoder(w).Encode(keys)
})
r.Get("/versions/{country}", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
countryName := chi.URLParam(r, "country")
countryID, err := rr.data.GetCountryIDByName(countryName)
if err != nil {
http.Error(w, fmt.Sprintf("Country Name: %s not found!!!", countryName), http.StatusNotFound)
return
}
files := []string{}
for _, v := range rr.data.AllRecipeFiles[countryID] {
files = append(files, v.Name)
}
json.NewEncoder(w).Encode(files)
})
r.Get("/test/sheet", func(w http.ResponseWriter, r *http.Request) {