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

Emojis order 271 #291

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import StyledDialog from 'shared/components/StyledDialog/StyledDialog';
import Picker from '@emoji-mart/react';
import Emoji from 'features/surveys/components/Emoji/Emoji';
import { EmojiObject } from 'features/surveys/features/SurveyCreator/components/EmojiPicker/EmojiObject';
import { DraggableProvidedDragHandleProps } from 'react-beautiful-dnd';

interface EmojiPickerProps {
index?: number;
Expand All @@ -24,6 +25,7 @@ interface EmojiPickerProps {
) => void;
onEmoteRemove?: (idx: number, questionIndex: number) => void;
questionIndex: number;
dragHandleProps?: DraggableProvidedDragHandleProps | null | undefined;
}

function EmojiPicker({
Expand All @@ -34,6 +36,7 @@ function EmojiPicker({
onEmoteAdd,
onEmoteRemove,
questionIndex,
dragHandleProps,
}: EmojiPickerProps) {
const [displayPicker, setDisplayPicker] = useState(false);

Expand All @@ -60,6 +63,16 @@ function EmojiPicker({
<PlusSmIcon className="w-[22px]" />
)}
</button>

{!addEmoji && (
<div
className="mt-1 w-14 cursor-pointer rounded-md border bg-zinc-50 shadow-sm hover:scale-95"
{...dragHandleProps}
>
m
</div>
)}

{onEmoteRemove && (
<Button
onClick={() => onEmoteRemove(index, questionIndex)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import EmojiPicker from 'features/surveys/features/SurveyCreator/components/EmojiPicker/EmojiPicker';
import { MAX_EMOJIS, MIN_EMOJIS } from 'shared/constants/emojisConfig';
import { useSurveyCreatorContext } from 'features/surveys/features/SurveyCreator/managers/createSurveyManager/context';
import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';
import clsx from 'clsx';

interface EmojiQuestionBlockProps {
pack: string[];
Expand All @@ -11,31 +13,62 @@ export default function EmojiQuestionBlock({
pack,
questionIndex,
}: EmojiQuestionBlockProps) {
const { handleAddingNewOption, handleOptionChange, handleOptionRemove } =
useSurveyCreatorContext();
const {
handleAddingNewOption,
handleOptionChange,
handleOptionRemove,
onDragEmojiEnd,
} = useSurveyCreatorContext();

return (
<div
className="mb-3 mt-1"
style={{
display: 'grid',
gridTemplateColumns: 'repeat(auto-fit, 58px)',
display: 'flex',
justifyContent: 'space-between',
gridGap: '8px',
flexGrow: '1',
}}
>
{pack.map((emote, idx) => (
<EmojiPicker
key={emote}
index={idx}
pickedEmoji={emote}
questionIndex={questionIndex}
onEmotePick={handleOptionChange}
onEmoteRemove={
pack.length > MIN_EMOJIS ? handleOptionRemove : undefined
}
/>
))}
<DragDropContext
onDragEnd={(result) => onDragEmojiEnd(result, questionIndex)}
>
<Droppable droppableId="droppable-emoji" direction="horizontal">
{(provided, snapshot) => (
<div
{...provided.droppableProps}
ref={provided.innerRef}
className={clsx('flex flex-wrap', snapshot.isDraggingOver && '')}
>
{pack.map((emote, index) => (
<Draggable key={emote} draggableId={emote} index={index}>
{(provided, snapshot) => (
<div
className={clsx('mb-3 mr-5', snapshot.isDragging && '')}
ref={provided.innerRef}
{...provided.draggableProps}
style={provided.draggableProps.style}
>
<EmojiPicker
key={emote}
index={index}
pickedEmoji={emote}
questionIndex={questionIndex}
onEmotePick={handleOptionChange}
onEmoteRemove={
pack.length > MIN_EMOJIS
? handleOptionRemove
: undefined
}
dragHandleProps={provided.dragHandleProps}
/>
</div>
)}
</Draggable>
))}
{provided.placeholder}
</div>
)}
</Droppable>
</DragDropContext>
{pack.length < MAX_EMOJIS && (
<EmojiPicker
questionIndex={questionIndex}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function QuestionsSection() {

<DragDropContext onDragEnd={onDragQuestionEnd}>
{isBrowser ? (
<Droppable droppableId="droppable">
<Droppable droppableId="droppable-question">
{(provided, snapshot) => (
<div
{...provided.droppableProps}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,30 @@ export const useCreateSurveyManager = (initialData?: SurveyWithQuestions) => {
reorderQuestion(result.source.index, result.destination.index);
};

const reorderEmoji = (
startIndex: number,
endIndex: number,
questionIndex: number
) => {
const newOrderedEmojis = questions[questionIndex].options || [];

const [removed] = newOrderedEmojis.splice(startIndex, 1);
newOrderedEmojis.splice(endIndex, 0, removed);

const copyQuestions = questions;
copyQuestions[questionIndex].options = newOrderedEmojis;

setQuestions(copyQuestions);
};

const onDragEmojiEnd = (result: DropResult, questionIndex: number) => {
if (!result.destination) {
return;
}

reorderEmoji(result.source.index, result.destination.index, questionIndex);
};

return {
title,
error,
Expand All @@ -361,6 +385,7 @@ export const useCreateSurveyManager = (initialData?: SurveyWithQuestions) => {
confirmEditSurvey,
discardChanges,
onDragQuestionEnd,
onDragEmojiEnd,
};
};

Expand Down
Loading