Skip to content

Commit

Permalink
All but featured-resources have test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyEJohnson committed Oct 29, 2024
1 parent 097a0de commit 8087a42
Show file tree
Hide file tree
Showing 24 changed files with 670 additions and 493 deletions.
4 changes: 3 additions & 1 deletion src/app/components/dialog/dialog.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export function useDialog(
): [
BoundDialog: ({
children
}: React.PropsWithChildren<object & {aria: ReactModal.Aria}>) => React.ReactNode,
}: React.PropsWithChildren<
object & {aria?: ReactModal.Aria} & {title?: string}
>) => React.ReactNode,
open: () => void,
close: () => void,
showDialog: boolean
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React from 'react';
import ResourceBoxes, {ResourceModel} from '../resource-box/resource-boxes';
import {FormattedMessage} from 'react-intl';
import './featured-resources.scss';

type ResourcesProps = {
header: string;
models: ResourceModel[];
'data-analytics-content-list': string;
};

function FeaturedResources({header, models, ...props}: ResourcesProps) {
const modResources = models.map((res) => {
const storageKey = `featured-resource-${res.heading}`;
const seenTimes = 1 + Number(window.localStorage[storageKey] || 0);
const model = Object.assign(
{
isNew: seenTimes <= 3,
onClick: () => {
window.localStorage[storageKey] = 5;
model.isNew = false;
}
},
res
);

window.localStorage[storageKey] = seenTimes;

return model;
});

return (
<div className="card">
<div className="headline">{header}</div>
<div
data-analytics-content-list={
props['data-analytics-content-list']
}
className="resources"
>
<ResourceBoxes models={modResources} />
</div>
</div>
);
}

