I would join the two changes to the frontmatter into one call of app.fileManager.processFrontMatter()
, so you don’t have two independent file write operations to the same file.
And the code also demands for the properties to be present when you start the show… There is also a small feature, that if you spam click either button, it might lag a little (or demand another click (or reload) after a little delay) to get the correct number.
Accounting for all of this, and joining the handling into one function, we end up with another way of doing this which look like this:
```dataviewjs
const TOTAL_KEY = "total"
const CORRECT_KEY = "correct"
async function handleButton(currentFile, deltaCorrect ) {
await app.fileManager.processFrontMatter(currentFile, (fm) => {
// Verify that our properties actually exists, and create if not
if ( !(TOTAL_KEY in fm) ) fm[TOTAL_KEY] = 0
if ( !(CORRECT_KEY in fm) ) fm[CORRECT_KEY] = 0
// Increase total number, and add the delta to the other
fm[TOTAL_KEY] += 1
fm[CORRECT_KEY] += deltaCorrect
})
}
const current = dv.current()
const currentFile = app.vault.getAbstractFileByPath(current.file.path)
const countCorrect = dv.el('button', 'Correct')
countCorrect.onclick = () => { handleButton(currentFile, 1) }
const countWrong = dv.el('button', 'Wrong')
countWrong.onclick = () => { handleButton(currentFile, 0) }
let test = parseInt(dv.current()[CORRECT_KEY] ?? 0)
let max = parseInt(dv.current()[TOTAL_KEY] ?? 0)
dv.el('progress', " ",{ attr: { value: test, max: max } })
dv.span(" " + test + " / " + max)
dv.paragraph("")
dv.span(countCorrect); dv.span(countWrong)
```
Here it’s also easier to change to whatever key/property names you want to use, as they’re declared as constant at the start. And we give them a start of 0, so it looks nice even with undefined properties.
There are simply too many ways to do stuff when coding, so this is not say that @Anwen’s solution is wrong in any way, it’s just a different variant with slightly more error handling along the way.