From cf00b5cea4fcf8dafb627d1e617c0c9f442376f6 Mon Sep 17 00:00:00 2001 From: devthejo Date: Sun, 8 Mar 2026 18:26:53 +0100 Subject: [PATCH] fix(dae): on alert modal --- src/containers/DaeSuggestModal/index.js | 28 ++++++++++------------ src/scenes/SendAlertConfirm/useOnSubmit.js | 14 ++++++----- src/utils/dae/subjectSuggestsDefib.js | 15 ++++++------ src/utils/dae/subjectSuggestsDefib.test.js | 25 +++++++++++++++++++ 4 files changed, 54 insertions(+), 28 deletions(-) diff --git a/src/containers/DaeSuggestModal/index.js b/src/containers/DaeSuggestModal/index.js index 5f87480..c18053c 100644 --- a/src/containers/DaeSuggestModal/index.js +++ b/src/containers/DaeSuggestModal/index.js @@ -15,16 +15,18 @@ export default function DaeSuggestModal() { const styles = useMemo( () => ({ - container: { backgroundColor: colors.surface, padding: 20 }, + container: { + backgroundColor: colors.surface, + padding: 20, + marginHorizontal: 16, + borderRadius: 12, + }, title: { fontSize: 20, fontWeight: "bold" }, paragraph: { marginTop: 10, fontSize: 16 }, - actionsRow: { + actionsColumn: { marginTop: 18, - flexDirection: "row", - justifyContent: "space-between", + gap: 10, }, - action: { flex: 1 }, - actionLeft: { marginRight: 12 }, }), [colors.surface], ); @@ -52,18 +54,14 @@ export default function DaeSuggestModal() { > Défibrillateur à proximité - Votre alerte semble concerner un malaise grave / arrêt cardiaque. - Recherchez rapidement un défibrillateur (DAE) près de vous. + En cas d'arrêt cardiaque, un défibrillateur (DAE) à proximité peut + sauver une vie. - - - diff --git a/src/scenes/SendAlertConfirm/useOnSubmit.js b/src/scenes/SendAlertConfirm/useOnSubmit.js index e0d02fb..3c281fd 100644 --- a/src/scenes/SendAlertConfirm/useOnSubmit.js +++ b/src/scenes/SendAlertConfirm/useOnSubmit.js @@ -16,6 +16,7 @@ import { getCurrentLocation } from "~/location"; import useSendAlertSMSToEmergency from "~/hooks/useSendAlertSMSToEmergency"; +import alertsList from "~/misc/alertsList"; import subjectSuggestsDefib from "~/utils/dae/subjectSuggestsDefib"; import { SEND_ALERT_MUTATION } from "./gql"; @@ -84,6 +85,13 @@ async function onSubmit(args, context) { speed, }; + // DAE suggest modal — must run before network call so it works offline. + // Show on red alerts unconditionally, or when cardiac keywords are detected. + const matchingAlert = alertsList.find((a) => a.title === subject); + if (level === "red" || subjectSuggestsDefib(subject, matchingAlert?.desc)) { + defibsActions.setShowDaeSuggestModal(true); + } + const { userId, deviceId } = getSessionState(); const createdAt = new Date().toISOString(); @@ -133,12 +141,6 @@ async function onSubmit(args, context) { alertActions.setNavAlertCur({ alert }); - // Task 9 (DAE v1): keyword detection based on subject only. - // Must be independent of network; we trigger purely from the subject and persist state in store. - if (subjectSuggestsDefib(subject)) { - defibsActions.setShowDaeSuggestModal(true); - } - navigation.navigate("Main", { screen: "AlertCur", params: { diff --git a/src/utils/dae/subjectSuggestsDefib.js b/src/utils/dae/subjectSuggestsDefib.js index 30cf5e5..4170bd4 100644 --- a/src/utils/dae/subjectSuggestsDefib.js +++ b/src/utils/dae/subjectSuggestsDefib.js @@ -59,13 +59,14 @@ const DEFIB_SUGGESTION_REGEXES = [ /\brespire\s+plus\b/, ]; -export function subjectSuggestsDefib(subject) { - const text = normalizeSubjectText(subject); - if (!text) { - return false; - } - - return DEFIB_SUGGESTION_REGEXES.some((re) => re.test(text)); +export function subjectSuggestsDefib(...texts) { + return texts.some((input) => { + const text = normalizeSubjectText(input); + if (!text) { + return false; + } + return DEFIB_SUGGESTION_REGEXES.some((re) => re.test(text)); + }); } export const __private__ = { diff --git a/src/utils/dae/subjectSuggestsDefib.test.js b/src/utils/dae/subjectSuggestsDefib.test.js index 5c6f1d7..93327d9 100644 --- a/src/utils/dae/subjectSuggestsDefib.test.js +++ b/src/utils/dae/subjectSuggestsDefib.test.js @@ -37,4 +37,29 @@ describe("dae/subjectSuggestsDefib", () => { expect(subjectSuggestsDefib("mal au dos")).toBe(false); expect(subjectSuggestsDefib("panne de voiture")).toBe(false); }); + + test("matches when keyword is in second argument (description)", () => { + expect( + subjectSuggestsDefib( + "urgence médicale mortelle", + "crise cardiaque, attaque cérébrale, hémorragie importante, blessure grave", + ), + ).toBe(true); + }); + + test("matches when keyword is only in subject, not description", () => { + expect(subjectSuggestsDefib("arrêt cardiaque", "some other desc")).toBe( + true, + ); + }); + + test("does not match when neither subject nor description has keywords", () => { + expect(subjectSuggestsDefib("agression", "violence physique")).toBe(false); + }); + + test("handles null/undefined in multi-arg", () => { + expect(subjectSuggestsDefib(null, "cardiaque")).toBe(true); + expect(subjectSuggestsDefib("cardiaque", undefined)).toBe(true); + expect(subjectSuggestsDefib(null, undefined)).toBe(false); + }); });