export default function FeaturedResourcesSection({
header,
models,
...props
}: ResourcesProps) {
return (
<div>
<div className="featured-resources">
<FeaturedResources header={header} models={models} {...props} />
</div>
<div className="divider">
<div className="line"></div>
<FormattedMessage
id="resources.additional"
defaultMessage="see additional resources below"
/>
<div className="line"></div>
</div>
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function GiveBeforeOther({
close: () => void;
data: DonationPopupData;
track?: string;
variant?: string;
variant: string;
onDownload?: (event: React.MouseEvent) => void;
id?: string;
}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function GiveBeforePdfAfterConditionals({
}: {
onThankYouClick: ReturnType<typeof useOnThankYouClick>['onThankYouClick'];
link: string;
track: string | undefined;
track?: string;
data: DonationPopupData;
close: () => void;
onDownload?: (event: React.MouseEvent) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,67 @@ import React from 'react';
import useUserContext from '~/contexts/user';
import trackLink from '../../track-link';
import './thank-you-form.scss';
import {TrackedMouseEvent} from '~/components/shell/router-helpers/use-link-handler';

function FormWithAfterSubmit({
afterSubmit,
responseId = 'form-response',
children,
onSubmit: firstOnSubmit,
...formProps
}) {
const iframeRef = React.useRef();
}: React.PropsWithChildren<
{
afterSubmit: () => void;
responseId?: string;
} & React.FormHTMLAttributes<HTMLFormElement>
>) {
const iframeRef = React.useRef<HTMLIFrameElement>(null);
const onSubmit = React.useCallback(
(event) => {
if (firstOnSubmit) {
firstOnSubmit(event);
if (event.defaultPrevented) {
return;
}
}
() => {
const iframe = iframeRef.current;
const runAfterSubmit = () => {
afterSubmit();
iframe.removeEventListener('load', runAfterSubmit);
iframe?.removeEventListener('load', runAfterSubmit);
};

iframe.addEventListener('load', runAfterSubmit);
iframe?.addEventListener('load', runAfterSubmit);
},
[firstOnSubmit, afterSubmit]
[afterSubmit]
);

return (
<React.Fragment>
<iframe
name={responseId}
title={responseId}
id={responseId}
className='hidden'
src=''
width='0'
height='0'
tabIndex='-1'
className="hidden"
src=""
width="0"
height="0"
tabIndex={-1}
ref={iframeRef}
/>
<form target={responseId} {...formProps} onSubmit={onSubmit}>
<form target={responseId} {...formProps} onSubmit={onSubmit} name="thank-you form">
{children}
</form>
</React.Fragment>
);
}

export default function ThankYou({link, close, source, itemType='PDF', track, id}) {
export default function ThankYou({
link,
close,
source,
itemType = 'PDF',
track,
id
}: {
link: string;
close: () => void;
source: string;
itemType?: string;
track?: string;
id?: string;
}) {
const {userModel} = useUserContext();
const first = userModel?.first_name;
const last = userModel?.last_name;
Expand All @@ -59,7 +72,7 @@ export default function ThankYou({link, close, source, itemType='PDF', track, id
close();
}, [link, close]);
const trackDownload = React.useCallback(
(event) => {
(event: TrackedMouseEvent) => {
trackLink(event, id);
},
[id]
Expand All @@ -68,81 +81,83 @@ export default function ThankYou({link, close, source, itemType='PDF', track, id
return (
<FormWithAfterSubmit
action={`${process.env.API_ORIGIN}/apps/cms/api/donations/thankyounote/`}
method='post'
className='thank-you-form'
method="post"
className="thank-you-form"
afterSubmit={afterSubmit}
>
<input type='hidden' name='source' value={source} />
<input type="hidden" name="source" value={source} />
<h1>Send us a thank you note</h1>
<div className='instructions'>
<span className='asterisk' />
<div className="instructions">
<span className="asterisk" />
indicates required field
</div>
<label>
Your message <span className='asterisk' />
Your message <span className="asterisk" />
<textarea
name='thank_you_note'
rows='4'
placeholder='Start typing'
name="thank_you_note"
rows={4}
placeholder="Start typing"
required
/>
</label>
<div className='names'>
<div className="names">
<label>
First name <span className='asterisk' />
First name <span className="asterisk" />
<input
name='first_name'
type='text'
placeholder='Your first name'
name="first_name"
type="text"
placeholder="Your first name"
required
defaultValue={first}
/>
</label>
<label>
Last name <span className='asterisk' />
Last name <span className="asterisk" />
<input
name='last_name'
type='text'
placeholder='Your last name'
name="last_name"
type="text"
placeholder="Your last name"
required
defaultValue={last}
/>
</label>
<label>
Institution name <span className='asterisk' />
Institution name <span className="asterisk" />
<input
name='institution'
type='text'
placeholder='Your school'
name="institution"
type="text"
placeholder="Your school"
required
defaultValue={school}
/>
</label>
<label>
Email address
<input
name='contact_email_address'
type='email'
placeholder='Your email (optional)'
name="contact_email_address"
type="email"
placeholder="Your email (optional)"
/>
</label>
</div>
<div className='consent-checkbox'>
<div className="consent-checkbox">
<label>
Consent <span className='asterisk' />
Consent <span className="asterisk" />
</label>
<label className='cb-group'>
<label className="cb-group">
<input
type='checkbox'
name='consent_to_share_or_contact'
value='True'
type="checkbox"
name="consent_to_share_or_contact"
value="True"
/>
By clicking here I understand that OpenStax may include my
story in publications and distribution in printed,
electronic, and digital media.
</label>
</div>
<button className='primary'>Submit note and go to {itemType}</button>
<button className="primary">
Submit note and go to {itemType}
</button>
<a
className="never-mind"
href={link}
Expand All @@ -158,7 +173,7 @@ export default function ThankYou({link, close, source, itemType='PDF', track, id

export function useOnThankYouClick() {
const [showThankYou, setShowThankYou] = React.useState(false);
const onThankYouClick = React.useCallback((event) => {
const onThankYouClick = React.useCallback((event: React.MouseEvent) => {
event.preventDefault();
setShowThankYou(true);
}, []);
Expand Down
Loading

0 comments on commit 8087a42

Please sign in to comment.