forked from Lab-Lab-Lab/CPR-Music
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivityInstructions.jsx
More file actions
145 lines (134 loc) · 4.83 KB
/
ActivityInstructions.jsx
File metadata and controls
145 lines (134 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* ActivityInstructions
*
* Renders activity instructions with conditionally unlocked questions
*/
import { Card, Form, Alert } from 'react-bootstrap';
import { FaCheckCircle, FaLock } from 'react-icons/fa';
import { useState, useRef, useCallback } from 'react';
export default function ActivityInstructions({
step,
instructions,
questions = [],
questionResponses = {},
completedOperations = [],
onResponseChange,
}) {
const [localResponses, setLocalResponses] = useState(questionResponses);
const debounceTimers = useRef({});
const handleChange = useCallback((questionId, value) => {
setLocalResponses((prev) => ({
...prev,
[questionId]: value,
}));
// Debounce the API save to avoid excessive requests
if (onResponseChange) {
clearTimeout(debounceTimers.current[questionId]);
debounceTimers.current[questionId] = setTimeout(() => {
onResponseChange(questionId, value);
}, 500);
}
}, [onResponseChange]);
const isQuestionUnlocked = (question) => {
if (!question.requiredOperation) return true;
return completedOperations.includes(question.requiredOperation);
};
return (
<Card className="mb-3">
<Card.Header className="bg-primary text-white">
<h4 className="mb-0">Activity {step} Instructions</h4>
</Card.Header>
<Card.Body>
{/* Render instructions */}
<div className="instructions-content">
{instructions.map((instruction, idx) => {
if (instruction.type === 'text') {
return (
<div key={idx} className="mb-3">
{instruction.content}
</div>
);
}
if (instruction.type === 'list') {
return (
<div key={idx} className="mb-3">
{instruction.title && (
<h6 className="fw-bold">{instruction.title}</h6>
)}
<ul className="mb-0">
{instruction.items.map((item, itemIdx) => (
<li key={itemIdx}>{item}</li>
))}
</ul>
</div>
);
}
if (instruction.type === 'alert') {
return (
<Alert key={idx} variant={instruction.variant || 'info'}>
{instruction.content}
</Alert>
);
}
return null;
})}
</div>
{/* Render questions */}
{questions.length > 0 && (
<div className="questions-section mt-4">
<h5 className="mb-3">Reflection Questions</h5>
{questions.map((question) => {
const unlocked = isQuestionUnlocked(question);
const hasResponse = localResponses[question.id]?.trim().length > 0;
return (
<div
key={question.id}
className={`question-item mb-3 p-3 border rounded ${
!unlocked ? 'bg-light' : ''
}`}
>
<div className="d-flex align-items-start gap-2 mb-2">
{unlocked ? (
hasResponse ? (
<FaCheckCircle className="text-success mt-1" />
) : (
<span className="text-muted mt-1">○</span>
)
) : (
<FaLock className="text-muted mt-1" />
)}
<div className="flex-grow-1">
<label className="fw-bold mb-2">
{question.question}
</label>
{!unlocked && (
<div className="text-muted small mb-2">
<em>
This question will unlock after you complete:{' '}
{question.unlockHint || 'the required operation'}
</em>
</div>
)}
<Form.Control
as={question.type === 'textarea' ? 'textarea' : 'input'}
rows={question.type === 'textarea' ? 3 : undefined}
placeholder={
unlocked
? 'Type your response here...'
: 'Complete the required operations to unlock'
}
disabled={!unlocked}
value={localResponses[question.id] || ''}
onChange={(e) => handleChange(question.id, e.target.value)}
/>
</div>
</div>
</div>
);
})}
</div>
)}
</Card.Body>
</Card>
);
}