Skip to content

Commit

Permalink
Create table
Browse files Browse the repository at this point in the history
  • Loading branch information
Yarik-Popov committed Oct 5, 2024
1 parent a251cf0 commit db19eb4
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions frontend/src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import './app.css'
import CommandTable from './display/table'
import CommandInput from './input/command_input'

function App() {
return (
<>
<CommandInput />
<p>Command List:</p>
<CommandTable />
</>
)
}
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/data/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ export interface CommandResponse {
created_on: string
updated_on: string
}

export interface CommandListReponse {
data: CommandResponse[]
}
13 changes: 13 additions & 0 deletions frontend/src/display/command_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import axios from "axios";
import { CommandListReponse } from "../data/response";
import { API_URL } from "../environment";

export const getCommands = async (): Promise<CommandListReponse> => {
try {
const { data } = await axios.get<CommandListReponse>(`${API_URL}/commands/`)
return data;
} catch (error) {
console.error(error)
throw error
}
}
21 changes: 21 additions & 0 deletions frontend/src/display/row.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CommandResponse } from "../data/response";

interface CommandRowProp extends CommandResponse {
handleDelete: () => void;
}

const CommandRow = ({ id, name, params, format, created_on, updated_on, handleDelete }: CommandRowProp) => {
return (
<tr>
<th>{id}</th>
<th>{name}</th>
<th>{params}</th>
<th>{format}</th>
<th>{created_on}</th>
<th>{updated_on}</th>
<th><button onClick={handleDelete}>Delete {id}</button></th>
</tr>
)
}

export default CommandRow;
42 changes: 42 additions & 0 deletions frontend/src/display/table.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useEffect, useState } from "react"
import { CommandResponse } from "../data/response"
import { getCommands } from "./command_api"
import CommandRow from "./row"

const CommandTable = () => {
const [commands, setCommands] = useState<CommandResponse[]>([])

const getCommandsFn = async () => {
const data = await getCommands();
setCommands(data.data)
}

useEffect(() => {
getCommandsFn();
}, [])

const handleDelete = (id: number) => {
return () => { setCommands(commands.filter((value) => value.id != id)) }
}

return (
<table>
<thead>
<tr>
<th>ID: </th>
<th>Name: </th>
<th>Params: </th>
<th>Format: </th>
<th>Created On: </th>
<th>Updated On: </th>
<th>Delete</th>
</tr>
</thead>
<thead>
{commands.map(value => (<CommandRow {...value} handleDelete={handleDelete(value.id)} />))}
</thead>
</table>
)
}

export default CommandTable;

0 comments on commit db19eb4

Please sign in to comment.