Skip to content

Commit 8ba373c

Browse files
authored
Needles exchanged - int fields, backend and frontend changes (#493)
* Handling all of the needles exchanged stuffs. * Changed needles_exchaged to exchanged_for. Also created test and did some minor styling.
1 parent 970ddba commit 8ba373c

File tree

10 files changed

+74
-18
lines changed

10 files changed

+74
-18
lines changed

core/fixtures/sep_data.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
site: 1
66
needles_in: 3
77
needles_out: 4
8+
exchanged_for: 6
89
created_at: '2020-09-22 01:38:39.204290+00:00'
910
- model: core.sepdata
1011
pk: 2
@@ -13,6 +14,7 @@
1314
site: 1
1415
needles_in: 5
1516
needles_out: 5
17+
exchanged_for: 5
1618
created_at: '2020-09-22 01:38:39.204290+00:00'
1719
- model: core.sepdata
1820
pk: 3
@@ -21,4 +23,5 @@
2123
site: 2
2224
needles_in: 1
2325
needles_out: 2
26+
exchanged_for: 10
2427
created_at: '2020-09-22 01:38:39.204290+00:00'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 2.2.13 on 2021-01-22 20:45
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('core', '0012_auto_20201216_0954'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='sepdata',
15+
name='exchanged_for',
16+
field=models.IntegerField(default=None, null=True),
17+
),
18+
]

core/models/sep_data.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ class SepData(models.Model):
88
created_at = models.DateTimeField(default=timezone.now)
99
needles_in = models.IntegerField()
1010
needles_out = models.IntegerField()
11+
exchanged_for = models.IntegerField(default=None, null=True)

core/sep_data/serializers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ class Meta:
1212
"created_at",
1313
"needles_in",
1414
"needles_out",
15+
"exchanged_for",
1516
)

core/tests/sep_data.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def test_create_sep_data_entry(self):
6262
"site": 3,
6363
"needles_in": 5,
6464
"needles_out": 5,
65+
"exchanged_for": 10,
6566
}
6667

6768
response = self.client.post(url, data, format="json", follow=True, **headers)
@@ -82,6 +83,7 @@ def test_post_auth(self):
8283
"site": 3,
8384
"needles_in": 5,
8485
"needles_out": 5,
86+
"exchanged_for": 10,
8587
}
8688

8789
pre_post_entry_count = SepData.objects.count()

