Skip to content

Commit

Permalink
Add drag and drop logic for recipe feed item -> card
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathonherbert committed Apr 29, 2024
1 parent bf6e2cf commit cb08835
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
4 changes: 2 additions & 2 deletions fronts-client/src/actions/Cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import noop from 'lodash/noop';
import { selectOpenParentFrontOfCard } from 'bundles/frontsUI';
import { getPageViewData } from 'actions/PageViewData';
import { startOptionsModal } from './OptionsModal';
import { getArticleEntitiesFromDrop } from 'util/card';
import { getCardEntitiesFromDrop } from 'util/card';
import {
RemoveActionCreator,
InsertActionCreator,
Expand Down Expand Up @@ -375,7 +375,7 @@ export const createArticleEntitiesFromDrop = (
): ThunkResult<Promise<Card | undefined>> => {
return async (dispatch, getState) => {
const isEdition = selectEditMode(getState()) === 'editions';
const [maybeCard, maybeExternalArticle] = await getArticleEntitiesFromDrop(
const [maybeCard, maybeExternalArticle] = await getCardEntitiesFromDrop(
drop,
isEdition,
dispatch
Expand Down
2 changes: 1 addition & 1 deletion fronts-client/src/components/feed/RecipeFeedItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface ComponentProps {

export const RecipeFeedItem = ({ recipe }: ComponentProps) => {
const handleDragStart = (event: React.DragEvent<HTMLDivElement>, dragNode: HTMLDivElement) => {
event.dataTransfer.setData('capi', JSON.stringify(recipe));
event.dataTransfer.setData('recipe', JSON.stringify(recipe));
if (dragNode) {
event.dataTransfer.setDragImage(
dragNode,
Expand Down
25 changes: 19 additions & 6 deletions fronts-client/src/util/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ import {
isGuardianUrl,
isValidURL,
} from 'util/url';
import {Recipe} from "../types/Recipe";

// Ideally we will convert this to a type. See
// https://trello.com/c/wIMDut8V/138-add-a-type-to-the-createcard-function-in-src-shared-util-cardts
const createCard = (
id: string,
isEdition: boolean,
isEditionsApp: boolean,
imageHide: boolean = false,
imageReplace: boolean = false,
imageCutoutReplace: boolean = false,
Expand All @@ -54,8 +55,8 @@ const createCard = (
...(imageCutoutReplace ? { imageCutoutReplace, imageCutoutSrc } : {}),
...(showByline ? { showByline } : {}),
...(showQuotedHeadline ? { showQuotedHeadline } : {}),
...(isEdition || showKickerCustom ? { showKickerCustom: true } : {}),
...(isEdition || showKickerCustom ? { customKicker } : {}),
...(isEditionsApp || showKickerCustom ? { showKickerCustom: true } : {}),
...(isEditionsApp || showKickerCustom ? { customKicker } : {}),
},
});

Expand Down Expand Up @@ -133,19 +134,25 @@ const cloneActiveImageMeta = ({ meta }: Card): CardMeta => {
};

/**
* Given a resource id, extract the appropriate entities -- an Card
* Given a resource id, extract the appropriate entities -- a Card
* and possibly an ExternalArticle. The resource id can be a few different things:
* - a article, tag or section (either the full URL or the ID)
* - a recipe for the Feast app
* - an external link.
*/
const getArticleEntitiesFromDrop = async (
const getCardEntitiesFromDrop = async (
drop: MappableDropType,
isEdition: boolean,
dispatch: Dispatch
): Promise<TArticleEntities> => {
if (drop.type === 'CAPI') {
return getArticleEntitiesFromFeedDrop(drop.data, isEdition);
}

if (drop.type === 'RECIPE') {
return getRecipeEntityFromFeedDrop(drop.data)
}

const droppedDataURL = drop.data.trim();
const resourceIdOrUrl = isGoogleRedirectUrl(droppedDataURL)
? getRelevantURLFromGoogleRedirectURL(droppedDataURL)
Expand Down Expand Up @@ -249,6 +256,12 @@ const getArticleEntitiesFromDrop = async (
return [];
};

const getRecipeEntityFromFeedDrop = (recipe: Recipe): [Card] => {
const card = createCard(recipe.id, false);

return [card];
}

const getArticleEntitiesFromFeedDrop = (
capiArticle: CapiArticle,
isEdition: boolean
Expand Down Expand Up @@ -333,7 +346,7 @@ export {
createCard,
cloneCard,
cloneActiveImageMeta,
getArticleEntitiesFromDrop,
getCardEntitiesFromDrop,
snapMetaWhitelist,
marketingParamsWhiteList,
};
16 changes: 13 additions & 3 deletions fronts-client/src/util/collectionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ThunkResult, Dispatch } from 'types/Store';
import { PosSpec } from 'lib/dnd';
import { insertCardWithCreate } from 'actions/Cards';
import { CapiArticle } from 'types/Capi';
import {Recipe} from "../types/Recipe";

export interface RefDrop {
type: 'REF';
Expand All @@ -13,14 +14,23 @@ export interface CAPIDrop {
data: CapiArticle;
}

export type MappableDropType = RefDrop | CAPIDrop;
export interface RecipeDrop {
type: 'RECIPE';
data: Recipe;
}

export type MappableDropType = RefDrop | CAPIDrop | RecipeDrop;

const dropToArticle = (e: React.DragEvent): MappableDropType | null => {
const dropToCard = (e: React.DragEvent): MappableDropType | null => {
const map = {
capi: (data: string): CAPIDrop => ({
type: 'CAPI',
data: JSON.parse(data),
}),
recipe: (data: string): RecipeDrop => ({
type: 'RECIPE',
data: JSON.parse(data)
}),
text: (url: string): RefDrop => ({ type: 'REF', data: url }),
};

Expand All @@ -40,7 +50,7 @@ const insertCardFromDropEvent = (
persistTo: 'collection' | 'clipboard'
): ThunkResult<void> => {
return (dispatch: Dispatch) => {
const dropType = dropToArticle(e);
const dropType = dropToCard(e);
if (!dropType) {
return;
}
Expand Down

0 comments on commit cb08835

Please sign in to comment.