Skip to content

Commit

Permalink
🐛 fix: able to unset panic-button (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
casperiv0 authored Jan 1, 2022
1 parent fce1c6c commit b660b8b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
50 changes: 40 additions & 10 deletions packages/api/src/controllers/leo/LeoController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,50 @@ export class LeoController {
},
});

let type: "ON" | "OFF" = "ON";
if (code) {
fullOfficer = await prisma.officer.update({
where: {
id: officer.id,
},
data: {
statusId: code.id,
},
include: unitProperties,
});
/**
* officer is already in panic-mode -> set status back to `ON_DUTY`
*/
if (fullOfficer?.statusId === code?.id) {
const onDutyCode = await prisma.statusValue.findFirst({
where: {
shouldDo: ShouldDoType.SET_ON_DUTY,
},
});

if (!onDutyCode) {
throw new BadRequest("mustHaveOnDutyCode");
}

type = "OFF";
fullOfficer = await prisma.officer.update({
where: {
id: officer.id,
},
data: {
statusId: onDutyCode?.id,
},
include: unitProperties,
});
} else {
/**
* officer is not yet in panic-mode -> set status to panic button status
*/
fullOfficer = await prisma.officer.update({
where: {
id: officer.id,
},
data: {
statusId: code.id,
},
include: unitProperties,
});
}
}

this.socket.emitUpdateOfficerStatus();
this.socket.emitPanicButtonLeo(fullOfficer);
this.socket.emitPanicButtonLeo(fullOfficer, type);
}

@Get("/impounded-vehicles")
Expand Down
8 changes: 6 additions & 2 deletions packages/api/src/services/SocketService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ export class Socket {
this.io.sockets.emit(SocketEvents.Signal100, value);
}

emitPanicButtonLeo(officer: any) {
this.io.sockets.emit(SocketEvents.PANIC_BUTTON, officer);
emitPanicButtonLeo(officer: any, type?: "ON" | "OFF") {
if (type === "OFF") {
this.io.sockets.emit(SocketEvents.PANIC_BUTTON_OFF, officer);
} else {
this.io.sockets.emit(SocketEvents.PANIC_BUTTON_ON, officer);
}
}
}
6 changes: 5 additions & 1 deletion packages/client/src/hooks/usePanicButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import { makeUnitName } from "lib/utils";
export function usePanicButton() {
const [unit, setUnit] = React.useState<FullDeputy | FullOfficer | null>(null);

useListener(SocketEvents.PANIC_BUTTON, (officer: FullOfficer) => {
useListener(SocketEvents.PANIC_BUTTON_ON, (officer: FullOfficer) => {
setUnit(officer);
});

useListener(SocketEvents.PANIC_BUTTON_OFF, () => {
setUnit(null);
});

return { unit, PanicButton: Component };
}

Expand Down
4 changes: 3 additions & 1 deletion packages/config/src/socket-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export enum SocketEvents {
DeleteCallEvent = "DELETE_CALL_EVENT",

Signal100 = "SIGNAL_100",
PANIC_BUTTON = "PANIC_BUTTON",

PANIC_BUTTON_ON = "PANIC_BUTTON_ON",
PANIC_BUTTON_OFF = "PANIC_BUTTON_OFF",

RoleplayStopped = "ROLEPLAY_STOPPED",
}

0 comments on commit b660b8b

Please sign in to comment.