Skip to content

Commit

Permalink
✨ add instant state update in decoy list
Browse files Browse the repository at this point in the history
  • Loading branch information
TarradeMarc committed Dec 13, 2024
1 parent a220916 commit 54f0bb9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ <h1>Decoys list 🗒️</h1>
</tbody>
</table>
<div class="buttons">
<button class="filled-btn" (click)="save()">Save</button>
<!-- <button class="filled-btn" (click)="save()">Save</button> -->
</div>
</div>
19 changes: 11 additions & 8 deletions controlpanel/cad/src/app/pages/list-decoy/list-decoy.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class ListDecoyComponent implements OnInit, OnDestroy {

ngOnDestroy(): void {
this.globalStateSubscription?.unsubscribe();
this.save();
// this.save();
}

async getDecoyList() {
Expand All @@ -52,9 +52,12 @@ export class ListDecoyComponent implements OnInit, OnDestroy {
if (state == 'active') return true;
else return false;
}
setDeployed(decoyData: DecoyData) {
async setDeployed(decoyData: DecoyData) {
if (decoyData.state == 'active') decoyData.state = 'inactive';
else decoyData.state = 'active';
const updateResponse = await this.decoyService.updateDecoyState(decoyData);
if (updateResponse.type == 'error') this.toastr.error(updateResponse.message, "Error updating state");
else this.toastr.success("Successfully updated decoy state", "Saved");
}
async deleteDecoy(decoyId: UUID) {
const saveResponse = await this.decoyService.deleteDecoy(decoyId);
Expand All @@ -65,10 +68,10 @@ export class ListDecoyComponent implements OnInit, OnDestroy {
}
}

async save() {
if (!this.decoys.length) return;
const saveResponse = await this.decoyService.updateDecoysState(this.decoys);
if (saveResponse.type == 'error') this.toastr.error(saveResponse.message, "Error saving");
else this.toastr.success("Successfully saved decoys states", "Saved");
}
// async save() {
// if (!this.decoys.length) return;
// const saveResponse = await this.decoyService.updateDecoysState(this.decoys);
// if (saveResponse.type == 'error') this.toastr.error(saveResponse.message, "Error saving");
// else this.toastr.success("Successfully saved decoys states", "Saved");
// }
}
3 changes: 3 additions & 0 deletions controlpanel/cad/src/app/services/api/decoy-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ export class DecoyApiService {
deleteDecoy(id: UUID) {
return this.http.delete<ApiResponse>(`${this.globalState.API_URL}/decoy/${id}`);
}
patchDecoyState(decoy: DecoyData) {
return this.http.patch<ApiResponse>(`${this.globalState.API_URL}/decoy/state`, decoy);
}
}
8 changes: 8 additions & 0 deletions controlpanel/cad/src/app/services/decoy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ export class DecoyService {
return { message: "Error when saving decoy", type: 'error' };
}
}
async updateDecoyState(decoy: DecoyData): Promise<ApiResponse> {
try {
const apiResponse = await lastValueFrom(this.decoyApi.patchDecoyState(decoy));
return apiResponse;
} catch(e) {
return { message: "Error when saving decoy", type: 'error' };
}
}

async getDecoys(): Promise<ApiResponse> {
try {
Expand Down

0 comments on commit 54f0bb9

Please sign in to comment.