Skip to content

Commit

Permalink
Finish TS migration
Browse files Browse the repository at this point in the history
  • Loading branch information
neerajsamtani committed Aug 19, 2024
1 parent f88b215 commit cbc1430
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 42 deletions.
8 changes: 4 additions & 4 deletions client/src/CreateCashTransactionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export default function CreateCashTransactionModal({ show, onHide }: { show: boo
}
)

const date = useField("date")
const person = useField("text")
const description = useField("text")
const amount = useField("number")
const date = useField<string>("date", "" as string)
const person = useField<string>("text", "" as string)
const description = useField<string>("text", "" as string)
const amount = useField<number>("number", 0 as number)

const createCashTransaction = () => {
var REACT_APP_API_ENDPOINT = String(process.env.REACT_APP_API_ENDPOINT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import Modal from 'react-bootstrap/Modal';
import { useLineItems, useLineItemsDispatch } from "./contexts/LineItemsContext";
import Notification from './Notification';
import defaultNameCleanup from './utils/stringHelpers'
import { useField } from './hooks/useField';
import { FormField, useField } from './hooks/useField';
import Stack from 'react-bootstrap/Stack';
import Badge from 'react-bootstrap/Badge';

export default function CreateEventModal({ show, onHide }) {
export default function CreateEventModal({ show, onHide }: { show: boolean, onHide: () => void }) {

const lineItems = useLineItems();
const lineItemsDispatch = useLineItemsDispatch();
Expand All @@ -26,10 +26,10 @@ export default function CreateEventModal({ show, onHide }) {
}
}, [selectedLineItems, show])

const name = useField("text")
const category = useField("text", "All")
const date = useField("date")
const isDuplicateTransaction = useField("checkbox")
const name = useField<string>("text", "" as string)
const category = useField("select", "All" as string)
const date = useField<string>("date", "" as string)
const isDuplicateTransaction = useField<boolean>("checkbox", false)

const disableSubmit = name.value === "" || category.value === "" || category.value === "All"

Expand Down Expand Up @@ -58,7 +58,7 @@ export default function CreateEventModal({ show, onHide }) {
onHide()
}

const createEvent = (name, category) => {
const createEvent = (name: FormField<string>, category: FormField<string>) => {
var REACT_APP_API_ENDPOINT = String(process.env.REACT_APP_API_ENDPOINT);
var newEvent = {
"name": name.value,
Expand Down
8 changes: 4 additions & 4 deletions client/src/Login.jsx → client/src/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { useNavigate } from 'react-router-dom';
import Button from 'react-bootstrap/Button';
import Form from 'react-bootstrap/Form';
import axiosInstance from "./axiosInstance";
import { useField } from './hooks/useField';
import { useField, FormField } from './hooks/useField';

export default function Login() {
const email = useField("text")
const password = useField("password")
const email = useField("text", "" as string)
const password = useField("password", "" as string)
const navigate = useNavigate()

const handleLogin = (email, password) => {
const handleLogin = (email: FormField<string>, password: FormField<string>) => {
var REACT_APP_API_ENDPOINT = String(process.env.REACT_APP_API_ENDPOINT);
var newUser = {
"email": email.value,
Expand Down
25 changes: 0 additions & 25 deletions client/src/hooks/useField.jsx

This file was deleted.

36 changes: 36 additions & 0 deletions client/src/hooks/useField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useState, ChangeEvent } from 'react'

export type FormField<T> = {
type: string;
value: T;
onChange: (event: ChangeEvent<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>) => void;
setCustomValue: (value: T) => void;
setEmpty: () => void;
}

export const useField = <T extends string | number | boolean | readonly string[]>(
type: string,
defaultState: T
): FormField<T> => {
const [value, setValue] = useState<T>(defaultState)

const setEmpty = () => {
setValue('' as T)
}

const setCustomValue = (newValue: T) => {
setValue(newValue)
}

const onChange = (event: ChangeEvent<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>) => {
setValue(event.target.value as T)
}

return {
type,
value,
onChange,
setCustomValue,
setEmpty
}
}
1 change: 1 addition & 0 deletions client/src/react-app-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="react-scripts" />
4 changes: 2 additions & 2 deletions client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dom.iterable",
"esnext"
],
"allowJs": true,
"allowJs": false,
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"resolveJsonModule": true,
Expand All @@ -22,4 +22,4 @@
"include": [
"src"
]
}
}

0 comments on commit cbc1430

Please sign in to comment.