frontend/src/components/SepForm.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ const useStyles = makeStyles(theme => ({
4343
needlesOut: {
4444
paddingLeft: theme.spacing(1),
4545
},
46+
needlesExchanged: {
47+
marginTop: "-12% !important",
48+
},
4649
clearButton: {
4750
marginLeft: theme.spacing(1),
4851
},
@@ -282,13 +285,15 @@ const SepForm = ({
282285
initialValues={{
283286
needles_in: "",
284287
needles_out: "",
288+
exchanged_for: "",
285289
visit_date: new Date().toISOString().substring(0, 10),
286290
}}
287291
validationSchema={SEPNeedleSchema}
288292
onSubmit={async (values, { setSubmitting }) => {
289293
const success = await participantStore.createSEP({
290294
needles_in: values.needles_in,
291295
needles_out: values.needles_out,
296+
exchanged_for: values.exchanged_for,
292297
visit_date: values.visit_date,
293298
site: currentSite,
294299
participant: participantId,
@@ -316,18 +321,12 @@ const SepForm = ({
316321
disabled={participantId && currentSite ? false : true}
317322
>
318323
<InputLabel htmlFor="needles_in">Needles In</InputLabel>
319-
<Select
324+
<PrevPointInput
320325
id="needles_in"
321326
name="needles_in"
322327
onChange={handleChange}
323328
value={values.needles_in}
324-
>
325-
{[...Array(20)].map((x, i) => (
326-
<MenuItem key={i + 1} value={i + 1}>
327-
{i + 1}
328-
</MenuItem>
329-
))}
330-
</Select>
329+
/>
331330
</FormControl>
332331
</Grid>
333332
<Grid item xs={6}>
@@ -336,18 +335,28 @@ const SepForm = ({
336335
disabled={participantId && currentSite ? false : true}
337336
>
338337
<InputLabel htmlFor="needles_out">Needles Out</InputLabel>
339-
<Select
338+
<PrevPointInput
340339
id="needles_out"
341340
name="needles_out"
342341
onChange={handleChange}
343342
value={values.needles_out}
344-
>
345-
{[...Array(20)].map((x, i) => (
346-
<MenuItem key={i + 1} value={i + 1}>
347-
{i + 1}
348-
</MenuItem>
349-
))}
350-
</Select>
343+
/>
344+
</FormControl>
345+
</Grid>
346+
<Grid className={classes.needlesExchanged} item xs={12}>
347+
<FormControl
348+
error={errors.exchanged_for && touched.exchanged_for}
349+
disabled={participantId && currentSite ? false : true}
350+
>
351+
<InputLabel htmlFor="exchanged_for">
352+
Needles Exchanged For
353+
</InputLabel>
354+
<PrevPointInput
355+
id="exchanged_for"
356+
name="exchanged_for"
357+
onChange={handleChange}
358+
value={values.exchanged_for}
359+
/>
351360
</FormControl>
352361
</Grid>
353362
</Grid>
@@ -390,10 +399,12 @@ const SepForm = ({
390399
</PrevPointButton>
391400
{(errors.needles_in ||
392401
errors.needles_out ||
402+
errors.exchanged_for ||
393403
errors.visit_date) && (
394404
<PrevPointCopy className={classes.errorMessage}>
395405
{errors.needles_in ||
396406
errors.needles_out ||
407+
errors.exchanged_for ||
397408
errors.visit_date}
398409
</PrevPointCopy>
399410
)}

frontend/src/components/VisitRouter/VisitData.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ const VisitData = ({ visitData }) => {
6767
</PrevPointCopy>
6868
<PrevPointCopy>Needles in: {visitData.needles_in}</PrevPointCopy>
6969
<PrevPointCopy>Needles out: {visitData.needles_out}</PrevPointCopy>
70+
<PrevPointCopy>
71+
Needles exchanged for: {visitData.exchanged_for}
72+
</PrevPointCopy>
7073
</div>
7174
</Grid>
7275
</Grid>

frontend/src/stores/ParticipantStore.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ export class ParticipantStore {
423423
participant,
424424
needles_in,
425425
needles_out,
426+
exchanged_for,
426427
visit_date,
427428
site,
428429
service,
@@ -447,6 +448,7 @@ export class ParticipantStore {
447448
const { ok, data, status } = yield api.createSEP({
448449
needles_in: needles_in,
449450
needles_out: needles_out,
451+
exchanged_for: exchanged_for,
450452
site: site,
451453
service: service,
452454
visit: visitData.id,

frontend/src/validation/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,15 @@ const SEPSearchSchema = Yup.object().shape(
6060

6161
const SEPNeedleErrorMessage = "Please complete all fields"
6262
const SEPNeedleSchema = Yup.object().shape({
63-
needles_in: Yup.number().required(SEPNeedleErrorMessage),
64-
needles_out: Yup.number().required(SEPNeedleErrorMessage),
63+
needles_in: Yup.number()
64+
.min(0)
65+
.required(SEPNeedleErrorMessage),
66+
needles_out: Yup.number()
67+
.min(0)
68+
.required(SEPNeedleErrorMessage),
69+
exchanged_for: Yup.number()
70+
.min(0)
71+
.defined(SEPNeedleErrorMessage),
6572
visit_date: Yup.string().required(SEPNeedleErrorMessage),
6673
})
6774

frontend/test/VisitData.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const mockVisitData = {
1010
id: 1,
1111
needles_in: 1,
1212
needles_out: 2,
13+
exchanged_for: 3,
1314
created_at: new Date(),
1415
site: { site_name: "a-site" },
1516
}
@@ -45,6 +46,13 @@ describe("<VisitData />", () => {
4546
expect(getByText(/needles out: 2/i)).toBeInTheDocument()
4647
})
4748

49+
it("has needles exchanged for value in dom", () => {
50+
const { getByText } = renderWithRouter(
51+
<VisitData visitData={mockVisitData} />
52+
)
53+
expect(getByText(/needles exchanged for: 3/i)).toBeInTheDocument()
54+
})
55+
4856
it("has site value in dom", () => {
4957
const { getByText } = renderWithRouter(
5058
<VisitData visitData={mockVisitData} />

0 commit comments

Comments
 (0)