-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathFunctions.tsx
61 lines (50 loc) · 2.08 KB
/
Functions.tsx
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
import 'firebase/storage';
import * as React from 'react';
import { useState } from 'react';
import { useFirebaseApp, FunctionsProvider, useFunctions, useCallableFunctionResponse } from 'reactfire';
import { CardSection } from '../display/Card';
import { LoadingSpinner } from '../display/LoadingSpinner';
import { WideButton } from '../display/Button';
import { getFunctions, httpsCallable } from 'firebase/functions';
function UpperCaser() {
const functions = useFunctions();
const capitalizeTextRemoteFunction = httpsCallable<{ text: string }, string>(functions, 'capitalizeText');
const [uppercasedText, setText] = useState<string>('');
const [isUppercasing, setIsUppercasing] = useState<boolean>(false);
const greetings = ['Hello World', 'yo', `what's up?`];
const textToUppercase = greetings[Math.floor(Math.random() * greetings.length)];
async function handleButtonClick() {
setIsUppercasing(true);
const { data: capitalizedText } = await capitalizeTextRemoteFunction({ text: textToUppercase });
setText(capitalizedText);
setIsUppercasing(false);
}
return (
<>
<WideButton label="Uppercase some text" onClick={handleButtonClick} />
{isUppercasing ? <LoadingSpinner /> : <span>{uppercasedText || `click the button to capitalize "${textToUppercase}"`}</span>}
</>
);
}
function UpperCaserOnRender() {
const greetings = ['Hello World', 'yo', `what's up?`];
const textToUppercase = greetings[Math.floor(Math.random() * greetings.length)];
const { status, data: uppercasedText } = useCallableFunctionResponse<{ text: string }, string>('capitalizeText', { data: { text: textToUppercase } });
if (status === 'loading') {
return <LoadingSpinner />;
}
return <span>{uppercasedText}</span>;
}
export function Functions() {
const app = useFirebaseApp();
return (
<FunctionsProvider sdk={getFunctions(app)}>
<CardSection title="Call a cloud function">
<UpperCaser />
</CardSection>
<CardSection title="Call a function on render">
<UpperCaserOnRender />
</CardSection>
</FunctionsProvider>
);
}