Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Drag & drop for re-ordering event types (without any external library) #14719

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions apps/web/modules/event-types/views/event-types-listing-view.tsx
Expand Up @@ -5,7 +5,7 @@ import { Trans } from "next-i18next";
import Link from "next/link";
import { usePathname, useRouter } from "next/navigation";
import type { FC } from "react";
import { memo, useEffect, useState } from "react";
import { memo, useEffect, useState, useRef } from "react";
import { z } from "zod";

import { useOrgBranding } from "@calcom/features/ee/organizations/context/provider";
Expand Down Expand Up @@ -277,7 +277,7 @@ export const EventTypeList = ({
},
});

async function moveEventType(index: number, increment: 1 | -1) {
async function moveEventType(index: number, increment: number) {
const newList = [...types.map(normalizeEventType)];

const type = types[index];
Expand Down Expand Up @@ -369,6 +369,17 @@ export const EventTypeList = ({
});

const [isNativeShare, setNativeShare] = useState(true);
const source = useRef<number>(0);
const destination = useRef<number>(0);

function handleOnDragEnd() {
const newIndex = destination.current;
const oldIndex = source.current;

if (newIndex === oldIndex) return;

moveEventType(oldIndex, newIndex - oldIndex);
}

useEffect(() => {
if (!navigator.share) {
Expand Down Expand Up @@ -403,8 +414,14 @@ export const EventTypeList = ({
const isChildrenManagedEventType =
type.metadata?.managedEventConfig !== undefined && type.schedulingType !== SchedulingType.MANAGED;
return (
<li key={type.id}>
<div className="hover:bg-muted flex w-full items-center justify-between transition">
<li
key={type.id}
draggable="true"
onDragStart={() => (source.current = index)}
onDragEnter={() => (destination.current = index)}
onDragEnd={handleOnDragEnd}
onDragOver={(e) => e.preventDefault()}>
<div className="hover:bg-muted flex w-full cursor-move items-center justify-between">
<div className="group flex w-full max-w-full items-center justify-between overflow-hidden px-4 py-4 sm:px-6">
{!(firstItem && firstItem.id === type.id) && (
<ArrowButton onClick={() => moveEventType(index, -1)} arrowDirection="up" />
Expand Down