fix: correct some workflow problems

This commit is contained in:
Riccardo
2024-05-28 07:11:57 +02:00
parent 85d66215a7
commit 8c36c90710
9 changed files with 47 additions and 20 deletions

View File

@@ -56,7 +56,7 @@ export function CreateItemForm({ profile, formAction, tags }: CreateItemProps) {
</Select> </Select>
</FormControl> </FormControl>
{profile && ( {profile && (
<FormControl variant="outlined"> <FormControl variant="outlined" required>
<InputLabel id="tag-label">Tag</InputLabel> <InputLabel id="tag-label">Tag</InputLabel>
<Select <Select
name="tag" name="tag"

View File

@@ -143,6 +143,7 @@ export default function ItemsTable() {
type="submit" type="submit"
onClick={handleCreateItemDialog} onClick={handleCreateItemDialog}
sx={{ width: '200px' }} sx={{ width: '200px' }}
disabled={!profile}
> >
Create Item Create Item
</Button> </Button>

View File

@@ -10,7 +10,7 @@ import {
Typography Typography
} from '@mui/material'; } from '@mui/material';
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
import { Dispatch, useEffect, useState } from 'react'; import { Dispatch, useCallback, useEffect, useState } from 'react';
import { useFormState } from 'react-dom'; import { useFormState } from 'react-dom';
import { Profiles } from '../../../../data/types'; import { Profiles } from '../../../../data/types';
import { CreateProfileAction } from './actions/CreateProfileAction'; import { CreateProfileAction } from './actions/CreateProfileAction';
@@ -51,6 +51,11 @@ export default function ProfilesTable({
fetchProfiles(); fetchProfiles();
}, [formState, refreshKey]); }, [formState, refreshKey]);
const handleDelete = useCallback(async () => {
setRefreshKey((prevKey) => prevKey + 1);
setSelectedProfile(undefined);
}, [setSelectedProfile]);
return ( return (
<Stack <Stack
sx={{ sx={{
@@ -78,6 +83,7 @@ export default function ProfilesTable({
<TableHead> <TableHead>
<TableRow sx={{ '& th': { textAlign: 'center' } }}> <TableRow sx={{ '& th': { textAlign: 'center' } }}>
<TableCell>Name</TableCell> <TableCell>Name</TableCell>
<TableCell>Creation date</TableCell>
<TableCell>Delete</TableCell> <TableCell>Delete</TableCell>
</TableRow> </TableRow>
</TableHead> </TableHead>
@@ -89,7 +95,7 @@ export default function ProfilesTable({
profile={profile} profile={profile}
selected={selectedProfile} selected={selectedProfile}
setSelected={setSelectedProfile} setSelected={setSelectedProfile}
onDelete={() => setRefreshKey((prevKey) => prevKey + 1)} onDelete={handleDelete}
/> />
))} ))}
</TableBody> </TableBody>

View File

@@ -55,13 +55,17 @@ export default function ProfilesTableRow({
}; };
return ( return (
<TableRow key={profile.id}> <TableRow
<TableCell sx={{
align="center" '& td': {
textAlign: 'center',
bgcolor: profile.id === selected ? 'lightblue' : 'inherit'
}
}}
onClick={handleClick} onClick={handleClick}
onDoubleClick={handleDoubleClick} key={profile.id}
sx={{ bgcolor: profile.id === selected ? 'lightblue' : 'inherit' }}
> >
<TableCell onDoubleClick={handleDoubleClick}>
{isEditing ? ( {isEditing ? (
<TextField <TextField
value={name} value={name}
@@ -73,7 +77,8 @@ export default function ProfilesTableRow({
name name
)} )}
</TableCell> </TableCell>
<TableCell align="center"> <TableCell>{new Date(profile.createdAt).toLocaleString()}</TableCell>
<TableCell>
<Button onClick={handleDelete}>Delete</Button> <Button onClick={handleDelete}>Delete</Button>
</TableCell> </TableCell>
</TableRow> </TableRow>

View File

@@ -40,6 +40,8 @@ export default function TagsTable({ selectedProfile }: TagsTableProps) {
} catch (error) { } catch (error) {
console.error("Couldn't fetch tags."); console.error("Couldn't fetch tags.");
} }
} else {
setTags([]);
} }
}; };
@@ -82,6 +84,7 @@ export default function TagsTable({ selectedProfile }: TagsTableProps) {
<TableHead> <TableHead>
<TableRow sx={{ '& th': { textAlign: 'center' } }}> <TableRow sx={{ '& th': { textAlign: 'center' } }}>
<TableCell>Name</TableCell> <TableCell>Name</TableCell>
<TableCell>Creation date</TableCell>
<TableCell>Delete</TableCell> <TableCell>Delete</TableCell>
</TableRow> </TableRow>
</TableHead> </TableHead>

View File

@@ -57,8 +57,11 @@ export default function TagsTableRow({ tag, onDelete }: TagsTableRowProps) {
name name
)} )}
</TableCell> </TableCell>
<TableCell>{new Date(tag.createdAt).toLocaleString()}</TableCell>
<TableCell> <TableCell>
<Button onClick={handleDelete}>Delete</Button> <Button onClick={handleDelete} disabled={tag.Items > 0}>
Delete
</Button>
</TableCell> </TableCell>
</TableRow> </TableRow>
); );

View File

@@ -2,7 +2,6 @@
import { z } from 'zod'; import { z } from 'zod';
import { initializeUser } from '../../../../../data/initializeUser'; import { initializeUser } from '../../../../../data/initializeUser';
import { Profiles } from '../../../../../data/types';
import prisma from '../../../../../prisma/prisma'; import prisma from '../../../../../prisma/prisma';
interface TagsActionProps { interface TagsActionProps {
@@ -19,7 +18,7 @@ export async function TagsAction({ selectedProfile }: TagsActionProps) {
const user = await initializeUser(); const user = await initializeUser();
try { try {
const profiles = await prisma.tag.findMany({ const tags = await prisma.tag.findMany({
where: { where: {
Profile: { Profile: {
User: { User: {
@@ -28,12 +27,22 @@ export async function TagsAction({ selectedProfile }: TagsActionProps) {
id: selectedProfile id: selectedProfile
} }
}, },
include: {
Items: {
select: {
id: true
}
}
},
orderBy: { orderBy: {
createdAt: 'desc' createdAt: 'desc'
} }
}); });
return profiles as Profiles; return tags.map((tag) => ({
...tag,
Items: tag.Items.length
}));
} catch (error) { } catch (error) {
throw new Error('Failed to find tags'); throw new Error('Failed to find tags');
} }

View File

@@ -69,7 +69,7 @@ export const CreateProfileFormSchema = z.object({
export const ProfileSchema = z.object({ export const ProfileSchema = z.object({
id: z.string(), id: z.string(),
name: z.string(), name: z.string(),
createdAt: z.date().optional() createdAt: z.date()
}); });
export type Profile = z.infer<typeof ProfileSchema>; export type Profile = z.infer<typeof ProfileSchema>;
@@ -85,7 +85,8 @@ export const CreateTagFormSchema = z.object({
export const TagSchema = z.object({ export const TagSchema = z.object({
id: z.string(), id: z.string(),
name: z.string(), name: z.string(),
createdAt: z.date().optional() Items: z.number(),
createdAt: z.date()
}); });
export type Tag = z.infer<typeof TagSchema>; export type Tag = z.infer<typeof TagSchema>;

View File

@@ -12,7 +12,6 @@
"typecheck": "tsc --noEmit", "typecheck": "tsc --noEmit",
"prepare": "husky install", "prepare": "husky install",
"migrate": "npx prisma migrate dev", "migrate": "npx prisma migrate dev",
"push": "npx prisma db push",
"generate": "npx prisma generate", "generate": "npx prisma generate",
"reset": "npx prisma db push --force-reset" "reset": "npx prisma db push --force-reset"
}, },
@@ -51,10 +50,10 @@
"typescript": "^5" "typescript": "^5"
}, },
"lint-staged": { "lint-staged": {
"*.ts": [ "*.(ts,tsx)": [
"eslint --quiet --fix" "eslint --quiet --fix"
], ],
"*.{json,ts}": [ "*.{json,ts,tsx}": [
"prettier --write --ignore-unknown" "prettier --write --ignore-unknown"
] ]
} }