This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a standardized input component and fixes validation of input dat…
…a POC
- Loading branch information
Showing
23 changed files
with
379 additions
and
420 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import styled from "styled-components"; | ||
|
||
export const SumWrapper = styled.div` | ||
padding-bottom: 14px; | ||
`; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,170 +1,59 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import React, { useEffect } from "react"; | ||
import { useForm } from "react-hook-form"; | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import Validator from "validator"; | ||
import { Organization } from "../../../types/Organization"; | ||
import { setShares } from "../../../store/donation/actions"; | ||
import { State } from "../../../store/state"; | ||
import { ToolTip } from "../../shared/ToolTip/ToolTip"; | ||
|
||
import { | ||
OrganizationName, | ||
PercentageText, | ||
ShareInput, | ||
ShareInputContainer, | ||
} from "./ShareSelection.style"; | ||
import { TextInput } from "../../shared/Input/TextInput"; | ||
import { OrganizationShare } from "../../../types/Temp"; | ||
|
||
const tooltipLink = "https://gieffektivt.no/organisasjoner"; | ||
|
||
export const SharesSelection: React.FC = () => { | ||
const dispatch = useDispatch(); | ||
const [submitLoading, setSubmitLoading] = useState(false); | ||
const donorName = useSelector((state: State) => state.donation.donor?.name); | ||
const donorEmail = useSelector((state: State) => state.donation.donor?.email); | ||
// const donorSSN = useSelector((state: State) => state.donation.donor?.ssn); | ||
const donorNewsletter = useSelector( | ||
(state: State) => state.donation.donor?.newsletter | ||
); | ||
// const donationSum = useSelector((state: State) => state.donation.sum); | ||
const donationMethod = useSelector((state: State) => state.donation.method); | ||
const organizations = useSelector( | ||
(state: State) => state.layout.organizations | ||
); | ||
const [percentageErrorAnimation, setPercentageErrorAnimation] = useState( | ||
false | ||
); | ||
const { register, watch, handleSubmit, setValue } = useForm({ mode: "all" }); | ||
const { register, watch } = useForm({ mode: "all" }); | ||
const watchAllFields = watch(); | ||
|
||
function getTotalPercentage() { | ||
let totalPercentage = 0; | ||
let detectedNegativeShare = false; | ||
Object.keys(watchAllFields).forEach((property) => { | ||
const share = watchAllFields[property]; | ||
|
||
if (share !== "") totalPercentage += parseInt(watchAllFields[property]); | ||
if (share === "0") setValue(property, ""); | ||
if (parseInt(watchAllFields[property], 10) < 0) | ||
detectedNegativeShare = true; | ||
}); | ||
return { | ||
totalPercentage, | ||
detectedNegativeShare, | ||
}; | ||
} | ||
|
||
function setupOrganizationInput(org: Organization) { | ||
return ( | ||
<ShareInputContainer key={org.id}> | ||
<div> | ||
<OrganizationName>{org.name}</OrganizationName> | ||
<ToolTip text={org.shortDesc} link={tooltipLink} /> | ||
</div> | ||
<div> | ||
<ShareInput | ||
type="number" | ||
inputMode="decimal" | ||
placeholder="0" | ||
name={org.id.toString()} | ||
defaultValue={org.standardShare ? org.standardShare : 0} | ||
ref={register} | ||
/> | ||
<PercentageText>%</PercentageText> | ||
</div> | ||
</ShareInputContainer> | ||
); | ||
} | ||
|
||
useEffect(() => { | ||
const total = getTotalPercentage().totalPercentage; | ||
const negative = getTotalPercentage().detectedNegativeShare; | ||
if (total === 100) { | ||
// setNextDisabled(false); | ||
setPercentageErrorAnimation(false); | ||
} else if (organizations) { | ||
// setNextDisabled(true); | ||
setPercentageErrorAnimation(true); | ||
} | ||
if (negative) { | ||
// setNextDisabled(true); | ||
setPercentageErrorAnimation(true); | ||
} | ||
}, [watchAllFields]); | ||
|
||
/** | ||
* Check if donation is valid on input and set state | ||
* TODO: | ||
* Extract mappings to _mapping.ts | ||
*/ | ||
function onSubmit() { | ||
dispatch(setShares(watchAllFields)); | ||
if (getTotalPercentage().totalPercentage === 100) { | ||
setSubmitLoading(true); | ||
if ( | ||
donorName && | ||
donorEmail && | ||
donationMethod && | ||
donorNewsletter !== undefined | ||
) { | ||
const orgShares: Array<OrganizationShare> = []; | ||
|
||
Object.keys(watchAllFields).forEach((property) => { | ||
const Share = watchAllFields[property]; | ||
if (Share > 0 && organizations) { | ||
const orgShare: OrganizationShare = { id: 0, share: 0, name: "" }; | ||
orgShare.id = parseInt(property); | ||
orgShare.share = parseInt(watchAllFields[property]); | ||
organizations.forEach((org: Organization) => { | ||
if (orgShare.id === org.id) { | ||
orgShare.name = org.name; | ||
} | ||
}); | ||
orgShares.push(orgShare); | ||
} | ||
}); | ||
|
||
/* | ||
const postData: DonationData = { | ||
donor: { | ||
name: donorName, | ||
email: donorEmail, | ||
newsletter: donorNewsletter, | ||
}, | ||
// TODO: Send payment method as string (not number) | ||
method: "", | ||
organizations: orgShares, | ||
}; | ||
if (donationSum) postData.amount = donationSum; | ||
if (donorSSN) postData.donor.ssn = donorSSN.toString(); | ||
// TODO: Move dispatches from network.ts to here | ||
postDonation(postData, dispatch).then(() => { | ||
setSubmitLoading(false); | ||
}); | ||
*/ | ||
} | ||
useEffect(() => { | ||
if (Object.keys(watchAllFields).length > 0) { | ||
const shares = Object.keys(watchAllFields).map( | ||
(key): OrganizationShare => ({ | ||
id: parseInt(key), | ||
share: Validator.isInt(watchAllFields[key]) | ||
? parseInt(watchAllFields[key]) | ||
: 0, | ||
}) | ||
); | ||
dispatch(setShares(shares)); | ||
} | ||
} | ||
}, [watchAllFields]); | ||
|
||
if (!organizations) return <div>Ingen organisasjoner</div>; | ||
|
||
return ( | ||
<div> | ||
{!submitLoading ? ( | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<div> | ||
{organizations.map((org: Organization) => | ||
setupOrganizationInput(org) | ||
)} | ||
</div> | ||
{percentageErrorAnimation && ( | ||
<p> | ||
Du har fordelt | ||
{`${getTotalPercentage().totalPercentage} / 100%`} | ||
</p> | ||
)} | ||
</form> | ||
) : ( | ||
<p>Laster...</p> | ||
)} | ||
<form> | ||
<div> | ||
{organizations.map((org: Organization) => ( | ||
<TextInput | ||
label={org.name} | ||
name={org.id.toString()} | ||
key={org.id} | ||
type="tel" | ||
defaultValue={org.standardShare ? org.standardShare : 0} | ||
denomination="%" | ||
selectOnClick | ||
innerRef={register} | ||
/> | ||
))} | ||
</div> | ||
</form> | ||
</div> | ||
); | ||
}; |
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,12 @@ | ||
import React from "react"; | ||
import { useSelector } from "react-redux"; | ||
import { State } from "../../../store/state"; | ||
|
||
export const SharesSum: React.FC = () => { | ||
const shares = useSelector((state: State) => state.donation.shares); | ||
const sum = shares.reduce((acc, curr) => acc + curr.share, 0); | ||
|
||
if (sum === 100) return null; | ||
|
||
return <p>{`Du har fordelt ${sum} / 100%`}</p>; | ||
}; |
Oops, something went wrong.