Start work with LocalSorage and SessionStorage(in future)

This commit is contained in:
Artur 2024-11-04 03:49:24 +01:00
parent faa87921e2
commit aaa358d3b7
5 changed files with 1293 additions and 43 deletions

4
.vscode/launch.json vendored
View File

@ -7,7 +7,9 @@
"name": "ng serve",
"type": "chrome",
"request": "launch",
"url": "http://localhost:4200/"
"preLaunchTask": "npm: start",
"url": "http://localhost:4201/",
"skipFiles": ["chrome-error://*"]
},
{
"name": "ng test",

View File

@ -62,7 +62,7 @@
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"port": 4200,
"port": 4201,
"host": "0.0.0.0"
},
"configurations": {

1290
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -34,6 +34,7 @@
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.1.0",
"typescript": "~5.5.2"
"typescript": "~5.5.2",
"vite": "^5.4.10"
}
}

View File

@ -40,33 +40,42 @@ interface FormValues {
constructor(private assetService : AssetService ){
effect( () => { this.calculate(); });
this.assetsDepreciationFormGroup.valueChanges.subscribe( (value) => this.calculate() );
this.lifeFormArray.valueChanges.subscribe( (value) => this.calculate() );
const savedAsset = localStorage.getItem('assetForCalculator');
if( savedAsset ) {
const asset = JSON.parse( savedAsset );
this.controlsFromAsset( asset );
}
}
ngOnInit(): void{
// const savedAsset = sessionStorage.getItem('assetForCalculator');
// if( savedAsset ) {
// const asset = JSON.parse( savedAsset ) ;
// this.controlsFromAsset( asset );
// }
this.assetsDepreciationFormGroup.valueChanges.subscribe( (value) => this.calculate() );
this.lifeFormArray.valueChanges.subscribe( (value) => this.calculate() );
}
ngOnDestroy(): void {
// Zapisywanie danych przed zniszczeniem komponentu
// const asset = this.controlsToAsset();
// sessionStorage.setItem('assetForCalculator', JSON.stringify(asset));
}
private controlsFromAsset( asset : Asset ) {
if( asset.life.length > 0){
const year_month = asset.life[0].when.year +'-'+asset.life[0].when.month;
this.assetsDepreciationFormGroup.patchValue({
initialValueAsset: asset.life[0].initial
})
year_month : year_month,
initialValueAsset : asset.life[0].initial,
depreciationRate : asset.depreciationRate,
typeDepreciation : asset.type,
factorValue : asset.factorValue,
})
for( let i = 1; i < asset.life.length; i++ ){
this.addChangeValue();
// /this.lifeFormArray.controls[ i -1 ]..patchValue( asset.life[i].initial );
}
}
}
private controlsToAsset() : Asset {
@ -74,7 +83,7 @@ interface FormValues {
const when = new YearMonth( formValues.year_month );
const asset = new Asset( formValues.depreciationRate, when, formValues.typeDepreciation, formValues.factorValue );
const creationlifeChange = new AssetLifeChange(when, formValues.initialValueAsset , 0, 0);
asset.addChange(creationlifeChange);
asset.addChange( creationlifeChange );
for (let changeControlGroup of this.lifeFormArray.controls) {
const initialValueAsset = changeControlGroup.get('initialValueAsset')?.value;
const year_month = changeControlGroup.get('year_month')?.value;
@ -86,6 +95,8 @@ interface FormValues {
calculate(){
const asset = this.controlsToAsset();
this.calculateForAsset( asset );
localStorage.setItem('assetForCalculator', JSON.stringify(asset));
}
calculateForAsset( asset : Asset ) {