-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ajoute le drag and drop sur les étapes de la fiche action
- Loading branch information
Showing
6 changed files
with
349 additions
and
52 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
138 changes: 138 additions & 0 deletions
138
...tions.react/src/app/pages/collectivite/PlansActions/FicheAction/etapes/etapes-context.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,138 @@ | ||
import { createContext, ReactNode, useContext, useReducer } from 'react'; | ||
|
||
import { RouterInput, RouterOutput } from '@/api/utils/trpc/client'; | ||
|
||
type Action = | ||
| { | ||
type: 'create'; | ||
payload: RouterOutput['plans']['fiches']['etapes']['upsert']; | ||
} | ||
| { | ||
type: 'toggleRealise'; | ||
payload: { | ||
etapeId: number; | ||
}; | ||
} | ||
| { | ||
type: 'updateNom'; | ||
payload: { | ||
etapeId: number; | ||
nom: string; | ||
}; | ||
} | ||
| { | ||
type: 'updateOrder'; | ||
payload: { | ||
etapeId: number; | ||
oldOrder: number; | ||
newOrder: number; | ||
}; | ||
} | ||
| { | ||
type: 'delete'; | ||
payload: RouterInput['plans']['fiches']['etapes']['delete']; | ||
}; | ||
|
||
type State = { | ||
etapes: RouterOutput['plans']['fiches']['etapes']['list']; | ||
}; | ||
|
||
type Dispatch = (action: Action) => void; | ||
|
||
const StateContext = createContext<State | undefined>(undefined); | ||
const DispatchContext = createContext<Dispatch | undefined>(undefined); | ||
|
||
const panelReducer = (state: State, action: Action) => { | ||
const { etapes } = state; | ||
switch (action.type) { | ||
case 'create': { | ||
const { payload: newEtape } = action; | ||
return { | ||
...state, | ||
etapes: [...etapes, newEtape], | ||
}; | ||
} | ||
case 'toggleRealise': { | ||
const { etapeId } = action.payload; | ||
return { | ||
...state, | ||
etapes: etapes.map((etape) => | ||
etapeId === etape.id ? { ...etape, realise: !etape.realise } : etape | ||
), | ||
}; | ||
} | ||
case 'updateNom': { | ||
const { etapeId, nom: newNom } = action.payload; | ||
return { | ||
...state, | ||
etapes: etapes.map((etape) => | ||
etapeId === etape.id ? { ...etape, nom: newNom } : etape | ||
), | ||
}; | ||
} | ||
case 'updateOrder': { | ||
const { etapeId, oldOrder, newOrder } = action.payload; | ||
return { | ||
...state, | ||
etapes: [ | ||
...etapes.map((e) => { | ||
if (e.id === etapeId) { | ||
return { ...e, ordre: newOrder }; | ||
} | ||
if (e.ordre > oldOrder && e.ordre <= newOrder) { | ||
return { ...e, ordre: e.ordre - 1 }; | ||
} | ||
if (e.ordre < oldOrder && e.ordre >= newOrder) { | ||
return { ...e, ordre: e.ordre + 1 }; | ||
} | ||
return e; | ||
}), | ||
].sort((a, b) => a.ordre - b.ordre), | ||
}; | ||
} | ||
case 'delete': { | ||
const { etapeId } = action.payload; | ||
return { | ||
...state, | ||
etapes: etapes.filter((etape) => etape.id !== etapeId), | ||
}; | ||
} | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export const EtapesProvider = ({ | ||
initialState, | ||
children, | ||
}: { | ||
initialState: State; | ||
children: ReactNode; | ||
}) => { | ||
const [state, dispatch] = useReducer(panelReducer, initialState); | ||
return ( | ||
<StateContext.Provider value={state}> | ||
<DispatchContext.Provider value={dispatch}> | ||
{children} | ||
</DispatchContext.Provider> | ||
</StateContext.Provider> | ||
); | ||
}; | ||
|
||
export const useEtapesState = () => { | ||
const context = useContext(StateContext); | ||
|
||
if (context === undefined) { | ||
throw new Error('usePanelSate must be used within a EtapesProvider'); | ||
} | ||
return context; | ||
}; | ||
|
||
export const useEtapesDispatch = () => { | ||
const context = useContext(DispatchContext); | ||
|
||
if (context === undefined) { | ||
throw new Error('usePanelDispatch must be used within a EtapesProvider'); | ||
} | ||
return context; | ||
}; |
83 changes: 83 additions & 0 deletions
83
...nsitions.react/src/app/pages/collectivite/PlansActions/FicheAction/etapes/etapes-list.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,83 @@ | ||
import { | ||
closestCenter, | ||
DndContext, | ||
DragEndEvent, | ||
PointerSensor, | ||
useSensor, | ||
useSensors, | ||
} from '@dnd-kit/core'; | ||
|
||
import { | ||
SortableContext, | ||
verticalListSortingStrategy, | ||
} from '@dnd-kit/sortable'; | ||
|
||
import { RouterOutput } from '@/api/utils/trpc/client'; | ||
|
||
import { Etape, useUpsertEtape } from './etape'; | ||
import { useEtapesDispatch } from './etapes-context'; | ||
|
||
type Etapes = RouterOutput['plans']['fiches']['etapes']['list']; | ||
|
||
type Props = { | ||
ficheId: number; | ||
etapes: Etapes; | ||
isReadonly: boolean; | ||
}; | ||
|
||
const EtapesList = ({ ficheId, etapes, isReadonly }: Props) => { | ||
const dispatchEtapes = useEtapesDispatch(); | ||
const { mutateAsync: updateEtapeOrder } = useUpsertEtape(); | ||
|
||
const sensors = useSensors( | ||
useSensor(PointerSensor, { | ||
activationConstraint: { | ||
distance: 20, | ||
}, | ||
}) | ||
); | ||
|
||
const handleDragEnd = async (event: DragEndEvent) => { | ||
const { active, over } = event; | ||
if (!(over && active.id !== over.id)) { | ||
return; | ||
} | ||
|
||
const activeEtape = etapes.find((etape) => etape.id === active.id); | ||
const overEtape = etapes.find((etape) => etape.id === over.id); | ||
|
||
if (activeEtape && overEtape) { | ||
updateEtapeOrder({ | ||
id: activeEtape.id, | ||
ficheId, | ||
ordre: overEtape.ordre, | ||
}); | ||
dispatchEtapes({ | ||
type: 'updateOrder', | ||
payload: { | ||
etapeId: activeEtape.id, | ||
oldOrder: activeEtape.ordre, | ||
newOrder: overEtape.ordre, | ||
}, | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<DndContext | ||
sensors={sensors} | ||
collisionDetection={closestCenter} | ||
onDragEnd={handleDragEnd} | ||
> | ||
<SortableContext items={etapes} strategy={verticalListSortingStrategy}> | ||
<div className="flex flex-col gap-1"> | ||
{etapes.map((etape) => ( | ||
<Etape key={etape.id} etape={etape} isReadonly={isReadonly} /> | ||
))} | ||
</div> | ||
</SortableContext> | ||
</DndContext> | ||
); | ||
}; | ||
|
||
export default EtapesList; |
Oops, something went wrong.