Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Added referral post
Browse files Browse the repository at this point in the history
  • Loading branch information
philiphand committed Nov 20, 2020
1 parent 7d7550e commit 1b9e79b
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 45 deletions.
18 changes: 6 additions & 12 deletions src/components/helpers/network.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { setDonorID, setKID } from "../../store/donation/actions"
import { setAnsweredReferral } from "../../store/layout/actions"
import { PaymentMethod } from "../../store/state"
import { DonationData, ReferralData } from "./network.types"

Expand All @@ -20,14 +21,6 @@ export async function getReferrals() {
return orgs.content
}

//TODO: Check if postReferrals actually works
export async function postReferrals(data: string) {
const xhttp = new XMLHttpRequest()
xhttp.open("POST", api_url + 'referrals/', true)
xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
xhttp.send(`data=${encodeURIComponent('test?')}`)
}

// Original code below

export function request(endpoint: string, type: string, data: any, cb: any) {
Expand All @@ -41,13 +34,13 @@ export function request(endpoint: string, type: string, data: any, cb: any) {
var response = JSON.parse(this.responseText);

if (response.status == 200) {
cb(null, response);
return cb(null, response);
}
else if (response.status == 400) {
cb(response.content, null);
return cb(response.content, null);
}
} else {
cb(this.status, null);
return cb(this.status, null);
}
    }
};
Expand Down Expand Up @@ -77,6 +70,8 @@ export async function postDonation(postData: DonationData, dispatch: Function) {
}

// TODO: Move dispatches to SharesPane instead?
console.log(data.content.hasAnsweredReferral)
dispatch(setAnsweredReferral(data.content.hasAnsweredReferral))
dispatch(setKID(data.content.KID))
dispatch(setDonorID(data.content.donorID))

Expand All @@ -94,7 +89,6 @@ export async function postDonation(postData: DonationData, dispatch: Function) {
// _self.getPane(VippsPane).setUrl(data.content.paymentProviderUrl)
// }
console.log(data)
return data
})
}

Expand Down
5 changes: 4 additions & 1 deletion src/components/panes/DonationPane/DonationPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ export default function DonationPane() {
if (donationSum && (currentPaymentMethod !== PaymentMethod.BANK && currentPaymentMethod !== PaymentMethod.BANK_UKID )) {
postData.amount = donationSum
}
postDonation(postData, dispatch)
postDonation(postData, dispatch).then(result => {
console.log(result)
})
}
}

dispatch(setPaneNumber(currentPaneNumber + (isCustomShare ? 1 : 2)))
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/components/panes/ReferralPane/ReferralPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { useForm } from 'react-hook-form'
import { useDispatch, useSelector } from 'react-redux'
import { setPaneNumber } from '../../../store/layout/actions'
import { State } from '../../../store/state'
import { getReferrals } from '../../helpers/network'
import { getReferrals, postReferral } from '../../helpers/network'
import { HorizontalLine, NavigationWrapper, Pane, PaneContainer, PaneTitle, UnderTitle, VerticalLine } from '../Panes.style'
import DonationInfoBar from '../shared/DonationInfoBar/DonationInfoBar'
import { NextButton, PrevButton } from '../shared/NavigationButtons'
import { ReferralButton, ReferralsWrapper } from './ReferralPane.style'

