-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(): componentize medication action (#145)
- Loading branch information
Showing
11 changed files
with
301 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
src/hostedPages/activities/collectMedication/CollectMedication.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import React, { FC, useCallback, useEffect, useState } from 'react' | ||
import { Button, InputField, useTheme } from '../../../atoms' | ||
import { HostedPageFooter } from '../../layouts/HostedPageLayout/HostedPageFooter' | ||
import layoutClasses from '../../layouts/HostedPageLayout/hostedPageLayout.module.scss' | ||
import classes from './collectMedication.module.scss' | ||
import { CollectMedicationProps, type Medication } from './types' | ||
import { isEmpty } from 'lodash' | ||
import { MinusCircleIcon } from '@heroicons/react/24/outline' | ||
|
||
export const CollectMedication: FC<CollectMedicationProps> = ({ | ||
text, | ||
onSubmit, | ||
}) => { | ||
const [medications, setMedications] = useState<Medication[]>([]) | ||
const { updateLayoutMode, resetLayoutMode } = useTheme() | ||
|
||
useEffect(() => { | ||
updateLayoutMode('flexible') | ||
|
||
return () => { | ||
// Reset to default mode on unmount | ||
resetLayoutMode() | ||
} | ||
}, []) | ||
|
||
const handleSubmit = useCallback(() => { | ||
const filteredMedications = medications.filter((medication) => { | ||
return ( | ||
!isEmpty(medication.name) || | ||
!isEmpty(medication.dose) || | ||
!isEmpty(medication.instructions) | ||
) | ||
}) | ||
|
||
onSubmit(filteredMedications) | ||
}, [medications, onsubmit]) | ||
|
||
const addMedication = () => { | ||
setMedications([...medications, { name: '', dose: '', instructions: '' }]) | ||
} | ||
|
||
const updateMedication = ( | ||
index: number, | ||
field: keyof Medication, | ||
value: string | ||
) => { | ||
const newMedications = [...medications] | ||
newMedications[index] = { ...newMedications[index], [field]: value } | ||
setMedications(newMedications) | ||
} | ||
|
||
const removeMedication = (index: number) => { | ||
setMedications(medications.filter((_, i) => i !== index)) | ||
} | ||
|
||
return ( | ||
<> | ||
<main | ||
id="ahp_main_content_with_scroll_hint" | ||
className={layoutClasses.main_content} | ||
> | ||
<div | ||
className={`${classes.container} ${classes.groupMedsListContainer}`} | ||
> | ||
{medications.map((medication, index) => ( | ||
<div className={classes.singleMedsListContainer} key={index}> | ||
<InputField | ||
id="name" | ||
label={text.medication_name} | ||
type="text" | ||
value={medication.name} | ||
onChange={(e) => | ||
updateMedication(index, 'name', e.target.value) | ||
} | ||
placeholder={text.medication_name} | ||
/> | ||
<InputField | ||
id="dose" | ||
label={text.medication_dose} | ||
type="text" | ||
value={medication.dose} | ||
onChange={(e) => | ||
updateMedication(index, 'dose', e.target.value) | ||
} | ||
placeholder={text.medication_dose} | ||
/> | ||
<InputField | ||
id="instructions" | ||
label={text.medication_instructions} | ||
type="text" | ||
value={medication.instructions || ''} | ||
onChange={(e) => | ||
updateMedication(index, 'instructions', e.target.value) | ||
} | ||
placeholder={text.medication_instructions} | ||
/> | ||
<button | ||
onClick={() => removeMedication(index)} | ||
type="button" | ||
className={classes.deleteMedsButton} | ||
> | ||
<MinusCircleIcon className={classes.icon} aria-hidden="true" /> | ||
</button> | ||
</div> | ||
))} | ||
</div> | ||
<div className={`${classes.container} ${classes.addMedsButton}`}> | ||
<Button onClick={addMedication} variant="secondary"> | ||
{text.add_medication_button} | ||
</Button> | ||
</div> | ||
</main> | ||
|
||
<HostedPageFooter showScrollHint={false}> | ||
<div className={`${classes.button_wrapper} ${classes.container}`}> | ||
<Button | ||
data-cy="submitMedication" | ||
variant="primary" | ||
onClick={handleSubmit} | ||
> | ||
{text.submit_medication} | ||
</Button> | ||
</div> | ||
</HostedPageFooter> | ||
</> | ||
) | ||
} | ||
|
||
CollectMedication.displayName = 'CollectMedication' |
86 changes: 86 additions & 0 deletions
86
src/hostedPages/activities/collectMedication/collectMedication.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
@use '../../../globalStyles'; | ||
|
||
:root { | ||
--awell-activity-collect-medication-max-width: 850px; | ||
--awell-activity-collect-medication-padding: var(--awell-activity-padding); | ||
} | ||
|
||
.container { | ||
max-width: var(--awell-activity-collect-medication-max-width); | ||
padding: var(--awell-activity-collect-medication-padding); | ||
margin: 0 auto; | ||
} | ||
|
||
.groupMedsListContainer { | ||
font-size: var(--awell-font-size-sm); | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
flex-direction: column; | ||
text-align: center; | ||
gap: var(--awell-spacing-4); | ||
@media (min-width: 640px) { | ||
font-size: var(--awell-font-size-base); | ||
gap: var(--awell-spacing-3); | ||
} | ||
} | ||
|
||
.singleMedsListContainer { | ||
display: flex; | ||
align-items: flex-end; | ||
justify-content: center; | ||
text-align: left; | ||
width: 100%; | ||
gap: var(--awell-spacing-2); | ||
@media (min-width: 640px) { | ||
gap: var(--awell-spacing-4); | ||
} | ||
} | ||
|
||
.singleMedsListContainer:not(:first-child) label { | ||
display: none; | ||
} | ||
|
||
.singleMedsListContainer > div { | ||
width: 100%; | ||
} | ||
|
||
.addMedsButton { | ||
margin-top: var(--awell-spacing-4); | ||
display: flex; | ||
justify-content: flex-start; | ||
} | ||
|
||
.deleteMedsButton { | ||
background-color: var(--awell-red50); | ||
border: none; | ||
color: var(--awell-red); | ||
border-radius: var(--awell-border-radius-md); | ||
display: flex; | ||
align-items: center; | ||
padding: var(--awell-spacing-2); | ||
cursor: pointer; | ||
&:hover { | ||
background-color: #fee2e2; | ||
} | ||
@media (min-width: 640px) { | ||
padding: var(--awell-spacing-2); | ||
} | ||
} | ||
|
||
.icon { | ||
width: 1.35rem; | ||
height: 1.35rem; | ||
@media (min-width: 640px) { | ||
width: 1.6rem; | ||
height: 1.6rem; | ||
} | ||
} | ||
|
||
.button_wrapper { | ||
display: flex; | ||
justify-content: flex-end; | ||
height: 100%; | ||
align-items: center; | ||
width: 100%; | ||
} |
47 changes: 47 additions & 0 deletions
47
src/hostedPages/activities/collectMedication/collectMedication.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from 'react' | ||
import { Meta, Story } from '@storybook/react/types-6-0' | ||
import { ThemeProvider } from '../../../atoms' | ||
import { HostedPageLayout } from '../../layouts/HostedPageLayout/HostedPageLayout' | ||
import { CollectMedication as CollectMedicationComponent } from './CollectMedication' | ||
import { CollectMedicationProps } from './types' | ||
|
||
export default { | ||
title: 'HostedPages/Activities/CollectMedication', | ||
component: CollectMedicationComponent, | ||
argTypes: { | ||
onSubmit: { action: 'submitted' }, | ||
}, | ||
decorators: [ | ||
(StoryComponent) => ( | ||
<ThemeProvider accentColor="#004ac2"> | ||
<StoryComponent /> | ||
</ThemeProvider> | ||
), | ||
], | ||
} as Meta | ||
|
||
export const CollectMedication: Story<CollectMedicationProps> = ({ | ||
onSubmit, | ||
}) => { | ||
return ( | ||
<HostedPageLayout | ||
logo={ | ||
'https://res.cloudinary.com/da7x4rzl4/image/upload/v1710884206/Developer%20portal/awell_logo.svg' | ||
} | ||
onCloseHostedPage={() => alert('Stop session')} | ||
> | ||
<CollectMedicationComponent | ||
onSubmit={onSubmit} | ||
text={{ | ||
medication_name: 'Name', | ||
medication_dose: 'Dose', | ||
medication_instructions: 'Instructions', | ||
add_medication_button: 'Add medication', | ||
submit_medication: 'Submit', | ||
}} | ||
/> | ||
</HostedPageLayout> | ||
) | ||
} | ||
|
||
CollectMedication.parameters = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { CollectMedication } from './CollectMedication' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export type Medication = { | ||
name: string | ||
dose: string | ||
instructions?: string | ||
} | ||
|
||
export interface CollectMedicationProps { | ||
onSubmit: (data: Medication[]) => void | ||
text: { | ||
medication_name: string | ||
medication_dose: string | ||
medication_instructions: string | ||
add_medication_button: string | ||
submit_medication: string | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters