Skip to content

Commit 2ef3614

Browse files
authored
Add adoption data from Salesforce to renewal form (#2579)
* Add adoption data from Salesforce to renewal form [DISCO-141] * Allow for possible lack of data * Handle staging subdomain * Catch fetch error
1 parent c9c053c commit 2ef3614

File tree

4 files changed

+153
-4
lines changed

4 files changed

+153
-4
lines changed

src/app/models/adoption-info.ts

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import {camelCaseKeys} from '~/helpers/page-data-utils';
2+
3+
export type AdoptionData = {
4+
adoptions: {
5+
baseYear: number;
6+
students: number | null;
7+
savings: number;
8+
}[];
9+
};
10+
11+
type WindowWithSettings = typeof window & {
12+
SETTINGS: {
13+
accountHref: string;
14+
};
15+
}
16+
const w = window as WindowWithSettings;
17+
const subdomains = ['staging.'];
18+
const subdomain = subdomains.find((sd) => w.SETTINGS.accountHref?.includes(sd)) || '';
19+
const url = `https://${subdomain}salesforce.openstax.org/api/v1/adoptions`;
20+
const TESTING = process.env.JEST_WORKER_ID !== undefined;
21+
/* eslint-disable camelcase */
22+
const testData = {
23+
count: 4,
24+
contact_id: '003U000001pyRebIAE',
25+
adoptions: [
26+
{
27+
id: 'a00Pc000007TWN3IAO',
28+
book: {
29+
id: 'a0Z0B00000HPp9eUAD',
30+
name: 'Business Ethics',
31+
official_name: 'Introductory Business Ethics',
32+
type: 'General',
33+
subject_areas: 'Business',
34+
website_url:
35+
'https://openstax.org/details/books/business-ethics'
36+
},
37+
base_year: 2022,
38+
school_year: '2022 - 23',
39+
school: 'Test University',
40+
confirmation_type: 'User Behavior Informed Adoption',
41+
students: null,
42+
savings: 0.0,
43+
how_using: null,
44+
confirmation_date: '2023-04-03'
45+
},
46+
{
47+
id: 'a00Pc000007TWN4IAO',
48+
book: {
49+
id: 'a0ZU0000008pytKMAQ',
50+
name: 'Biology',
51+
official_name: 'Biology',
52+
type: 'General',
53+
subject_areas: 'Science',
54+
website_url: 'https://openstax.org/details/books/biology'
55+
},
56+
base_year: 2022,
57+
school_year: '2022 - 23',
58+
school: 'Test University',
59+
confirmation_type: 'User Behavior Informed Adoption',
60+
students: null,
61+
savings: 0.0,
62+
how_using: null,
63+
confirmation_date: '2023-02-21'
64+
},
65+
{
66+
id: 'a00Pc000009o2B8IAI',
67+
book: {
68+
id: 'a0ZPc0000011zVVMAY',
69+
name: 'Calculus Volume 1',
70+
official_name: 'Calculus Volume 1',
71+
type: 'General',
72+
subject_areas: 'Math',
73+
website_url:
74+
'https://openstax.org/details/books/calculus-volume-1'
75+
},
76+
base_year: 2023,
77+
school_year: '2023 - 24',
78+
school: 'Test University',
79+
confirmation_type: 'OpenStax Confirmed Adoption',
80+
students: 50,
81+
savings: 3968.5,
82+
how_using: 'As the core textbook for my course',
83+
confirmation_date: '2024-03-04'
84+
},
85+
{
86+
id: 'a00Pc00000BD2uRIAT',
87+
book: {
88+
id: 'a0ZU0000008pyv1MAA',
89+
name: 'Micro Econ',
90+
official_name: 'Principles of Microeconomics',
91+
type: 'General',
92+
subject_areas: 'Business;Social Sciences',
93+
website_url:
94+
'https://openstax.org/details/books/principles-microeconomics'
95+
},
96+
base_year: 2023,
97+
school_year: '2023 - 24',
98+
school: 'Test University',
99+
confirmation_type: 'User Behavior Informed Adoption',
100+
students: null,
101+
savings: 0.0,
102+
how_using: null,
103+
confirmation_date: '2024-03-26'
104+
}
105+
],
106+
cache_create: '2024-04-12T16:17:43.345Z',
107+
cache_expire: '2024-04-12T17:17:43.345Z'
108+
};
109+
const testPromise = Promise.resolve(testData);
110+
111+
export default (TESTING ? testPromise : fetch(url).then((r) => r.json())).then(
112+
camelCaseKeys
113+
).catch(() => ({}));

src/app/pages/renewal-form/renewal-form.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {useLocation} from 'react-router-dom';
33
import linkHelper from '~/helpers/link';
44
import useUserContext from '~/contexts/user';
55
import useAdoptions from '~/models/renewals';
6+
import adoptionsPromise from './salesforce-data';
67
import BookTagsMultiselect, {BookTagsContextProvider, useBookTagsContext}
78
from '~/components/multiselect/book-tags/book-tags';
89

@@ -170,6 +171,14 @@ function TheForm() {
170171

171172
function EnsureLoggedIn() {
172173
const {userStatus: {uuid}} = useUserContext();
174+
const defaultMsg = `Reporting your use of OpenStax helps us
175+
secure additional funding for future titles!`;
176+
const [adoptionInfo, setAdoptionInfo] = React.useState(defaultMsg);
177+
178+
React.useEffect(
179+
() => adoptionsPromise.then((info) => info && setAdoptionInfo(info)),
180+
[]
181+
);
173182

174183
React.useEffect(
175184
() => {
@@ -194,6 +203,9 @@ function EnsureLoggedIn() {
194203

195204
return (
196205
<BookTagsContextProvider maxSelections={MAX_SELECTIONS}>
206+
<div>
207+
{adoptionInfo}
208+
</div>
197209
<TheForm />
198210
</BookTagsContextProvider>
199211
);
@@ -204,9 +216,6 @@ export default function RenewalForm() {
204216
<div className="renewal-form page">
205217
<div className="boxed">
206218
<h1>What textbook(s) are you using?</h1>
207-
<div>
208-
Reporting your use of OpenStax helps us secure additional funding for future titles!
209-
</div>
210219
<EnsureLoggedIn />
211220
</div>
212221
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import adoptionPromise, {AdoptionData} from '~/models/adoption-info';
2+
3+
const asUSD = new Intl.NumberFormat('en-US', {
4+
style: 'currency',
5+
currency: 'USD'
6+
});
7+
8+
function createThanksStatement({adoptions}: AdoptionData) {
9+
const firstYear = Math.min(...adoptions.map((a) => a.baseYear));
10+
const sumStudents = adoptions
11+
.map((a) => a.students)
12+
.filter((s): s is Exclude<typeof s, null> => s !== null)
13+
.reduce((a, b) => a + b);
14+
const sumSavings = adoptions.map((a) => a.savings).reduce((a, b) => a + b);
15+
16+
return (
17+
(firstYear
18+
? `Thanks for being an adopter of our books since ${firstYear}. `
19+
: '') +
20+
(sumSavings
21+
? `You've impacted ${sumStudents} students, saving them a total of
22+
${asUSD.format(sumSavings)}.`
23+
: '')
24+
);
25+
}
26+
27+
export default adoptionPromise.then(createThanksStatement);

test/src/pages/renewal-form.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ test(`renders navigator`, async () => {
1313
</ShellContextProvider>
1414
);
1515

16-
screen.getByText('Reporting your use', {exact: false});
16+
screen.getByText('logged in', {exact: false});
1717
await screen.findByRole('button');
1818
});

0 commit comments

Comments
 (0)