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

Articles can be "completed" #1

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Conversation

potpov
Copy link
Collaborator

@potpov potpov commented Oct 24, 2021

Each time all the words for a given article are guessed, the article is marked as completed and the user interface gets updated. Once all the articles from the level are completed, the user is moved to the next level and loads harder articles (still static).

The idea: each user has a level and on each level, we fetch a list of articles, progressively more complex and with a new vocabulary. Hence the user is supposed to find and learn a bunch of new words per level.

Copy link
Collaborator

@tom95 tom95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice changes, in particular also thanks for updating for the changes in MaterialUI!

I added some feedback on the stylistic/idiomatic level. I also believe the project was originally formatted using an automatic pretty-printer but I could be wrong. If not, it may make sense to configure prettier.js for the project and run it on the whole codebase once, this may be easier than hunting for small whitespace differences (some of which I had made comments for, so you can ignore these :))

(Note that I didn't get around to running the code yet)

src/App.js Outdated Show resolved Hide resolved
Comment on lines 99 to 103
const [open, setOpen] = React.useState(false);
const [content, setContent] = React.useState('');
const [notification, setNotification] = React.useState('');
const [articles, setArticles] = React.useState(fetch_articles());
const [level, setLevel] = React.useState(fetch_user_level());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the original code was not consistent either but maybe you could make it consistent in one swoop and remove the React. prefix for the original two lines as well, so that it's the same as useParams and useHistory below?

useEffect(() => {
// TODO: check from state if all articles are completed -> level up!
// use:
if (articles.filter((article) => !article.completed).length == 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using articles.every :)

Copy link
Collaborator Author

@potpov potpov Oct 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, thanks! there is still a bit of rust on JS to take off :D

const markAsComplete = (article_id) => {
let updated_articles = articles.map(
(article) => {
return((article.id == article_id && !article.completed) ? {...article, 'completed': true}: article
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some minor style comments, I would have probably formulated it as:

articles.map((article) => article.id === articleId ? {...article, completed: true} : article)

Note camelCase instead of snake_case, the triple ===l, and changed whitespace

)
}
)
setArticles(updated_articles)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, take the line from above and then insert it here:

setArticles((articles) => articles.map(...))

if you update a value in React, the recommended pattern is to use these update functions instead of reading the value first. This prevents using an out-of-date articles value if multiple "set"s happened before the next rebuild.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right.
I initially made the all-articles-completed check on this function; as setState methods won't reflect state changes immediately I was using the tmp variable as a workaround. I figured there's the useEffect function for this purpose but forgot to edit the line :)

Also, react-dom inherently treats URL params as strings; will force casting on the next commit to add the strict equality here.


}

function get_article(article_id){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider changing to camelCase as well :)

Setmatch_left(match_left - 1)
}

const reactStringReplace = require('react-string-replace');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wow this worked? I didn't know you could that tbh :D

I believe the more idiomatic approach (correct me if I'm wrong for this library) would be to move it to the very top of the file and use esmodule-style imports, along the lines of import { reactStringReplace } from 'react-string-replace; or similar.

'id': 0,
'completed': false,
'title': 'Apple Pie',
'content': 'German Apple Cake is a traditional German dessert that is so easy to make even if you aren’t totally kitchen confident! With a simple batter that rises up and bakes around the apples this easy apple coffee cake is the perfect everyday dessert that tastes best with a dollop of whipped cream on top.',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you replaced the content with the preview? At least when I open an article, I also only see this short text, rather than the steps and methods, as in ApplePie.js.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup. can fix it easily but I am actually craving for a model and a migration with all articles to be run on init so we can clean up this file.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, in the long run that would definitely be more convenient.

'id': 2,
'completed': false,
'title': 'Capitain Bluebear',
'content': 'The 13​¹⁄₂ Lives of Captain Bluebear is a 1999 fantasy novel by German writer and cartoonist Walter Moers which details the numerous lives of a human-sized bear with blue fur.apple',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there "apple" at the end of almost every article? Might be something we had for the demo, but we probably should remove it now :D

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the demo right, you need at least one word to guess on each article or you won't be able to complete it and level up (laziness got real here and I just added apples everywhere sorry) :D


const handleSubmit = (answer) => {
if (confirmWord(german, answer)) {
setSuccess(true);
setOpened(false);
onComplete();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is this being triggered? I got all words to "white" in the apple pie article, but it wasn't marked as completed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think the problem was that I had to click on every occurrence of the world "apple" again, even though it was already white.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly! You have to guess all the words to complete an article (: just like an exercise. but the interface is confusing now.
Since the beginning, I thought we have to change the confetti effect to be only on the occurrence of the word you guess. it is a bit counterintuitive atm (now even more with this update) - will look at it

package.json Outdated
"proxy": "http://localhost:8000"
"proxy": "http://localhost:8000",
"devDependencies": {
"prettier": "2.4.1"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call!

strict comparison on URL, removed commented routes, camel case refactoring
- Partial translation component is now loaded correctly into the DOM even when an article contains html tags

- Old articles body restored

- Minor warning from Material UI fixed
@potpov
Copy link
Collaborator Author

potpov commented Jan 3, 2022

Alright,
I think I solved pretty much all the issues you highlighted for this first update. Let me know if I missed something.
Adding back the content of the articles arose some bug that took me quite a while to solve. Static articles fetch is still icky to see but it's just a mock-up I plan to remove next with some real API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants