Skip to content

Commit

Permalink
feat: Add user selection persistence
Browse files Browse the repository at this point in the history
This commit adds functionality to persist user language selection in the application. The `loadUserSelection` method retrieves the selected language and API provider from local storage and sets them as the default values in the form. The `saveUserSelection` method saves the selected language and API provider to local storage whenever the form is submitted.
  • Loading branch information
EdiWang committed Jun 18, 2024
1 parent 854c424 commit 871acb0
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion web/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class AppComponent implements OnInit {
ngOnInit(): void {
this.buildForm();
this.loadTranslations();
this.loadUserSelection();
}

buildForm() {
Expand All @@ -70,6 +71,8 @@ export class AppComponent implements OnInit {
this.isBusy = true;
this.errorMessage = '';

this.saveUserSelection();

this.azureTranslatorProxyService.translate(
{
Content: this.sourceForm?.controls['sourceText']?.value,
Expand Down Expand Up @@ -102,6 +105,24 @@ export class AppComponent implements OnInit {
);
}

saveUserSelection() {
localStorage.setItem('sourceLanguage', this.sourceForm.controls['sourceLanguage'].value);
localStorage.setItem('targetLanguage', this.sourceForm.controls['targetLanguage'].value);
localStorage.setItem('apiProvider', this.sourceForm.controls['apiProvider'].value);
}

loadUserSelection() {
const sourceLanguage = localStorage.getItem('sourceLanguage');
const targetLanguage = localStorage.getItem('targetLanguage');
const apiProvider = localStorage.getItem('apiProvider');

if (sourceLanguage && targetLanguage) {
this.sourceForm.controls['sourceLanguage'].setValue(sourceLanguage);
this.sourceForm.controls['targetLanguage'].setValue(targetLanguage);
this.sourceForm.controls['apiProvider'].setValue(apiProvider);
}
}

swapLanguageSelector() {
const sourceLanguage = this.sourceForm.controls['sourceLanguage'].value;
const targetLanguage = this.sourceForm.controls['targetLanguage'].value;
Expand Down Expand Up @@ -151,7 +172,7 @@ export class AppComponent implements OnInit {
this.sourceForm.controls['targetLanguage'].setValue(translation.TargetLanguage.Code);
this.sourceForm.controls['sourceText'].setValue(translation.SourceText);
this.sourceForm.controls['apiProvider'].setValue(translation.Provider.Code);

this.translatedText = translation.TranslatedText;
}
}

0 comments on commit 871acb0

Please sign in to comment.