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

Fix card bugs #37

Merged
merged 4 commits into from
Dec 30, 2023
Merged
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
38 changes: 26 additions & 12 deletions src/components/Card/Content.jsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useDispatch } from 'react-redux';

import * as actions from '../../store/actionIndex';
import ContentTextarea from '../UI/Inputs/ContentTextarea';
import * as hooks from './hooks';

import './index.scss';

const Content = ({
cardId,
setEditingCard,
text,
}) => {
const dispatch = useDispatch();

// STORE SELECTORS
const cardText = useSelector(state => state.campaignData.present.cards[cardId].content.text);

// FUNCTIONS

// STYLES
const {
readOnly,
contentRef,
contentValue,
changeContentValue,
beginContentEdit,
endContentEdit,
} = hooks.useContentHooks({
saveNewValue: (value) => dispatch(actions.updCardText(cardId, value)),
setEditingCard,
value: text,
});

return (
<div className="content">
<ContentTextarea className="text"
value={cardText}
saveValue={v => dispatch(actions.updCardText(cardId, v))}
setEditingParent={setEditingCard}
<textarea
className='text'
onBlur={endContentEdit}
onChange={(e) => changeContentValue(e.target.value)}
onClick={beginContentEdit}
onDragOver={(e) => e.preventDefault()}
onWheel={(e) => e.stopPropagation()}
placeholder="Fill me in!"
readOnly={readOnly}
ref={contentRef}
value={contentValue}
/>
</div>
);
Expand Down
7 changes: 2 additions & 5 deletions src/components/Card/Title.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ const Title = ({
endTitleEdit,
handleTitleKeyPress,
} = hooks.useTitleHooks({
color,
saveNewValue: (value) => dispatch(actions.updCardTitle(cardId, value)),
setIsEditingParent: setEditingCard,
setEditingCard,
value: title,
});

Expand Down Expand Up @@ -70,11 +69,9 @@ const Title = ({
onKeyDown={handleTitleKeyPress}
readOnly={readOnly}
ref={titleRef}
required
size=''
title={titleValue}
type='text'
value={titleValue ?? ''}
value={titleValue}
/>
</div>
<button
Expand Down
101 changes: 70 additions & 31 deletions src/components/Card/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import RedTrashIcon from '../../assets/icons/red-trash.png';
import { PopupKeys } from '../Popup/PopupKey';

export const useTitleHooks = ({
color,
saveNewValue,
setIsEditingParent,
setEditingCard,
value,
}) => {
const [ titleValue, setTitleValue ] = useState('');
Expand All @@ -20,18 +19,32 @@ export const useTitleHooks = ({

useEffect(() => {
setTitleValue(value);
}, [setTitleValue, value]);
}, [value]);

const beginTitleEdit = () => {
if (!isEditing) {
setIsEditing(true);
setEditingCard(true);
titleRef.current.focus();
titleRef.current.setSelectionRange(
titleRef.current.value.length,
titleRef.current.value.length,
);
}
};

const endEdit = () => {
const endTitleEdit = () => {
if (isEditing) {
document.getSelection().removeAllRanges();
if (titleValue !== value) {
saveNewValue(titleValue);
}
saveNewValue(titleValue);
setIsEditing(false);
if (setIsEditingParent) {
setIsEditingParent(false);
}
setEditingCard(false);
}
};

const handleTitleKeyPress = (event) => {
if(event.key === 'Enter' || event.key === 'Tab') {
endTitleEdit();
}
};

Expand All @@ -41,27 +54,9 @@ export const useTitleHooks = ({
readOnly: !isEditing,
inputClassName: isEditing ? 'editing' : '',
changeTitleValue: (newValue) => setTitleValue(newValue),
beginTitleEdit: () => {
if (!isEditing) {
setIsEditing(true);
if(setIsEditingParent) {
setIsEditingParent(true);
}
titleRef.current.focus();
titleRef.current.setSelectionRange(
titleRef.current.value.length,
titleRef.current.value.length,
);
}
},
endTitleEdit: endEdit,
handleTitleKeyPress: (event) => {
if (isEditing) {
if(event.key === 'Enter' || event.key === 'Tab') {
endEdit();
}
}
},
beginTitleEdit,
endTitleEdit,
handleTitleKeyPress,
};
};

Expand Down Expand Up @@ -137,3 +132,47 @@ export const useOptionsDropdownHooks = ({
closeOptionsDropdown: () => setIsOptionDropdownOpen(false),
};
};

export const useContentHooks = ({
saveNewValue,
setEditingCard,
value,
}) => {
const [ contentValue, setContentValue ] = useState('');
const [ isEditing, setIsEditing ] = useState(false);
const contentRef = useRef();

useEffect(() => {
setContentValue(value);
}, [value]);

const beginContentEdit = () => {
if (!isEditing) {
setIsEditing(true);
setEditingCard(true);
contentRef.current.focus();
contentRef.current.setSelectionRange(
contentRef.current.value.length,
contentRef.current.value.length,
);
}
};

const endContentEdit = () => {
if (isEditing) {
document.getSelection().removeAllRanges();
saveNewValue(contentValue);
setIsEditing(false);
setEditingCard(false);
}
};

return {
contentRef,
contentValue,
readOnly: !isEditing,
changeContentValue: (newValue) => setContentValue(newValue),
beginContentEdit,
endContentEdit,
};
};
14 changes: 10 additions & 4 deletions src/components/Card/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ import React, { useState, useRef } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Rnd } from 'react-rnd';

import './index.scss';
import { GRID } from '../../shared/constants/grid';

import useOutsideClick from '../../utils/useOutsideClick';
import * as actions from '../../store/actionIndex';

import Title from './Title';
import Content from './Content';

import './index.scss';
import { GRID } from '../../shared/constants/grid';

export const Card = ({
cardId,
toolMenuRef,
cardAnimation,
setCardAnimation,
}) => {
// TODO move logic into hooks.js file
const dispatch = useDispatch();

// STATES
Expand All @@ -32,6 +33,7 @@ export const Card = ({
const cardSize = useSelector(state => state.campaignData.present.cards[cardId].views[activeViewId].size);
const cardColor = useSelector(state => state.campaignData.present.cards[cardId].color);
const cardTitle = useSelector(state => state.campaignData.present.cards[cardId].title);
const cardText = useSelector(state => state.campaignData.present.cards[cardId].content.text);

// REFS
const cardRef = useRef();
Expand Down Expand Up @@ -130,7 +132,11 @@ export const Card = ({
setEditingCard={setEditingCard}
title={cardTitle}
/>
<Content cardId={cardId} setEditingCard={setEditingCard} />
<Content
cardId={cardId}
setEditingCard={setEditingCard}
text={cardText}
/>
</div>
</Rnd>
);
Expand Down
3 changes: 2 additions & 1 deletion src/components/Card/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,18 @@ $title-height: 32px;

button {
padding: 0;
background-color: transparent;
justify-content: center;
display: flex;
}

.color-btn {
background-color: transparent;
img { padding-top: 2px; }
&:hover > img { content: url('../../assets/icons/rounded-square-hover.svg'); }
}

.dropdown-btn {
background-color: transparent;
img { transition-duration: .5s; }
&:hover > img { transform: translateY(3px); }
}
Expand Down
Loading