generated from taylorbryant/gatsby-starter-tailwind
-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new form for changing climb left-right order
- Loading branch information
viet nguyen
committed
Dec 28, 2023
1 parent
673ea55
commit 657166f
Showing
12 changed files
with
289 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 2 additions & 10 deletions
12
src/app/editArea/[slug]/general/components/climb/ClimbListForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
src/app/editArea/[slug]/manageClimbs/components/sorting/SortClimbsForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
'use client' | ||
import { useEffect, useState } from 'react' | ||
import { useSession } from 'next-auth/react' | ||
import { useRouter } from 'next/navigation' | ||
import { | ||
DndContext, | ||
closestCenter, | ||
KeyboardSensor, | ||
useSensor, | ||
useSensors, | ||
DragEndEvent, | ||
MouseSensor as LibMouseSensor | ||
} from '@dnd-kit/core' | ||
import { SortableContext, rectSortingStrategy, sortableKeyboardCoordinates, arrayMove } from '@dnd-kit/sortable' | ||
import clx from 'classnames' | ||
|
||
import { SingleEntryForm } from 'app/editArea/[slug]/components/SingleEntryForm' | ||
import { ClimbType } from '@/js/types' | ||
import { climbLeftRightIndexComparator } from '@/js/utils' | ||
import { SortableClimbItem } from './SortableClimbItem' | ||
import { disciplinesToCodes, gradesToString } from '@/js/grades/util' | ||
import useUpdateClimbsCmd from '@/js/hooks/useUpdateClimbsCmd' | ||
import { IndividualClimbChangeInput } from '@/js/graphql/gql/contribs' | ||
import { GradeContexts } from '@/js/grades/Grade' | ||
|
||
/** | ||
* Sort climbs form | ||
*/ | ||
export const SortClimbsForm: React.FC<{ parentAreaId: string, climbs: ClimbType[], gradeContext: GradeContexts }> = ({ parentAreaId, climbs, gradeContext }) => { | ||
const router = useRouter() | ||
const session = useSession({ required: true }) | ||
const { updateClimbCmd } = useUpdateClimbsCmd({ | ||
parentId: parentAreaId, | ||
accessToken: session?.data?.accessToken as string | ||
}) | ||
|
||
const [enabled, setEnabled] = useState(false) | ||
const [climbHashmap, setClimbHashmap] = useState<Map<string, ClimbType>>(new Map()) | ||
const [sortedList, setSortedList] = useState<string[]>([]) | ||
|
||
useEffect(() => { | ||
reset() | ||
}, [enabled, climbs]) | ||
|
||
const reset = (): void => { | ||
const climbMap = new Map([...climbs].sort(climbLeftRightIndexComparator).map(climb => [climb.id, climb])) | ||
setClimbHashmap(climbMap) | ||
setSortedList(Array.from(climbMap.keys())) | ||
} | ||
|
||
const sensors = useSensors( | ||
useSensor(LibMouseSensor), | ||
useSensor(KeyboardSensor, { | ||
coordinateGetter: sortableKeyboardCoordinates | ||
}) | ||
) | ||
|
||
function handleDragEnd (event: DragEndEvent): void { | ||
const { active, over } = event | ||
if (active.id != null && over?.id != null && active.id !== over.id) { | ||
const oldIndex = sortedList.indexOf(active.id as string) | ||
const newIndex = sortedList.indexOf(over.id as string) | ||
const newList = arrayMove(sortedList, oldIndex, newIndex) | ||
setSortedList(newList) | ||
} | ||
} | ||
|
||
const submitHandler = async (): Promise<void> => { | ||
const updatedList = sortedList.reduce<IndividualClimbChangeInput[]>((acc, curr, index) => { | ||
const climb = climbHashmap.get(curr) | ||
if (climb == null) return acc | ||
const { id, metadata: { leftRightIndex } } = climb | ||
if (leftRightIndex !== index) { | ||
acc.push({ id, leftRightIndex: index }) | ||
} | ||
return acc | ||
}, []) | ||
await updateClimbCmd({ parentId: parentAreaId, changes: updatedList }) | ||
router.refresh() | ||
} | ||
|
||
return ( | ||
<DndContext | ||
sensors={sensors} | ||
collisionDetection={closestCenter} | ||
onDragEnd={handleDragEnd} | ||
> | ||
<SortableContext | ||
items={sortedList} | ||
strategy={rectSortingStrategy} | ||
> | ||
<SingleEntryForm<{ dummyField: string }> | ||
title='Sort climbs' | ||
validationMode='onSubmit' | ||
alwaysEnableSubmit | ||
submitHandler={submitHandler} | ||
helperText='Drag and drop climbs to set their left-to-right order.' | ||
> | ||
<div> | ||
<button | ||
type='button' | ||
className='btn btn-primary btn-outline' | ||
onClick={() => { | ||
setEnabled(curr => !curr) | ||
}} | ||
> | ||
{enabled ? 'Cancel' : 'Sort climbs'} | ||
</button> | ||
</div> | ||
{enabled && | ||
( | ||
<div className='border-t'> | ||
<p className='mt-4'>Climbs are ordered from left to right. Drag and drop climbs to change their position.</p> | ||
|
||
<table className='mt-4 table-auto border-separate border-spacing-y-2'> | ||
|
||
<thead> | ||
<tr> | ||
<th className='border-b border-base-300 py-2'>Position</th> | ||
<th className='border-b border-base-300 py-2'>Name</th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
{sortedList.map((climbId, index) => { | ||
const climb = climbHashmap.get(climbId) | ||
if (climb == null) return null | ||
const { id, name, type, grades, metadata: { leftRightIndex } } = climb | ||
const gradeStr = gradesToString(grades, type, gradeContext) | ||
const hasChanged = leftRightIndex !== index | ||
return ( | ||
<SortableClimbItem | ||
key={id} | ||
id={id} | ||
className='' | ||
> | ||
<> | ||
<td className={clx('text-center px-2 border-l-4', hasChanged ? 'italic font-medium border-l-warning' : 'text-secondary border-transparent')}> | ||
{index + 1} | ||
</td> | ||
<td className='max-w-sm flex gap-x-4 items-center nowrap overflow-hidden'> | ||
<span className='pl-2 truncate max-w-xs overflow-hidden font-medium'>{name}</span> | ||
<span className='text-secondary'>{gradeStr} {disciplinesToCodes(type)}</span> | ||
</td> | ||
</> | ||
</SortableClimbItem> | ||
) | ||
})} | ||
</tbody> | ||
|
||
<tfoot> | ||
<tr> | ||
<td className='border-t border-base-300 py-2' /> | ||
<td className='border-t border-base-300 text-right py-2'> | ||
<button className='btn btn-sm' onClick={reset} type='button'>Reset</button> | ||
</td> | ||
</tr> | ||
</tfoot> | ||
</table> | ||
|
||
</div> | ||
)} | ||
</SingleEntryForm> | ||
</SortableContext> | ||
</DndContext> | ||
) | ||
} |
40 changes: 40 additions & 0 deletions
40
src/app/editArea/[slug]/manageClimbs/components/sorting/SortableClimbItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { CSS } from '@dnd-kit/utilities' | ||
import { useSortable } from '@dnd-kit/sortable' | ||
import clx from 'classnames' | ||
|
||
interface SortableItemProps { | ||
id: string | ||
children: JSX.Element | ||
className: string | ||
} | ||
|
||
export const SortableClimbItem: React.FC<SortableItemProps> = ({ id, className, children }: SortableItemProps) => { | ||
const { | ||
attributes, | ||
listeners, | ||
setNodeRef, | ||
transform, | ||
transition, | ||
isDragging | ||
} = useSortable({ id }) | ||
|
||
const style = { | ||
transform: CSS.Transform.toString(transform), | ||
transition | ||
} | ||
|
||
return ( | ||
<tr | ||
ref={setNodeRef} | ||
style={style} | ||
{...attributes} | ||
{...listeners} | ||
className={ | ||
clx(className, | ||
isDragging ? 'text-neutral-content bg-neutral cursor-move' : 'hover:bg-base-200') | ||
} | ||
> | ||
{children} | ||
</tr> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.