-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCostCalculatorWithExportPdf.tsx
More file actions
94 lines (88 loc) · 2.61 KB
/
CostCalculatorWithExportPdf.tsx
File metadata and controls
94 lines (88 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import {
CostCalculatorFlow,
CostCalculatorForm,
CostCalculatorSubmitButton,
CostCalculatorResetButton,
useCostCalculatorEstimationPdf,
buildCostCalculatorEstimationPayload,
} from '@remoteoss/remote-flows';
import type {
CostCalculatorEstimateResponse,
CostCalculatorEstimationSubmitValues,
} from '@remoteoss/remote-flows';
import './css/main.css';
import { useState } from 'react';
import { RemoteFlows } from './RemoteFlows';
import { downloadFile } from './utils';
const estimationOptions = {
title: 'Estimate for a new company',
includeBenefits: true,
includeCostBreakdowns: true,
};
function CostCalculatorFormDemo() {
const [estimations, setEstimations] =
useState<CostCalculatorEstimateResponse | null>(null);
const [payload, setPayload] =
useState<CostCalculatorEstimationSubmitValues | null>(null);
const exportPdfMutation = useCostCalculatorEstimationPdf();
const handleExportPdf = () => {
if (payload) {
exportPdfMutation.mutate(buildCostCalculatorEstimationPayload(payload), {
onSuccess: (response) => {
if (response?.data?.data?.content !== undefined) {
downloadFile(
response.data.data.content as unknown as string,
'estimation.pdf',
);
}
},
onError: (error) => {
console.error({ error });
},
});
}
};
return (
<>
<CostCalculatorFlow
estimationOptions={estimationOptions}
render={(props) => {
if (props.isLoading) {
return <div>Loading...</div>;
}
return (
<div>
<CostCalculatorForm
onSubmit={(payload) => setPayload(payload)}
onError={(error) => console.error({ error })}
onSuccess={(response) => {
setEstimations(response);
}}
/>
<div className='buttons-container'>
<CostCalculatorResetButton className='reset-button'>
Reset
</CostCalculatorResetButton>
<CostCalculatorSubmitButton className='submit-button'>
Get estimate
</CostCalculatorSubmitButton>
{estimations && (
<button className='submit-button' onClick={handleExportPdf}>
Export as PDF
</button>
)}
</div>
</div>
);
}}
/>
</>
);
}
export function CostCalculatorWithExportPdf() {
return (
<RemoteFlows>
<CostCalculatorFormDemo />
</RemoteFlows>
);
}