create delete button from /snippets

main
Mike Fix 5 years ago
parent d693a54437
commit 1372a4066d

@ -8,6 +8,7 @@ const Button = ({
onClick = () => {}, onClick = () => {},
background = COLORS.BLACK, background = COLORS.BLACK,
color = COLORS.SECONDARY, color = COLORS.SECONDARY,
textColor,
hoverBackground = COLORS.HOVER, hoverBackground = COLORS.HOVER,
hoverColor, hoverColor,
disabled, disabled,
@ -33,7 +34,7 @@ const Button = ({
display: ${display || 'flex'}; display: ${display || 'flex'};
flex: ${flex}; flex: ${flex};
background-color: ${background}; background-color: ${background};
color: ${color}; color: ${textColor || color};
box-shadow: ${border ? `inset 0px 0px 0px ${selected ? 2 : 1}px ${color}` : 'initial'}; box-shadow: ${border ? `inset 0px 0px 0px ${selected ? 2 : 1}px ${color}` : 'initial'};
cursor: ${disabled ? 'not-allowed' : 'pointer'}; cursor: ${disabled ? 'not-allowed' : 'pointer'};
outline: none; outline: none;

@ -0,0 +1,22 @@
import React from 'react'
import Button from './Button'
export default function ConfirmButton(props) {
const [confirmed, setConfirmed] = React.useState(false)
return (
<Button
{...props}
onClick={e => {
if (confirmed) {
props.onClick(e)
setConfirmed(false)
} else {
setConfirmed(true)
}
}}
onBlur={() => setConfirmed(false)}
>
{confirmed ? 'Are you sure?' : props.children}
</Button>
)
}

@ -3,31 +3,11 @@ import { useAsyncCallback, useOnline } from '@dawnlabs/tacklebox'
import Button from './Button' import Button from './Button'
import Toolbar from './Toolbar' import Toolbar from './Toolbar'
import ConfirmButton from './ConfirmButton'
import { useAuth } from './AuthContext' import { useAuth } from './AuthContext'
import { COLORS } from '../lib/constants' import { COLORS } from '../lib/constants'
function ConfirmButton(props) {
const [confirmed, setConfirmed] = React.useState(false)
return (
<Button
{...props}
onClick={e => {
if (confirmed) {
props.onClick(e)
setConfirmed(false)
} else {
setConfirmed(true)
}
}}
onBlur={() => setConfirmed(false)}
>
{confirmed ? 'Are you sure?' : props.children}
</Button>
)
}
function DeleteButton(props) { function DeleteButton(props) {
const [onClick, { loading }] = useAsyncCallback(props.onClick) const [onClick, { loading }] = useAsyncCallback(props.onClick)

@ -7,6 +7,7 @@ import { useAsyncCallback } from '@dawnlabs/tacklebox'
import Button from '../components/Button' import Button from '../components/Button'
import LoginButton from '../components/LoginButton' import LoginButton from '../components/LoginButton'
import ConfirmButton from '../components/ConfirmButton'
import { useAuth } from '../components/AuthContext' import { useAuth } from '../components/AuthContext'
import { useAPI } from '../components/ApiContext' import { useAPI } from '../components/ApiContext'
@ -29,8 +30,6 @@ function Snippet(props) {
const config = { ...DEFAULT_SETTINGS, ...props, fontSize: '2px', windowControls: false } const config = { ...DEFAULT_SETTINGS, ...props, fontSize: '2px', windowControls: false }
return ( return (
<Link prefetch={false} href={`/${props.id}`}>
<a href={`/${props.id}`}>
<div className="snippet"> <div className="snippet">
<div className="column"> <div className="column">
<div className="carbon-container"> <div className="carbon-container">
@ -43,6 +42,34 @@ function Snippet(props) {
Edited {formatDistanceToNow(correctTimestamp(props.updatedAt), { addSuffix: true })} Edited {formatDistanceToNow(correctTimestamp(props.updatedAt), { addSuffix: true })}
</div> </div>
</div> </div>
<div className="overlay">
<Link prefetch={false} href={`/${props.id}`}>
<Button
large
border
center
flex="unset"
margin="0 auto 1.5rem"
padding="0.5rem 16px"
color="#fff"
>
Open
</Button>
</Link>
<ConfirmButton
large
border
center
flex="unset"
margin="0 auto"
padding="0.5rem 20px"
color="#fff"
textColor={COLORS.RED}
hoverColor={COLORS.RED}
onClick={props.deleteSnippet}
>
Delete
</ConfirmButton>
</div> </div>
<style jsx> <style jsx>
{` {`
@ -55,13 +82,11 @@ function Snippet(props) {
cursor: pointer !important; cursor: pointer !important;
overflow: hidden; overflow: hidden;
} }
.snippet:hover { .overlay {
background: ${COLORS.HOVER}; display: none;
}
.snippet:hover:after {
position: absolute; position: absolute;
content: '↗'; content: '↗';
display: flex; flex-direction: column;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
background: ${COLORS.HOVER}; background: ${COLORS.HOVER};
@ -73,6 +98,10 @@ function Snippet(props) {
font-size: 48px; font-size: 48px;
z-index: 999; z-index: 999;
} }
.snippet:hover .overlay {
display: flex;
background: ${COLORS.HOVER};
}
.column { .column {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@ -83,7 +112,7 @@ function Snippet(props) {
.id { .id {
position: absolute; position: absolute;
top: 0.25rem; top: 0.25rem;
right: 0.125rem; right: 0.25rem;
border-radius: 3px; border-radius: 3px;
background: ${COLORS.SECONDARY}; background: ${COLORS.SECONDARY};
color: ${COLORS.BLACK}; color: ${COLORS.BLACK};
@ -116,8 +145,7 @@ function Snippet(props) {
} }
`} `}
</style> </style>
</a> </div>
</Link>
) )
} }
@ -127,9 +155,9 @@ function ActionButton(props) {
border border
center center
margin="0.5rem" margin="0.5rem"
flex="0 0 266px" flex="unset"
color={COLORS.GRAY} color={COLORS.GRAY}
style={{ minHeight: 266 }} style={{ width: 266, minHeight: 266 }}
{...props} {...props}
/> />
) )
@ -164,6 +192,10 @@ function SnippetsPage() {
} }
}, [loadMore, page, user]) }, [loadMore, page, user])
function deleteSnippet(id) {
return api.snippet.delete(id).then(() => setSnippets(curr => curr.filter(s => s.id !== id)))
}
if (!user) { if (!user) {
return <LoginButton /> return <LoginButton />
} }
@ -171,7 +203,12 @@ function SnippetsPage() {
return ( return (
<div className="container"> <div className="container">
{snippets.map(snippet => ( {snippets.map(snippet => (
<Snippet key={snippet.id} {...snippet} loading={!mounted} /> <Snippet
key={snippet.id}
{...snippet}
loading={!mounted}
deleteSnippet={deleteSnippet.bind(null, snippet.id)}
/>
))} ))}
{snippets.length && previousRes && previousRes.length < 10 ? null : ( {snippets.length && previousRes && previousRes.length < 10 ? null : (
<ActionButton <ActionButton

Loading…
Cancel
Save