This repository has been archived by the owner on May 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add keyboard shortcuts to Swipe Review Tool
- Loading branch information
1 parent
31fb562
commit e51dece
Showing
4 changed files
with
208 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
|
||
import Cards from './swipecard/Cards'; | ||
import Card from "./swipecard/CardSwitcher"; | ||
import SubmitButton from './submit-button'; | ||
|
||
export default function SwipeReview(props) { | ||
const { | ||
onReviewSentence, | ||
onSubmit, | ||
sentences, | ||
page, | ||
lastPage, | ||
offset, | ||
message, | ||
reviewedSentencesCount, | ||
} = props; | ||
|
||
const cardsRef = useRef(null); | ||
|
||
const onReviewButtonPress = (event, approval) => { | ||
event.preventDefault(); | ||
processReviewOnCurrentCard(approval); | ||
}; | ||
|
||
const processReviewOnCurrentCard = (approval) => { | ||
const currentIndex = cardsRef.current.state.index; | ||
|
||
if (typeof approval !== 'undefined') { | ||
onReviewSentence(currentIndex, approval); | ||
} | ||
|
||
cardsRef.current.setState({ index: currentIndex + 1 }); | ||
}; | ||
|
||
useEffect(() => { | ||
const handler = ({ key }) => { | ||
if (key === 'y') { | ||
return processReviewOnCurrentCard(true); | ||
} | ||
|
||
if (key === 'n') { | ||
return processReviewOnCurrentCard(false); | ||
} | ||
|
||
if (key === 's') { | ||
return processReviewOnCurrentCard(undefined); | ||
} | ||
}; | ||
|
||
window.addEventListener('keydown', handler); | ||
|
||
return () => { | ||
window.removeEventListener('keydown', handler); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<form id="review-form" onSubmit={onSubmit}> | ||
<p>Swipe right to approve the sentence. Swipe left to reject it.</p> | ||
<p>You have reviewed {reviewedSentencesCount} sentences. Do not forget to submit your review by clicking on the "Finish Review" button below!</p> | ||
|
||
<SubmitButton submitText="Finish Review"/> | ||
|
||
{ message && ( <p>{message}</p> ) } | ||
|
||
<Cards onEnd={() => { | ||
if (page === lastPage) { | ||
onSubmit({preventDefault: () => {}}); | ||
} | ||
}} className="main-root" ref={cardsRef}> | ||
{ sentences.map((sentence, i) => ( | ||
<Card | ||
key={offset + i} | ||
onSwipeLeft={() => onReviewSentence(i, false)} | ||
onSwipeRight={() => onReviewSentence(i, true)} | ||
> | ||
<div className="card-sentence-box"> | ||
<p>{sentence.sentence || sentence}</p> | ||
<small className="card-source">{sentence.source ? `Source: ${sentence.source}` : ''}</small> | ||
</div> | ||
</Card> | ||
))} | ||
</Cards> | ||
<section className="card-review-footer"> | ||
<button className="standalone secondary big" onClick={(event) => onReviewButtonPress(event, false)}>Reject</button> | ||
<button className="standalone secondary big" onClick={(event) => onReviewButtonPress(event, undefined)}>Skip</button> | ||
<button className="standalone secondary big" onClick={(event) => onReviewButtonPress(event, true)}>Approve</button> | ||
</section> | ||
</form> | ||
); | ||
} |
Oops, something went wrong.