Skip to content

Commit d9a7d64

Browse files
nicolesorialnicolesorial
andauthored
Display GPAs and percentages as doubles (#89)
* grades as doubles * also in the quickinfofield Co-authored-by: nicolesorial <grace.cola@gmail.com>
1 parent 40b1c5a commit d9a7d64

3 files changed

Lines changed: 37 additions & 12 deletions

File tree

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React from "react";
22
import tw, { styled } from "twin.macro";
3-
import { NumberField } from "react-admin";
3+
import { FunctionField } from "react-admin";
44
import TextField from "./TextField";
55
import { getFieldColor } from "../../themes/field-colors";
66
import { STUDENT_COURSE } from "../../constants/apiObjects";
7+
import { formatDecimal } from '../../services/Utility'
78

89
const StyledTextField = styled(TextField)`
910
${tw`fontStyle-6 text-gray-700 font-medium`}
@@ -12,23 +13,24 @@ const StyledTextField = styled(TextField)`
1213
font-size: 1.4em;
1314
`;
1415

15-
const StyledNumberField = styled(NumberField)`
16+
const StyledFunctionField = styled(FunctionField)`
1617
${tw`fontStyle-6 text-gray-700 font-medium`}
1718
1819
color: ${props => props.color};
1920
font-size: 1.4em;
2021
`;
2122

2223
const GradeField = props => {
23-
return props.source === STUDENT_COURSE.LETTER_GRADE ? (
24-
<StyledTextField color={getFieldColor(props)} {...props} />
25-
) : (
26-
<StyledNumberField
27-
color={getFieldColor(props)}
28-
{...props}
29-
options={{ maximumFractionDigits: 2 }}
30-
/>
31-
);
24+
return props.source === STUDENT_COURSE.LETTER_GRADE ?
25+
(
26+
<StyledTextField color={getFieldColor(props)} {...props} />
27+
) : (
28+
<StyledFunctionField
29+
render={record => `${formatDecimal(record[props.source])}`}
30+
color={getFieldColor(props)}
31+
{...props}
32+
/>
33+
);
3234
};
3335

3436
export default GradeField;

pharmd-app/src/components/Fields/QuickInfoField.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Function Imports
44
import React from "react";
55
import { Loading, useGetOne } from "react-admin";
6+
import { formatDecimal } from "../../services/Utility";
67

78
// Component Imports
89
import { styled } from "twin.macro";
@@ -34,7 +35,7 @@ const QuickInfoField = ({ record = {}, source }) => {
3435
}
3536
return (
3637
<Info>
37-
<QuickInfo info={data.gpa} label="GPA" />
38+
<QuickInfo info={formatDecimal(data.gpa)} label="GPA" />
3839
<QuickInfo info={data.gradDate} label="Cohort" />
3940
</Info>
4041
);

pharmd-app/src/services/Utility.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,25 @@ export function arrayToObject(inputArray, keySourceArray) {
1313
}
1414

1515
export const doAllRequests = requests => axios.all(requests);
16+
17+
18+
export function formatDecimal(preformatted) {
19+
const preformattedString = preformatted.toString();
20+
const decimalRegex = /^(?<wholeNumber>\d*)(?<point>\.)?(?<decimal>\d*)?/;
21+
const numPlaces = 2;
22+
23+
const matches = preformattedString.match(decimalRegex);
24+
const wholeNumber = matches.groups.wholeNumber;
25+
const point = matches.groups.point || '.';
26+
let decimal = matches.groups.decimal || '00';
27+
28+
while (decimal.length < numPlaces) {
29+
decimal = decimal + '0';
30+
}
31+
32+
if (decimal.length > numPlaces) {
33+
decimal = decimal.substring(0, numPlaces);
34+
}
35+
36+
return wholeNumber + point + decimal;
37+
}

0 commit comments

Comments
 (0)