Skip to content

Commit

Permalink
string typescript is on
Browse files Browse the repository at this point in the history
  • Loading branch information
jarjan committed Aug 26, 2024
1 parent 6536264 commit 2e97b33
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
13 changes: 8 additions & 5 deletions src/components/Calculator.js → src/components/Calculator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import { getSalaryInfo, formatCurrency } from "./utils";
import "./Calculator.css";

const Calculator = () => {
const [salary, setSalary] = useState(70000);
const [salary, setSalary] = useState<string>("70000");

const handleChangeSalary = (event) => {
setSalary(event.target.value);
const handleChangeSalary = (event: InputEvent) => {
if (event.target instanceof HTMLInputElement) {
setSalary(event.target.value);
}
};

const { nettoSalary, pension, tax, grossSalary, insurance } =
getSalaryInfo(salary);
const { nettoSalary, pension, tax, grossSalary, insurance } = getSalaryInfo(
Number(salary),
);

return (
<div className="Calculator">
Expand Down
22 changes: 15 additions & 7 deletions src/components/utils.js → src/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ const currencyFormatter = new Intl.NumberFormat("ru-KZ", {
currency: "KZT",
});

export const getPension = (nettoSalary, minSalary) =>
export const getPension = (nettoSalary: number, minSalary: number) =>
nettoSalary * 0.1 < minSalary * 75 ? nettoSalary * 0.1 : minSalary * 75;

export const getTax = (nettoSalary, minSalary, pension) =>
export const getTax = (
nettoSalary: number,
minSalary: number,
pension: number,
) =>
nettoSalary === minSalary ? 0 : (nettoSalary - pension - minSalary) * 0.1;

export const getGrossSalary = (nettoSalary, pension, tax) =>
nettoSalary - pension - tax;
export const getGrossSalary = (
nettoSalary: number,
pension: number,
tax: number,
) => nettoSalary - pension - tax;

export const getInsurance = (salary) => salary * 0.01;
export const getInsurance = (salary: number) => salary * 0.01;

export const getSalaryInfo = (salary) => {
// TODO: fix calculation and cover with tests
export const getSalaryInfo = (salary: number) => {
const minSalary = 70000;
if (salary <= minSalary) {
return { nettoSalary: salary, pension: 0, tax: 0, grossSalary: salary };
Expand All @@ -29,4 +37,4 @@ export const getSalaryInfo = (salary) => {
return { nettoSalary, pension, tax, grossSalary, insurance };
};

export const formatCurrency = (number) => currencyFormatter.format(number);
export const formatCurrency = (num: number) => currencyFormatter.format(num);
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export function App() {
return <Calculator />;
}

render(<App />, document.getElementById("app"));
render(<App />, document.getElementById("app")!);
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"allowJs": true,
"checkJs": true,
"jsx": "react-jsx",
"strict": true,
"jsxImportSource": "preact"
},
"include": ["node_modules/vite/client.d.ts", "**/*"]
Expand Down

0 comments on commit 2e97b33

Please sign in to comment.