Skip to content

Commit

Permalink
Self-help changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sarithapillai8 committed Dec 3, 2024
1 parent 791c195 commit 6df1bef
Show file tree
Hide file tree
Showing 21 changed files with 1,859 additions and 235 deletions.
108 changes: 52 additions & 56 deletions src/components/ActivityCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,48 @@ export default function ActivityCard({
const [helpAnchor, setHelpAnchor] = useState<Element>()
const [showGrid, setShowGrid] = useState<boolean>(forceDefaultGrid || Boolean(freeText.length))
const { t } = useTranslation()

const selectedActivity = activity
events.sort((a, b) => a.timestamp - b.timestamp)
let each = Object.values(
events
.map((d) =>
d.temporal_slices
.filter((t) => t.type != "manual_exit")
.map((t, index) => ({
item:
activity.spec === "lamp.symbol_digit_substitution"
? d.temporal_slices.length > index + 1
? "Digit " + (index + 1) + " : " + t.type
: t.type
: activity.spec === "lamp.maze_game"
? t.level
: t.item,
[new Date(d.timestamp).toLocaleString("en-US", Date.formatStyle("medium"))]:
activity.spec === "lamp.maze_game"
? t.duration
: activity.spec === "lamp.survey" ||
activity.spec === "lamp.pop_the_bubbles" ||
activity.spec === "lamp.symbol_digit_substitution"
? typeof t.value === "string" && t.value !== null
? typeof t.value === "string" && ["Yes", "True"].includes(t.value.replace(/\"/g, ""))
? 1
: typeof t.value === "string" && ["No", "False"].includes(t.value.replace(/\"/g, ""))
? 0
: !isNaN(Number(t.value.replace(/\"/g, "")))
? Number(t.value.replace(/\"/g, ""))
: t.value
d.temporal_slices.map((t, index) => ({
item:
activity.spec === "lamp.symbol_digit_substitution"
? d.temporal_slices.length > index + 1
? "Digit " + (index + 1) + " : " + t.type
: t.type
: activity.spec === "lamp.maze_game"
? t.level
: t.item,
[new Date(d.timestamp).toLocaleString("en-US", Date.formatStyle("medium"))]:
activity.spec === "lamp.maze_game"
? t.duration
: activity.spec === "lamp.survey" ||
activity.spec === "lamp.pop_the_bubbles" ||
activity.spec === "lamp.symbol_digit_substitution"
? typeof t.value === "string" && t.value !== null
? typeof t.value === "string" && ["Yes", "True"].includes(t.value.replace(/\"/g, ""))
? 1
: typeof t.value === "string" && ["No", "False"].includes(t.value.replace(/\"/g, ""))
? 0
: !isNaN(Number(t.value.replace(/\"/g, "")))
? Number(t.value.replace(/\"/g, ""))
: t.value
: activity.spec === "lamp.spin_wheel" || activity.spec === "lamp.emotion_recognition"
? t.type
: !!t.type
? 1
: 0,
}))
: t.value
: activity.spec === "lamp.spin_wheel" || activity.spec === "lamp.emotion_recognition"
? t.type
: !!t.type
? 1
: 0,
}))
)
.reduce((x, y) => x.concat(y), [])
.groupBy("item")
)
.map((v: any) => Object.assign({}, ...v))
.reduce((x, y) => x.concat(y), [])
console.log(each)
let eachData = []
each = each.map((d, key) => {
let keys = Object.keys(d)
Expand Down Expand Up @@ -192,29 +190,27 @@ export default function ActivityCard({
value={Object.values(
events
.map((d) =>
d.temporal_slices
.filter((t) => t.type != "manual_exit")
.map((t, index) => ({
item: activity.spec === "lamp.maze_game" ? t.level : t.item,
[new Date(d.timestamp).toLocaleString("en-US", Date.formatStyle("medium"))]:
activity.spec === "lamp.maze_game"
? t.duration
: activity.spec === "lamp.survey" || activity.spec === "lamp.pop_the_bubbles"
? typeof t.value === "string" && t.value !== null
? typeof t.value === "string" && ["Yes", "True"].includes(t.value.replace(/\"/g, ""))
? 1
: typeof t.value === "string" && ["No", "False"].includes(t.value.replace(/\"/g, ""))
? 0
: !isNaN(Number(t.value.replace(/\"/g, "")))
? Number(t.value.replace(/\"/g, ""))
: t.value
d.temporal_slices.map((t, index) => ({
item: activity.spec === "lamp.maze_game" ? t.level : t.item,
[new Date(d.timestamp).toLocaleString("en-US", Date.formatStyle("medium"))]:
activity.spec === "lamp.maze_game"
? t.duration
: activity.spec === "lamp.survey" || activity.spec === "lamp.pop_the_bubbles"
? typeof t.value === "string" && t.value !== null
? typeof t.value === "string" && ["Yes", "True"].includes(t.value.replace(/\"/g, ""))
? 1
: typeof t.value === "string" && ["No", "False"].includes(t.value.replace(/\"/g, ""))
? 0
: !isNaN(Number(t.value.replace(/\"/g, "")))
? Number(t.value.replace(/\"/g, ""))
: t.value
: activity.spec === "lamp.spin_wheel" || activity.spec === "lamp.emotion_recognition"
? t.type
: !!t.type
? 1
: 0,
}))
: t.value
: activity.spec === "lamp.spin_wheel" || activity.spec === "lamp.emotion_recognition"
? t.type
: !!t.type
? 1
: 0,
}))
)
.reduce((x, y) => x.concat(y), [])
.groupBy("item")
Expand All @@ -239,7 +235,7 @@ export default function ActivityCard({
activity.spec === "lamp.spin_wheel" ||
activity.spec === "lamp.pop_the_bubbles" ||
activity.spec === "lamp.maze_game"
? d.temporal_slices.filter((t) => t.type != "manual_exit")
? d.temporal_slices
: activity.spec === "lamp.scratch_image" ||
activity.spec === "lamp.breathe" ||
activity.spec === "lamp.tips"
Expand All @@ -249,7 +245,7 @@ export default function ActivityCard({
undefined
)
: 0,
slice: d.temporal_slices.filter((t) => t.type != "manual_exit"),
slice: d.temporal_slices,
missing:
activity.spec === "lamp.survey" ||
activity.spec === "lamp.pop_the_bubbles" ||
Expand Down
11 changes: 10 additions & 1 deletion src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import ImportActivity from "./Researcher/ActivityList/ImportActivity"
import PreventPage from "./PreventPage"
import { sensorEventUpdate } from "./BottomMenu"
import TwoFA from "./TwoFA"
import demo_db from "../demo_db.json"
import self_help_db from "../self_help_db.json"

function ErrorFallback({ error }) {
const [trace, setTrace] = useState([])
Expand Down Expand Up @@ -129,6 +131,11 @@ function AppRouter({ ...props }) {
const serverAddressFro2FA = ["api-staging.lamp.digital", "api.lamp.digital"]

useEffect(() => {
if (localStorage.getItem("demo_mode") === "try_it") {
LAMP.initializeDemoDB(demo_db)
} else if (localStorage.getItem("demo_mode") === "self_help") {
LAMP.initializeDemoDB(self_help_db)
}
let query = window.location.hash.split("?")
if (!!query && query.length > 1) {
let src = Object.fromEntries(new URLSearchParams(query[1]))["src"]
Expand Down Expand Up @@ -276,7 +283,9 @@ function AppRouter({ ...props }) {
}, [state])

let reset = async (identity?: any) => {
Service.deleteUserDB()
if (identity?.id != "[email protected]") {
Service.deleteUserDB()
}
Service.deleteDB()
if (typeof identity === "undefined" && LAMP.Auth._type === "participant") {
await sensorEventUpdate(null, (state.identity as any)?.id ?? null, null)
Expand Down
23 changes: 23 additions & 0 deletions src/components/DBService/DBService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ class DBService {
})
}

getActivityEventData(tablespace: any, key: string) {
return userDbPromise
.then(async (db) => {
return (await db.transaction([tablespace], "readonly").objectStore(tablespace).getAll()).filter(
(event) => event.activity === key
)
})
.catch((error) => {
// Do something?
})
}

addData(tablespace: any, data: any) {
return dbPromise
.then((db) => {
Expand All @@ -189,6 +201,17 @@ class DBService {
})
}

addUserDBRow(tablespace: any, data: any) {
return userDbPromise
.then((db) => {
let store = db.transaction(tablespace, "readwrite").objectStore(tablespace)
store.put(data)
})
.catch((error) => {
// Do something?
})
}

addRow(tablespace: any, data: any) {
return dbPromise
.then((db) => {
Expand Down
15 changes: 15 additions & 0 deletions src/components/DBService/UserDB.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ interface LampUSERDB extends DBSchema {
}
indexes: { id: string }
}
activityEvents: {
key: string
value: {
activity: string
timestamp: string
duration: any
static_data: object
temporal_slices: []
}
indexes: { id: number }
}
}

export const userDbPromise = idb.openDB<LampUSERDB>(DATABASE_NAME, 1, {
Expand All @@ -21,5 +32,9 @@ export const userDbPromise = idb.openDB<LampUSERDB>(DATABASE_NAME, 1, {
const user = lampUserDb.createObjectStore("activitytags", { keyPath: "id" })
user.createIndex("id", "id", { unique: true })
}
if (!lampUserDb.objectStoreNames.contains("activityEvents")) {
const activityEvents = lampUserDb.createObjectStore("activityEvents", { autoIncrement: true })
activityEvents.createIndex("id", "id", { unique: true })
}
},
})
17 changes: 14 additions & 3 deletions src/components/EmbeddedActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useTranslation } from "react-i18next"
import LAMP from "lamp-core"
import { useSnackbar } from "notistack"
import { sensorEventUpdate } from "./BottomMenu"
import { Service } from "./DBService/DBService"
const useStyles = makeStyles((theme: Theme) =>
createStyles({
backdrop: {
Expand Down Expand Up @@ -47,9 +48,7 @@ const demoActivities = {
"lamp.symbol_digit_substitution": "symbol_digit_substitution",
"lamp.gyroscope": "gyroscope",
"lamp.dcog": "d-cog",
"lamp.funny_memory": "funnymemory",
"lamp.trails_b": "dottouch",
"lamp.voice_survey": "speech",
"lamp.simple_memory": "funnymemory",
}

export default function EmbeddedActivity({ participant, activity, name, onComplete, noBack, tab, ...props }) {
Expand Down Expand Up @@ -129,6 +128,11 @@ export default function EmbeddedActivity({ participant, activity, name, onComple
data["timestamp"] = activityTimestamp
data["duration"] = new Date().getTime() - activityTimestamp
setData(data)
if (LAMP.Auth._auth.id === "[email protected]") {
setTimeout(() => {
Service.addUserDBRow("activityEvents", data)
}, 500)
}
setEmbeddedActivity(undefined)
setSettings(null)
} else {
Expand Down Expand Up @@ -198,6 +202,13 @@ export default function EmbeddedActivity({ participant, activity, name, onComple
const exist = localStorage.getItem("first-time-" + (participant?.id ?? participant) + "-" + currentActivity?.id)
try {
setSaved(false)
console.log({
...settings,
activity: currentActivity,
configuration: { language: i18n.language },
autoCorrect: !(exist === "true"),
noBack: noBack,
})
setSettings({
...settings,
activity: currentActivity,
Expand Down
6 changes: 5 additions & 1 deletion src/components/Feed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ import zhLocale from "date-fns/locale/zh-CN"
import esLocale from "date-fns/locale/es"
import enLocale from "date-fns/locale/en-US"
import hiLocale from "date-fns/locale/hi"
import { getSelfHelpAllActivityEvents } from "./Participant"

const userLanguages = ["en-US", "es-ES", "hi-IN", "de-DE", "da-DK", "fr-FR", "ko-KR", "it-IT", "zh-CN"]

Expand Down Expand Up @@ -470,7 +471,10 @@ export default function Feed({
date.setSeconds(0)
let startTime = date.getTime()
let endTime = startTime + 86400000
let activityEvents = await LAMP.ActivityEvent.allByParticipant(participant.id, null, startTime, endTime, null, true)
let activityEvents =
LAMP.Auth._auth.id === "[email protected]"
? await getSelfHelpAllActivityEvents(startTime, endTime)
: await LAMP.ActivityEvent.allByParticipant(participant.id, null, startTime, endTime, null, true)
return activityEvents
}

Expand Down
32 changes: 28 additions & 4 deletions src/components/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import { ReactComponent as Logo } from "../icons/Logo.svg"
import { ReactComponent as Logotext } from "../icons/mindLAMP.svg"
import { useTranslation } from "react-i18next"
import { Autocomplete } from "@mui/material"

import demo_db from "../demo_db.json"
import self_help_db from "../self_help_db.json"
type SuggestedUrlOption = {
label: string
}
Expand Down Expand Up @@ -58,7 +59,13 @@ const useStyles = makeStyles((theme: Theme) =>
buttonNav: {
"& button": { width: 200, "& span": { textTransform: "capitalize", fontSize: 16, fontWeight: "bold" } },
},
linkBlue: { color: "#6083E7", fontWeight: "bold", cursor: "pointer", "&:hover": { textDecoration: "underline" } },
linkBlue: {
color: "#6083E7",
fontWeight: "bold",
cursor: "pointer",
marginRight: "20px",
"&:hover": { textDecoration: "underline" },
},
loginContainer: { height: "90vh", paddingTop: "3%" },
loginInner: { maxWidth: 320 },
loginDisabled: {
Expand Down Expand Up @@ -178,7 +185,9 @@ export default function Login({ setIdentity, lastDomain, onComplete, ...props })
)
;(async () => {
await Service.deleteDB()
await Service.deleteUserDB()
if (mode != "selfHelp") {
await Service.deleteUserDB()
}
})()
setLoginClick(false)
onComplete()
Expand Down Expand Up @@ -375,10 +384,25 @@ export default function Login({ setIdentity, lastDomain, onComplete, ...props })
<Link
underline="none"
className={classes.linkBlue}
onClick={(event) => setTryitMenu(event.currentTarget)}
onClick={(event) => {
LAMP.initializeDemoDB(demo_db)
localStorage.setItem("demo_mode", "try_it")
setTryitMenu(event.currentTarget)
}}
>
{`${t("Try it")}`}
</Link>
<Link
underline="none"
className={classes.linkBlue}
onClick={(event) => {
LAMP.initializeDemoDB(self_help_db)
localStorage.setItem("demo_mode", "self_help")
handleLogin(event, "selfHelp")
}}
>
{`${t("Self Help")}`}
</Link>
<br />
<Link
underline="none"
Expand Down
Loading

0 comments on commit 6df1bef

Please sign in to comment.