Skip to content

Commit

Permalink
Update UserEventResponse to account for more items with additional …
Browse files Browse the repository at this point in the history
…text
  • Loading branch information
sultanofcardio committed Feb 1, 2024
1 parent 647a71d commit d277b40
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 55 deletions.
78 changes: 25 additions & 53 deletions src/entities/applet/model/hooks/useUserEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ export const useUserEvents = (props: Props) => {
return
}

const response = mapItemAnswerToUserEventResponse(item)

if (typeof response !== "object") {
// This should never happen since text items don't have additional text
// but the TS compiler doesn't know that
return
}

const userEvents = activityProgress.userEvents

const activityItemScreenId = getActivityItemScreenId(props.activityId, item.id)
Expand All @@ -108,11 +116,10 @@ export const useUserEvents = (props: Props) => {
if (
previousUserEvent.screen === activityItemScreenId &&
previousUserEvent.type === "SET_ANSWER" &&
typeof previousUserEvent.response !== "string"
typeof previousUserEvent.response === "object" &&
previousUserEvent.response.text !== undefined
) {
// We want to always update the previous event when it is a SET_ANSWER event for
// the same item. However, the data model currently only supports additional text
// on events for singleSelect and multiSelect items
// Update the text of the previous response if it contains text (to prevent incremental events)
return dispatch(
actions.updateUserEventByIndex({
entityId: props.activityId,
Expand All @@ -123,10 +130,7 @@ export const useUserEvents = (props: Props) => {
screen: activityItemScreenId,
time: Date.now(),
response: {
// The type doesn't allow setting a null value here, but the flow logic
// prevents proceeding without setting the actual answer so using []
// should be fine
value: previousUserEvent.response?.value ?? [],
value: previousUserEvent.response.value,
text: item.additionalText,
},
},
Expand All @@ -136,51 +140,19 @@ export const useUserEvents = (props: Props) => {
}

// Create a new event in all other cases
if (item.responseType === "singleSelect" || item.responseType === "multiSelect") {
const response = mapItemAnswerToUserEventResponse(item)

if (typeof response !== "object") {
// This should never happen since text items don't have additional text
// but the TS compiler doesn't know that
return
}

return dispatch(
actions.saveUserEvent({
entityId: props.activityId,
eventId: props.eventId,
itemId: item.id,
userEvent: {
type: "SET_ANSWER",
screen: activityItemScreenId,
time: Date.now(),
response: {
value: response.value,
text: item.additionalText,
},
},
}),
)
} else {
// The user event data model for other item types don't yet
// support additional text, so we just save the additional text
return dispatch(
actions.saveUserEvent({
entityId: props.activityId,
eventId: props.eventId,
itemId: item.id,
userEvent: {
type: "SET_ANSWER",
screen: activityItemScreenId,
time: Date.now(),
response: {
value: [],
text: item.additionalText,
},
},
}),
)
}
return dispatch(
actions.saveUserEvent({
entityId: props.activityId,
eventId: props.eventId,
itemId: item.id,
userEvent: {
type: "SET_ANSWER",
screen: activityItemScreenId,
time: Date.now(),
response,
},
}),
)
},
[activityProgress, dispatch, props.activityId, props.eventId],
)
Expand Down
5 changes: 4 additions & 1 deletion src/entities/applet/model/mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export const mapItemAnswerToUserEventResponse = (item: ItemRecord): UserEventRes
}
}

return itemAnswer[0]
return {
value: itemAnswer[0],
text: item.additionalText ?? undefined,
}
}

export function mapItemToRecord(item: ActivityItemDetailsDTO): ItemRecord {
Expand Down
2 changes: 1 addition & 1 deletion src/entities/applet/model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type UserEventTypes = "SET_ANSWER" | "PREV" | "NEXT" | "SKIP" | "DONE"
export type UserEventResponse =
| string
| {
value: number[]
value: string | string[] | number[]
text?: string
}

Expand Down

0 comments on commit d277b40

Please sign in to comment.