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";
|
||||
|
||||
export const Appunti = () => {
|
||||
const { data, columns, setData, updateCall, removeColumn } = useAppunti();
|
||||
const { data, columns, setData, removeColumn } = useAppunti();
|
||||
console.log("Appunti data:", data);
|
||||
return (
|
||||
<ScrollArea className="w-screen pr-2 sm:w-full">
|
||||
<KanbanProvider
|
||||
|
|
@ -52,9 +53,6 @@ export const Appunti = () => {
|
|||
columns={columns}
|
||||
data={data}
|
||||
onDataChange={setData}
|
||||
onDragEnd={() => {
|
||||
updateCall();
|
||||
}}
|
||||
>
|
||||
{(column) => (
|
||||
<KanbanBoard
|
||||
|
|
|
|||
|
|
@ -189,7 +189,7 @@ export const KanbanCards = <T extends KanbanItemProps = KanbanItemProps>({
|
|||
<ScrollArea className="overflow-hidden">
|
||||
<SortableContext items={items}>
|
||||
<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}
|
||||
>
|
||||
{filteredData.map(children)}
|
||||
|
|
|
|||
|
|
@ -64,8 +64,16 @@ export const AppuntiProvider = ({ children }: AppuntiProviderProps) => {
|
|||
const removeItem = (id: AppuntiId) => {
|
||||
removeItemMutation(id);
|
||||
};
|
||||
const setData = (newData: AppuntiData[]) => {
|
||||
utils.appunti.getAppunti.setData(undefined, newData);
|
||||
const setData = (data: AppuntiData[]) => {
|
||||
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 } =
|
||||
|
|
@ -76,9 +84,11 @@ export const AppuntiProvider = ({ children }: AppuntiProviderProps) => {
|
|||
});
|
||||
|
||||
const updateCall = () => {
|
||||
updateAppuntiMutation({
|
||||
data: utils.appunti.getAppunti.getData() || [],
|
||||
});
|
||||
const data = utils.appunti.getAppunti.getData();
|
||||
console.log("Updating Appunti with data:", data);
|
||||
// updateAppuntiMutation({
|
||||
// data: data || [],
|
||||
// });
|
||||
};
|
||||
|
||||
const { mutate: addColumnMutation } = api.appunti.addAppuntiGroup.useMutation(
|
||||
|
|
|
|||
|
|
@ -19,6 +19,8 @@ export default interface AppuntiTable {
|
|||
userid: ColumnType<UsersId | null, UsersId | null, UsersId | null>;
|
||||
|
||||
appunti_group_id: ColumnType<AppuntiGroupsId, AppuntiGroupsId, AppuntiGroupsId>;
|
||||
|
||||
posizione: ColumnType<number | null, number | null, number | null>;
|
||||
}
|
||||
|
||||
export type Appunti = Selectable<AppuntiTable>;
|
||||
|
|
@ -36,6 +38,7 @@ export const AppuntiSchema = z.object({
|
|||
created_at: z.date(),
|
||||
userid: usersId.nullable(),
|
||||
appunti_group_id: appuntiGroupsId,
|
||||
posizione: z.number().nullable(),
|
||||
});
|
||||
|
||||
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(),
|
||||
userid: usersId.optional().nullable(),
|
||||
appunti_group_id: appuntiGroupsId,
|
||||
posizione: z.number().optional().nullable(),
|
||||
});
|
||||
|
||||
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(),
|
||||
userid: usersId.optional().nullable(),
|
||||
appunti_group_id: appuntiGroupsId.optional(),
|
||||
posizione: z.number().optional().nullable(),
|
||||
});
|
||||
|
|
@ -42,6 +42,7 @@ export const appuntiRouter = createTRPCRouter({
|
|||
}),
|
||||
)
|
||||
.mutation(async ({ input }) => {
|
||||
console.log(input.data);
|
||||
return await updateAppunti(input.data);
|
||||
}),
|
||||
updateAppunto: adminProcedure
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ export const getAppunti = async (): Promise<AppuntiData[]> => {
|
|||
"appunti.descrizione",
|
||||
"appunti.userid",
|
||||
"appunti.appunti_group_id as column",
|
||||
"appunti.posizione",
|
||||
])
|
||||
.innerJoin(
|
||||
"appunti_groups",
|
||||
|
|
@ -37,8 +38,9 @@ export const getAppunti = async (): Promise<AppuntiData[]> => {
|
|||
)
|
||||
.leftJoin("users", "appunti.userid", "users.id")
|
||||
.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();
|
||||
|
||||
return appunti.map((data) => ({
|
||||
|
|
@ -126,29 +128,20 @@ export const updateAppunto = async (id: AppuntiId, data: AppuntiUpdate) => {
|
|||
|
||||
export const updateAppunti = async (data: AppuntiData[]) => {
|
||||
try {
|
||||
const db_mappings = await db
|
||||
.selectFrom("appunti")
|
||||
.select(["id", "appunti_group_id"])
|
||||
.execute();
|
||||
const dataWithPositions = data.map((item, index) => ({
|
||||
...item,
|
||||
posizione: index,
|
||||
}));
|
||||
console.log("Data with positions to update:", dataWithPositions);
|
||||
|
||||
const updates = data
|
||||
.map((item) => {
|
||||
const db_item = db_mappings.find((db_item) => db_item.id === item.id);
|
||||
if (db_item && db_item.appunti_group_id !== item.column) {
|
||||
for (const item of dataWithPositions) {
|
||||
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
|
||||
.updateTable("appunti")
|
||||
.set(item)
|
||||
.set({
|
||||
...rest,
|
||||
appunti_group_id: column as AppuntiGroupsId,
|
||||
})
|
||||
.where("id", "=", item.id)
|
||||
.execute();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue