Skip to content
New issue

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

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

Already on GitHub? # to your account

Clipboard fix & pin icon #9

Merged
merged 2 commits into from
Sep 24, 2021
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
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.css
*.css
CHANGELOG.md
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### v1.1 (2021-09-24)
- Added pin icon directly to snippet card ([#4](https://github.com/pawelmalak/snippet-box/issues/4))
- Fixed issue with copying snippets ([#6](https://github.com/pawelmalak/snippet-box/issues/6))

### v1.0 (2021-09-23)
Initial release
5 changes: 5 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@types/react": "^17.0.21",
"@types/react-dom": "^17.0.9",
"axios": "^0.21.4",
"clipboard-copy": "^4.0.1",
"dayjs": "^1.10.7",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
8 changes: 4 additions & 4 deletions client/src/components/Snippets/SnippetCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Snippet } from '../../typescript/interfaces';
import { dateParser, badgeColor } from '../../utils';
import { Badge, Button, Card } from '../UI';
import { SnippetsContext } from '../../store';
import Icon from '@mdi/react';
import { mdiPin } from '@mdi/js';
import copy from 'clipboard-copy';
import { SnippetPin } from './SnippetPin';

interface Props {
snippet: Snippet;
Expand All @@ -17,15 +17,15 @@ export const SnippetCard = (props: Props): JSX.Element => {
const { setSnippet } = useContext(SnippetsContext);

const copyHandler = () => {
navigator.clipboard.writeText(code);
copy(code);
};

return (
<Card classes='h-100' bodyClasses='d-flex flex-column'>
{/* TITLE */}
<h5 className='card-title d-flex align-items-center justify-content-between'>
{title}
{isPinned ? <Icon path={mdiPin} size={0.8} color='#212529' /> : ''}
<SnippetPin id={id} isPinned={isPinned} />
</h5>

<h6 className='card-subtitle mb-2 text-muted'>
Expand Down
50 changes: 17 additions & 33 deletions client/src/components/Snippets/SnippetDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useContext } from 'react';
import { Link } from 'react-router-dom';
import { useHistory } from 'react-router-dom';
import { SnippetsContext } from '../../store';
import { Snippet } from '../../typescript/interfaces';
import { dateParser } from '../../utils';
import { Button, Card } from '../UI';
import Icon from '@mdi/react';
import { mdiPin } from '@mdi/js';
import copy from 'clipboard-copy';
import { SnippetPin } from './SnippetPin';

interface Props {
snippet: Snippet;
Expand All @@ -23,21 +23,22 @@ export const SnippetDetails = (props: Props): JSX.Element => {
isPinned
} = props.snippet;

const { deleteSnippet, toggleSnippetPin, setSnippet } =
useContext(SnippetsContext);
const history = useHistory();

const { deleteSnippet, setSnippet } = useContext(SnippetsContext);

const creationDate = dateParser(createdAt);
const updateDate = dateParser(updatedAt);

const copyHandler = () => {
navigator.clipboard.writeText(code);
copy(code);
};

return (
<Card>
<h5 className='card-title d-flex align-items-center justify-content-between'>
{title}
{isPinned ? <Icon path={mdiPin} size={0.8} color='#212529' /> : ''}
<SnippetPin id={id} isPinned={isPinned} />
</h5>
<p>{description}</p>

Expand All @@ -58,33 +59,22 @@ export const SnippetDetails = (props: Props): JSX.Element => {
<span>Last updated</span>
<span>{updateDate.relative}</span>
</div>

<hr />

{/* ACTIONS */}
<div className='d-flex justify-content-between'>
<Link
to={{
pathname: `/editor/${id}`,
state: { from: window.location.pathname }
}}
>
<Button
text='Edit'
color='dark'
small
outline
classes='me-3'
handler={() => setSnippet(id)}
/>
</Link>
<div className='d-grid g-2' style={{ rowGap: '10px' }}>
<Button
text={`${isPinned ? 'Unpin snippet' : 'Pin snippet'}`}
text='Edit'
color='dark'
small
outline
handler={() => toggleSnippetPin(id)}
classes='me-3'
handler={() => {
setSnippet(id);
history.push({
pathname: `/editor/${id}`,
state: { from: window.location.pathname }
});
}}
/>
<Button
text='Delete'
Expand All @@ -93,12 +83,6 @@ export const SnippetDetails = (props: Props): JSX.Element => {
outline
handler={() => deleteSnippet(id)}
/>
</div>

<hr />

{/* COPY */}
<div className='d-grid'>
<Button text='Copy code' color='dark' small handler={copyHandler} />
</div>
</Card>
Expand Down
24 changes: 24 additions & 0 deletions client/src/components/Snippets/SnippetPin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useContext } from 'react';
import { SnippetsContext } from '../../store';
import Icon from '@mdi/react';
import { mdiPin, mdiPinOutline } from '@mdi/js';

interface Props {
id: number;
isPinned: boolean;
}

export const SnippetPin = (props: Props): JSX.Element => {
const { toggleSnippetPin } = useContext(SnippetsContext);
const { id, isPinned } = props;

return (
<div onClick={() => toggleSnippetPin(id)} className='cursor-pointer'>
{isPinned ? (
<Icon path={mdiPin} size={0.8} color='#212529' />
) : (
<Icon path={mdiPinOutline} size={0.8} color='#9a9a9a' />
)}
</div>
);
};
21 changes: 14 additions & 7 deletions client/src/store/SnippetsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const SnippetsContext = createContext<Context>({
getSnippetById: (id: number) => {},
setSnippet: (id: number) => {},
createSnippet: (snippet: NewSnippet) => {},
updateSnippet: (snippet: NewSnippet, id: number) => {},
updateSnippet: (snippet: NewSnippet, id: number, isLocal?: boolean) => {},
deleteSnippet: (id: number) => {},
toggleSnippetPin: (id: number) => {},
countSnippets: () => {}
Expand Down Expand Up @@ -81,7 +81,11 @@ export const SnippetsContextProvider = (props: Props): JSX.Element => {
.catch(err => redirectOnError());
};

const updateSnippet = (snippet: NewSnippet, id: number): void => {
const updateSnippet = (
snippet: NewSnippet,
id: number,
isLocal?: boolean
): void => {
axios
.put<Response<Snippet>>(`/api/snippets/${id}`, snippet)
.then(res => {
Expand All @@ -92,10 +96,13 @@ export const SnippetsContextProvider = (props: Props): JSX.Element => {
...snippets.slice(oldSnippetIdx + 1)
]);
setCurrentSnippet(res.data.data);
history.push({
pathname: `/snippet/${res.data.data.id}`,
state: { from: '/snippets' }
});

if (!isLocal) {
history.push({
pathname: `/snippet/${res.data.data.id}`,
state: { from: '/snippets' }
});
}
})
.catch(err => redirectOnError());
};
Expand All @@ -121,7 +128,7 @@ export const SnippetsContextProvider = (props: Props): JSX.Element => {
const snippet = snippets.find(s => s.id === id);

if (snippet) {
updateSnippet({ ...snippet, isPinned: !snippet.isPinned }, id);
updateSnippet({ ...snippet, isPinned: !snippet.isPinned }, id, true);
}
};

Expand Down
2 changes: 1 addition & 1 deletion client/src/typescript/interfaces/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface Context {
getSnippetById: (id: number) => void;
setSnippet: (id: number) => void;
createSnippet: (snippet: NewSnippet) => void;
updateSnippet: (snippet: NewSnippet, id: number) => void;
updateSnippet: (snippet: NewSnippet, id: number, isLocal?: boolean) => void;
deleteSnippet: (id: number) => void;
toggleSnippetPin: (id: number) => void;
countSnippets: () => void;
Expand Down