Skip to content

Commit

Permalink
feat: delete a test result
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-hc committed May 9, 2022
1 parent 0ba5bd7 commit 8245522
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 38 deletions.
12 changes: 10 additions & 2 deletions src/components/results/Table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as React from 'react'
import { FiTrash2 } from 'react-icons/fi'
import { format } from 'date-fns'
import { HamburgerIcon } from '@chakra-ui/icons'
import { $fetch } from 'ohmyfetch'

export const ResultsTable = props => (
<Table {...props}>
Expand Down Expand Up @@ -51,10 +52,17 @@ export const ResultsTable = props => (
rounded="full"
/>
<MenuList>
<MenuItem icon={<FiTrash2 />} command='⌘D'>
<MenuItem icon={<FiTrash2 />} command='⌘D' onClick={() => {
const res = $fetch(`/api/tests`, {
method: 'DELETE',
body: JSON.stringify({
id: result.id
})
})
}}>
Delete result
</MenuItem>
<MenuItem icon={<FiTrash2 />} command='⌘E'>
<MenuItem icon={<FiTrash2 />} command='⌘E' disabled>
Export result
</MenuItem>
</MenuList>
Expand Down
82 changes: 46 additions & 36 deletions src/pages/api/tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,57 @@ import { NextApiRequest } from 'next'
import { usePrisma } from '@prismaClient'

export default async function handler(req: NextApiRequest, res) {
const { test } = usePrisma()
const { method } = req
const { test, result } = usePrisma()

if (req.method === 'POST') {
const { repoId, path, repo } = JSON.parse(req.body)

// is the test exist
const isTest = await test.findFirst({
where: {
path: `${repo}/${path.join('/')}`
},
include: {
results: {
include: {
user: true
switch (method) {
case 'POST':
const { repoId, path, repo } = JSON.parse(req.body)
// is the test exist
const isTest = await test.findFirst({
where: {
path: `${repo}/${path.join('/')}`
},
include: {
results: {
include: {
user: true
}
}
}
}
})
if (isTest) {
console.log(`👹 ~ file: index.ts ~ line 34 ~ handler ~ isTest`, isTest)
res.status(200).json({
message: 'Test exists',
data: isTest
})
}

console.log(req.body)
if (!isTest) {
const newTest = await test.create({
data: {
repoId: repoId,
path: `${repo}/${path.join('/')}`,
name: path[path.length - 1]
if (isTest) {
res.status(200).json({
message: 'Test exists',
data: isTest
})
}

if (!isTest) {
const newTest = await test.create({
data: {
repoId: repoId,
path: `${repo}/${path.join('/')}`,
name: path[path.length - 1]
}
})
res.status(201).json({
message: 'Test created',
data: newTest
})
}
break
case 'DELETE':
console.log('delete case')
const { id } = JSON.parse(req.body)
const removed = await result.delete({
where: {
id: id
}
})
res.status(201).json({
message: 'Test created',
data: newTest
})
}
} else {
return res.status(405).json({ error: 'Method not allowed' })
res.status(200).json({ message: 'Test result deleted'})
break
default:
res.status(405).json({ error: 'Method not allowed' })
}
}

0 comments on commit 8245522

Please sign in to comment.