-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
91 lines (81 loc) · 2.78 KB
/
App.js
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
import React, { useState, useEffect, useCallback } from 'react';
import './App.css';
function App() {
const [input, setInput] = useState('');
const [isResult, setIsResult] = useState(false);
const azureFunctionUrl = 'https://severlesslab2023.azurewebsites.net/api/HttpFun';
const handleInput = useCallback((value) => {
setInput((prevInput) => (isResult ? value : prevInput + value));
setIsResult(false);
}, [isResult]);
const handleOperationClick = useCallback((operation) => {
if (!isResult && !['+', '-', '*', '/'].includes(input.slice(-1))) {
setInput(input + operation);
} else if (isResult) {
setInput(input);
}
setIsResult(false);
}, [input, isResult]);
const handleClear = useCallback(() => {
setInput('');
setIsResult(false);
}, []);
const handleDelete = useCallback(() => {
setInput(input.slice(0, -1));
}, [input]);
const handleEqual = useCallback(async () => {
if (input.trim() === '') return;
try {
const response = await fetch(azureFunctionUrl, {
method: 'POST',
body: JSON.stringify({ expression: input }),
headers: { 'Content-Type': 'application/json' }
});
if (!response.ok) {
throw new Error(`Error: ${response.status}`);
}
const data = await response.json();
setInput(data.result.toString());
setIsResult(true);
} catch (error) {
console.error('Error calling Azure Function:', error);
setInput('Error');
setIsResult(true);
}
}, [input]);
useEffect(() => {
const handleKeyDown = (event) => {
if (event.key === 'Enter') {
handleEqual();
} else if (event.key === 'Backspace') {
handleDelete();
} else if (event.key === 'Escape') {
handleClear();
} else {
const button = event.key.match(/[0-9+\-*/.]/);
if (button) {
handleInput(button[0]);
}
}
};
document.addEventListener('keydown', handleKeyDown);
return () => document.removeEventListener('keydown', handleKeyDown);
}, [handleDelete, handleEqual, handleInput, handleClear]);
return (
<div className="App">
<div className="display">{input || "0"}</div>
<div className="button-container">
{'7894561230.'.split('').map(number => (
<button key={number} onClick={() => handleInput(number)}>{number}</button>
))}
<button onClick={() => handleOperationClick('+')}>+</button>
<button onClick={() => handleOperationClick('-')}>-</button>
<button onClick={() => handleOperationClick('*')}>*</button>
<button onClick={() => handleOperationClick('/')}>/</button>
<button onClick={handleEqual}>=</button>
<button onClick={handleClear}>AC</button>
</div>
</div>
);
}
export default App;