interface Referral {
ID: number;
name: string;
Expand All @@ -17,6 +18,7 @@ interface Referral {
export default function ReferralPane() {
const [referrals, setReferrals] = useState<Referral[]>()
const currentPaneNumber = useSelector((state: State) => state.layout.paneNumber)
const donorID = useSelector((state: State) => state.donation.donor?.donorID)
const { handleSubmit } = useForm()
const dispatch = useDispatch()

Expand All @@ -43,8 +45,16 @@ export default function ReferralPane() {
return referralsList
}

function onSubmit(referral: number) {
function onSubmit(referralID: number) {
dispatch(setPaneNumber(currentPaneNumber + 1))
if (donorID) {
const referralData = {
referralTypeID: referralID,
donorID: donorID,
otherComment: ""
}
postReferral(referralData)
}
}

return (
Expand Down
2 changes: 1 addition & 1 deletion src/components/panes/SharesPane/SharesPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default function SharesPane() {
<NavigationWrapper>
<PrevButton />
<VerticalLine />
<NextButton isDisabled={nextDisabled} />
<NextButton isDisabled={nextDisabled} text="Fullfør"/>
</NavigationWrapper>
</form>
:
Expand Down
2 changes: 1 addition & 1 deletion src/components/panes/shared/ErrorField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const ErrorWrapper = styled.div`
`

export const ErrorMessage = styled.div`
margin-left: 3px;
margin-left: 5px;
font-size: 12px;
color: red;
display: inline;
Expand Down
11 changes: 10 additions & 1 deletion src/store/layout/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LayoutActionTypes, SELECT_CUSTOM_SHARE, SELECT_PRIVACY_POLICY, SET_PANE_NUMBER } from './types';
import { LayoutActionTypes, SELECT_CUSTOM_SHARE, SELECT_PRIVACY_POLICY, SET_ANSWERED_REFERRAL, SET_PANE_NUMBER } from './types';

export function selectCustomShare(customShare: boolean): LayoutActionTypes {
return {
Expand Down Expand Up @@ -26,3 +26,12 @@ export function setPaneNumber(paneNumber: number): LayoutActionTypes {
},
};
}

export function setAnsweredReferral(answeredReferral: boolean) {
return {
type: SET_ANSWERED_REFERRAL,
payload: {
answeredReferral
}
}
}
4 changes: 3 additions & 1 deletion src/store/layout/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Reducer } from 'redux';
import { Layout } from '../state';
import { SET_PANE_NUMBER, LayoutActionTypes, SELECT_CUSTOM_SHARE, SELECT_PRIVACY_POLICY } from './types';
import { SET_PANE_NUMBER, LayoutActionTypes, SELECT_CUSTOM_SHARE, SELECT_PRIVACY_POLICY, SET_ANSWERED_REFERRAL } from './types';

const initialState: Layout = {
privacyPolicy: false,
Expand All @@ -27,6 +27,8 @@ export const layoutReducer: Reducer<Layout, LayoutActionTypes> = (
return { ...state, privacyPolicy: action.payload.privacyPolicy }
case SET_PANE_NUMBER:
return { ...state, paneNumber: action.payload.paneNumber}
case SET_ANSWERED_REFERRAL:
return { ...state, answeredReferral: action.payload.answeredReferral }
default:
return state;
}
Expand Down
10 changes: 9 additions & 1 deletion src/store/layout/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const SELECT_CUSTOM_SHARE = 'SELECT_CUSTOM_SHARE'
export const SELECT_PRIVACY_POLICY = 'SELECT_PRIVACY_POLICY'
export const SET_PANE_NUMBER = 'SET_PANE_NUMBER'
export const SET_ANSWERED_REFERRAL = 'SET_ANSWERRED_REFERRAL'

interface SelectCustomShare {
type: typeof SELECT_CUSTOM_SHARE
Expand All @@ -23,4 +24,11 @@ interface setPaneNumber {
}
}

export type LayoutActionTypes = SelectCustomShare | SelectPrivacyPolicy | setPaneNumber
interface setAnsweredReferral {
type: typeof SET_ANSWERED_REFERRAL
payload: {
answeredReferral: boolean
}
}

export type LayoutActionTypes = SelectCustomShare | SelectPrivacyPolicy | setPaneNumber | setAnsweredReferral
52 changes: 27 additions & 25 deletions src/store/state.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,62 @@
import { FormStateMap } from 'redux-form'
import { FormStateMap } from 'redux-form' //TODO: Remove redux form stuff

export interface State {
donation: Donation,
layout: Layout,
paypal: PayPalState,
error: Error,
form: FormStateMap,
donation: Donation
layout: Layout
paypal: PayPalState
error: Error
form: FormStateMap
}

//TODO: Remove privacyPolicy, it is only needed locally in DonationPane
export interface Layout {
privacyPolicy: boolean,
customShare: boolean,
paneNumber: number,
privacyPolicy: boolean
customShare: boolean
paneNumber: number
answeredReferral?: boolean
}

export interface PayPalState {
loading: boolean,
success: boolean,
loading: boolean
success: boolean
}

export interface DonationInput {
method?: PaymentMethod,
sum: number,
recurring: boolean,
donor?: Donor,
shares?: Splits,
method?: PaymentMethod
sum: number
recurring: boolean
donor?: Donor
shares?: Splits
}

export interface Donation extends DonationInput {
kid?: number
}

export interface DonorInput {
name?: string,
email?: string,
taxDeduction?: boolean,
ssn?: number,
newsletter?: boolean,
name?: string
email?: string
taxDeduction?: boolean
ssn?: number
newsletter?: boolean
}

export interface Donor extends DonorInput {
donorID?: number
}

export interface Share {
organizationID: number,
share: number,
organizationID: number
share: number
}

export interface Splits {
[key: string]: number
}

export interface Error {
isVisible: boolean,
message: string,
isVisible: boolean
message: string
}

export enum PaymentMethod {
Expand Down

0 comments on commit 1b9e79b

Please sign in to comment.