Skip to content

Commit 36d8d6b

Browse files
committed
toggle edit view
1 parent 338b260 commit 36d8d6b

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/components/CardForm.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import { saveCard } from '../services/cardService'
33

4-
export function CardForm({ onSave }) {
4+
export function CardForm({ onSave, onCancel }) {
55
const [term, setTerm] = React.useState('')
66
const [definition, setDef] = React.useState('')
77

@@ -16,6 +16,7 @@ export function CardForm({ onSave }) {
1616
function clearForm() {
1717
setTerm('')
1818
setDef('')
19+
onCancel && typeof onCancel === 'function' && onCancel()
1920
}
2021

2122
function handleTermChange(event) {

src/components/CardPreview.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import React from 'react'
22
import { destroyCard } from '../services/cardService'
3+
import { CardForm } from './CardForm'
34

4-
export function CardPreview({ id, term, definition, onRemove }) {
5+
export function CardPreview({ onRemove, ...card }) {
6+
const [isEditMode, setIsEditMode] = React.useState(false)
7+
function handleToggleEdit() {
8+
setIsEditMode(current => !current)
9+
}
10+
return isEditMode ? (
11+
<CardForm onCancel={handleToggleEdit} />
12+
) : (
13+
<View {...card} onEdit={handleToggleEdit} onRemove={onRemove} />
14+
)
15+
}
16+
17+
function View({ id, term, definition, onEdit, onRemove }) {
518
const [isFront, setIsFront] = React.useState(true)
619
function handleCardFlip() {
720
setIsFront(current => !current)
@@ -22,7 +35,7 @@ export function CardPreview({ id, term, definition, onRemove }) {
2235
{isFront ? 'show back' : 'show front'}
2336
</button>
2437
<div>
25-
<button type="button" className="secondary">
38+
<button type="button" className="secondary" onClick={onEdit}>
2639
edit
2740
</button>
2841
<button

0 commit comments

Comments
 (0)