Skip to content

Commit

Permalink
Enable shift categories to be reordered through drag and drop
Browse files Browse the repository at this point in the history
  • Loading branch information
beverloo committed Mar 4, 2024
1 parent 9bb1a2c commit d69036f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
8 changes: 2 additions & 6 deletions app/admin/volunteers/shifts/ShiftCategoriesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export function ShiftCategoriesTable() {
field: 'id',
headerName: /* no header= */ '',
editable: false,
sortable: false,
width: 50,

// The default shift category cannot be removed, as it's hardcoded in the source when
Expand All @@ -66,15 +65,13 @@ export function ShiftCategoriesTable() {
field: 'name',
headerName: 'Category',
editable: true,
sortable: true,
flex: 1,
},
{
field: 'excitement',
headerAlign: 'center',
headerName: 'Default excitement',
editable: true,
sortable: true,
align: 'center',
width: 200,

Expand All @@ -90,7 +87,6 @@ export function ShiftCategoriesTable() {
headerName: 'Colour',
description: 'Colours that will be assigned to shifts',
editable: true,
sortable: false,
align: 'center',
width: 200,

Expand All @@ -102,7 +98,6 @@ export function ShiftCategoriesTable() {
headerAlign: 'center',
headerName: /* no header= */ '',
editable: true,
sortable: false,
align: 'center',
width: 50,

Expand Down Expand Up @@ -133,6 +128,7 @@ export function ShiftCategoriesTable() {
return (
<RemoteDataTable columns={columns} endpoint="/api/admin/event/shifts/categories"
defaultSort={{ field: 'order', sort: 'asc' }} subject="category"
enableCreate enableDelete enableUpdate pageSize={50} disableFooter />
enableCreate enableDelete enableReorder enableUpdate pageSize={50}
disableFooter />
);
}
17 changes: 17 additions & 0 deletions app/api/admin/event/shifts/categories/[[...id]]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@ createDataTableApi(kEventShiftCategoryRowModel, kEventShiftCategoryContext, {
};
},

async reorder({ order }) {
const dbInstance = db;
await dbInstance.transaction(async () => {
for (let index = 0; index < order.length; ++index) {
await db.update(tShiftsCategories)
.set({ shiftCategoryOrder: index })
.where(tShiftsCategories.shiftCategoryId.equals(order[index]))
.executeUpdate();
}
});

return { success: true };
},

async update({ row }) {
const affectedRows = await db.update(tShiftsCategories)
.set({
Expand All @@ -158,6 +172,9 @@ createDataTableApi(kEventShiftCategoryRowModel, kEventShiftCategoryContext, {
},

async writeLog({ id }, mutation, props) {
if (mutation === 'Reordered')
return; // no need to log when someone changes the order of categories

const shiftCategoryName = await db.selectFrom(tShiftsCategories)
.where(tShiftsCategories.shiftCategoryId.equals(id))
.selectOneColumn(tShiftsCategories.shiftCategoryName)
Expand Down

0 comments on commit d69036f

Please sign in to comment.