feat: add posizione column to appunti and update related logic for data handling
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
parent
2669de4caf
commit
899289f6f8
7 changed files with 43 additions and 32 deletions
4
apps/db/migrations/50_kanban_posizione.up.sql
Normal file
4
apps/db/migrations/50_kanban_posizione.up.sql
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
ALTER TABLE IF EXISTS public.appunti
|
||||||
|
ADD COLUMN IF NOT EXISTS posizione integer;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -44,7 +44,8 @@ import { Textarea } from "../ui/textarea";
|
||||||
import { UserAvatar } from "../user_avatar";
|
import { UserAvatar } from "../user_avatar";
|
||||||
|
|
||||||
export const Appunti = () => {
|
export const Appunti = () => {
|
||||||
const { data, columns, setData, updateCall, removeColumn } = useAppunti();
|
const { data, columns, setData, removeColumn } = useAppunti();
|
||||||
|
console.log("Appunti data:", data);
|
||||||
return (
|
return (
|
||||||
<ScrollArea className="w-screen pr-2 sm:w-full">
|
<ScrollArea className="w-screen pr-2 sm:w-full">
|
||||||
<KanbanProvider
|
<KanbanProvider
|
||||||
|
|
@ -52,9 +53,6 @@ export const Appunti = () => {
|
||||||
columns={columns}
|
columns={columns}
|
||||||
data={data}
|
data={data}
|
||||||
onDataChange={setData}
|
onDataChange={setData}
|
||||||
onDragEnd={() => {
|
|
||||||
updateCall();
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
{(column) => (
|
{(column) => (
|
||||||
<KanbanBoard
|
<KanbanBoard
|
||||||
|
|
|
||||||
|
|
@ -189,7 +189,7 @@ export const KanbanCards = <T extends KanbanItemProps = KanbanItemProps>({
|
||||||
<ScrollArea className="overflow-hidden">
|
<ScrollArea className="overflow-hidden">
|
||||||
<SortableContext items={items}>
|
<SortableContext items={items}>
|
||||||
<div
|
<div
|
||||||
className={cn("flex flex-grow flex-col gap-2 p-2", className)}
|
className={cn("flex grow flex-col gap-2 p-2", className)}
|
||||||
{...props}
|
{...props}
|
||||||
>
|
>
|
||||||
{filteredData.map(children)}
|
{filteredData.map(children)}
|
||||||
|
|
|
||||||
|
|
@ -64,8 +64,16 @@ export const AppuntiProvider = ({ children }: AppuntiProviderProps) => {
|
||||||
const removeItem = (id: AppuntiId) => {
|
const removeItem = (id: AppuntiId) => {
|
||||||
removeItemMutation(id);
|
removeItemMutation(id);
|
||||||
};
|
};
|
||||||
const setData = (newData: AppuntiData[]) => {
|
const setData = (data: AppuntiData[]) => {
|
||||||
utils.appunti.getAppunti.setData(undefined, newData);
|
const dataWithPositions = data.map((item, index) => ({
|
||||||
|
...item,
|
||||||
|
posizione: index,
|
||||||
|
}));
|
||||||
|
console.log("Setting data with positions:", dataWithPositions);
|
||||||
|
utils.appunti.getAppunti.setData(undefined, dataWithPositions);
|
||||||
|
updateAppuntiMutation({
|
||||||
|
data: dataWithPositions,
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const { mutate: updateAppuntiMutation } =
|
const { mutate: updateAppuntiMutation } =
|
||||||
|
|
@ -76,9 +84,11 @@ export const AppuntiProvider = ({ children }: AppuntiProviderProps) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
const updateCall = () => {
|
const updateCall = () => {
|
||||||
updateAppuntiMutation({
|
const data = utils.appunti.getAppunti.getData();
|
||||||
data: utils.appunti.getAppunti.getData() || [],
|
console.log("Updating Appunti with data:", data);
|
||||||
});
|
// updateAppuntiMutation({
|
||||||
|
// data: data || [],
|
||||||
|
// });
|
||||||
};
|
};
|
||||||
|
|
||||||
const { mutate: addColumnMutation } = api.appunti.addAppuntiGroup.useMutation(
|
const { mutate: addColumnMutation } = api.appunti.addAppuntiGroup.useMutation(
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,8 @@ export default interface AppuntiTable {
|
||||||
userid: ColumnType<UsersId | null, UsersId | null, UsersId | null>;
|
userid: ColumnType<UsersId | null, UsersId | null, UsersId | null>;
|
||||||
|
|
||||||
appunti_group_id: ColumnType<AppuntiGroupsId, AppuntiGroupsId, AppuntiGroupsId>;
|
appunti_group_id: ColumnType<AppuntiGroupsId, AppuntiGroupsId, AppuntiGroupsId>;
|
||||||
|
|
||||||
|
posizione: ColumnType<number | null, number | null, number | null>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Appunti = Selectable<AppuntiTable>;
|
export type Appunti = Selectable<AppuntiTable>;
|
||||||
|
|
@ -36,6 +38,7 @@ export const AppuntiSchema = z.object({
|
||||||
created_at: z.date(),
|
created_at: z.date(),
|
||||||
userid: usersId.nullable(),
|
userid: usersId.nullable(),
|
||||||
appunti_group_id: appuntiGroupsId,
|
appunti_group_id: appuntiGroupsId,
|
||||||
|
posizione: z.number().nullable(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const NewAppuntiSchema = z.object({
|
export const NewAppuntiSchema = z.object({
|
||||||
|
|
@ -45,6 +48,7 @@ export const NewAppuntiSchema = z.object({
|
||||||
created_at: z.union([z.date(), z.string()]).pipe(z.coerce.date()).optional(),
|
created_at: z.union([z.date(), z.string()]).pipe(z.coerce.date()).optional(),
|
||||||
userid: usersId.optional().nullable(),
|
userid: usersId.optional().nullable(),
|
||||||
appunti_group_id: appuntiGroupsId,
|
appunti_group_id: appuntiGroupsId,
|
||||||
|
posizione: z.number().optional().nullable(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const AppuntiUpdateSchema = z.object({
|
export const AppuntiUpdateSchema = z.object({
|
||||||
|
|
@ -54,4 +58,5 @@ export const AppuntiUpdateSchema = z.object({
|
||||||
created_at: z.union([z.date(), z.string()]).pipe(z.coerce.date()).optional(),
|
created_at: z.union([z.date(), z.string()]).pipe(z.coerce.date()).optional(),
|
||||||
userid: usersId.optional().nullable(),
|
userid: usersId.optional().nullable(),
|
||||||
appunti_group_id: appuntiGroupsId.optional(),
|
appunti_group_id: appuntiGroupsId.optional(),
|
||||||
|
posizione: z.number().optional().nullable(),
|
||||||
});
|
});
|
||||||
|
|
@ -42,6 +42,7 @@ export const appuntiRouter = createTRPCRouter({
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.mutation(async ({ input }) => {
|
.mutation(async ({ input }) => {
|
||||||
|
console.log(input.data);
|
||||||
return await updateAppunti(input.data);
|
return await updateAppunti(input.data);
|
||||||
}),
|
}),
|
||||||
updateAppunto: adminProcedure
|
updateAppunto: adminProcedure
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ export const getAppunti = async (): Promise<AppuntiData[]> => {
|
||||||
"appunti.descrizione",
|
"appunti.descrizione",
|
||||||
"appunti.userid",
|
"appunti.userid",
|
||||||
"appunti.appunti_group_id as column",
|
"appunti.appunti_group_id as column",
|
||||||
|
"appunti.posizione",
|
||||||
])
|
])
|
||||||
.innerJoin(
|
.innerJoin(
|
||||||
"appunti_groups",
|
"appunti_groups",
|
||||||
|
|
@ -37,8 +38,9 @@ export const getAppunti = async (): Promise<AppuntiData[]> => {
|
||||||
)
|
)
|
||||||
.leftJoin("users", "appunti.userid", "users.id")
|
.leftJoin("users", "appunti.userid", "users.id")
|
||||||
.select("users.username")
|
.select("users.username")
|
||||||
.orderBy("appunti_groups.name", "asc")
|
//.orderBy("appunti.created_at", "asc")
|
||||||
.orderBy("appunti.created_at", "asc")
|
.orderBy("appunti.posizione", (ob) => ob.asc().nullsLast())
|
||||||
|
//.orderBy("appunti_groups.name", "asc")
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
return appunti.map((data) => ({
|
return appunti.map((data) => ({
|
||||||
|
|
@ -126,29 +128,20 @@ export const updateAppunto = async (id: AppuntiId, data: AppuntiUpdate) => {
|
||||||
|
|
||||||
export const updateAppunti = async (data: AppuntiData[]) => {
|
export const updateAppunti = async (data: AppuntiData[]) => {
|
||||||
try {
|
try {
|
||||||
const db_mappings = await db
|
const dataWithPositions = data.map((item, index) => ({
|
||||||
.selectFrom("appunti")
|
...item,
|
||||||
.select(["id", "appunti_group_id"])
|
posizione: index,
|
||||||
.execute();
|
}));
|
||||||
|
console.log("Data with positions to update:", dataWithPositions);
|
||||||
|
|
||||||
const updates = data
|
for (const item of dataWithPositions) {
|
||||||
.map((item) => {
|
const { column, username: _username, ...rest } = item;
|
||||||
const db_item = db_mappings.find((db_item) => db_item.id === item.id);
|
|
||||||
if (db_item && db_item.appunti_group_id !== item.column) {
|
|
||||||
const { column, username: _username, ...rest } = item;
|
|
||||||
return {
|
|
||||||
...rest,
|
|
||||||
appunti_group_id: column as AppuntiGroupsId,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
})
|
|
||||||
.filter((item) => item !== null);
|
|
||||||
|
|
||||||
for (const item of updates) {
|
|
||||||
await db
|
await db
|
||||||
.updateTable("appunti")
|
.updateTable("appunti")
|
||||||
.set(item)
|
.set({
|
||||||
|
...rest,
|
||||||
|
appunti_group_id: column as AppuntiGroupsId,
|
||||||
|
})
|
||||||
.where("id", "=", item.id)
|
.where("id", "=", item.id)
|
||||||
.execute();
|
.execute();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue