Disable logging

This commit is contained in:
Andrea Fazzi 2022-09-06 14:41:32 +02:00
parent d5d6fd2e08
commit 2e79c6595f
4 changed files with 58 additions and 38 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.log

View file

@ -5,7 +5,6 @@ import (
"net/http" "net/http"
"git.andreafazzi.eu/andrea/probo/hasher/sha256" "git.andreafazzi.eu/andrea/probo/hasher/sha256"
"git.andreafazzi.eu/andrea/probo/logger"
"git.andreafazzi.eu/andrea/probo/store/memory" "git.andreafazzi.eu/andrea/probo/store/memory"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -13,7 +12,7 @@ import (
const port = "3000" const port = "3000"
func main() { func main() {
logger.SetLevel(logger.DebugLevel) // logger.SetLevel(logger.DebugLevel)
server := NewProboCollectorServer( server := NewProboCollectorServer(
memory.NewMemoryProboCollectorStore( memory.NewMemoryProboCollectorStore(

View file

@ -8,6 +8,8 @@ const uniqueIdentifier = () =>
.toString(36) .toString(36)
.replace(/[^a-z]+/g, ""); .replace(/[^a-z]+/g, "");
const sanitizeBlockContent = (text: string) => text.replace(/((?<=::).*|.*::)/g, "").replace(/{.*}/, "").trim()
async function fetchQuizzes() { async function fetchQuizzes() {
const { status: status, content: quizzes } = await fetch(endpoint).then(res => res.json()) const { status: status, content: quizzes } = await fetch(endpoint).then(res => res.json())
const ret = quizzes || [] const ret = quizzes || []
@ -17,6 +19,19 @@ async function fetchQuizzes() {
}) })
} }
const render = (id, slot, status: ("modified" | "saved" | "error")) => {
logseq.provideUI({
key: `${id}`,
slot,
reset: true,
template: `
${status === 'saved' ? '<button data-on-click="createOrUpdateQuiz" class="renderBtn">Save</button><span>saved</span>' : '<button data-on-click="createOrUpdateQuiz" class="renderBtn">Save</button>'}
`,
});
}
const main = () => { const main = () => {
console.log("logseq-probo-plugin LOADED!"); console.log("logseq-probo-plugin LOADED!");
@ -37,23 +52,18 @@ const main = () => {
`{{renderer :probo_${uniqueIdentifier()}}}` `{{renderer :probo_${uniqueIdentifier()}}}`
); );
// const currBlock = await logseq.Editor.getCurrentBlock();
// await logseq.Editor.insertBlock(currBlock.uuid, 'Write your question here...',
// {
// sibling: false,
// before: false,
// }
// );
// await logseq.Editor.exitEditingMode();
}); });
logseq.App.onMacroRendererSlotted(async ({ slot, payload }) => { logseq.App.onMacroRendererSlotted(async ({ slot, payload }) => {
const [type] = payload.arguments; const [type] = payload.arguments;
if (!type.startsWith(":probo")) return
const id = type.split("_")[1]?.trim(); const id = type.split("_")[1]?.trim();
const proboId = `probo_${id}`; const proboId = `probo_${id}`;
let status: ("modified" | "saved" | "error")
logseq.provideModel({ logseq.provideModel({
async createOrUpdateQuiz() { async createOrUpdateQuiz() {
const parentBlock = await logseq.Editor.getBlock(payload.uuid, { includeChildren: true }); const parentBlock = await logseq.Editor.getBlock(payload.uuid, { includeChildren: true });
@ -62,30 +72,40 @@ const main = () => {
}) })
const quiz = { const quiz = {
question: {text: parentBlock.content }, question: { text: sanitizeBlockContent(parentBlock.content) },
answers: answers answers: answers
} }
const res = await fetch(endpoint+'/create', { method: 'POST', body: JSON.stringify(quiz) }) if (parentBlock.properties.proboQuizUuid) {
const res = await fetch(endpoint + `/update/${parentBlock.properties.proboQuizUuid}`, { method: 'PUT', body: JSON.stringify(quiz) })
const data = await res.json(); const data = await res.json();
await logseq.Editor.upsertBlockProperty(parentBlock.uuid, `probo-quiz-uuid`, data.content.ID) await logseq.Editor.upsertBlockProperty(parentBlock.uuid, `probo-quiz-uuid`, data.content.ID)
render(proboId, slot, "saved")
} else {
const res = await fetch(endpoint + '/create', { method: 'POST', body: JSON.stringify(quiz) })
const data = await res.json();
await logseq.Editor.upsertBlockProperty(parentBlock.uuid, `probo-quiz-uuid`, data.content.ID)
render(proboId, slot, "saved")
}
} }
}); });
logseq.provideStyle(` logseq.provideStyle(`
.renderBtn { .renderBtn {
border: 1px solid white; border: 1px solid white;
border-radius: 8px; border-radius: 8px;
padding: 5px; padding: 5px;
font-size: 80%; margin-right: 5px;
background-color: black; font-size: 80%;
color: white; background-color: black;
} color: white;
.renderBtn:hover { }
background-color: white;
color: black; .renderBtn:hover {
} background-color: white;
color: black;
}
`); `);
logseq.provideUI({ logseq.provideUI({

View file

@ -117,8 +117,8 @@ func (s *MemoryProboCollectorStore) createAnswerFromHash(hash string, answer *mo
func (s *MemoryProboCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) { func (s *MemoryProboCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) {
result := make([]*models.Quiz, 0) result := make([]*models.Quiz, 0)
for hash := range s.quizzesHashes { for id := range s.quizzes {
result = append(result, s.getQuizFromHash(hash)) result = append(result, s.getQuizFromID(id))
} }
return result, nil return result, nil
} }
@ -163,7 +163,7 @@ func (s *MemoryProboCollectorStore) createOrUpdateQuiz(r *client.CreateUpdateQui
}) })
} }
if answer.Correct { if answer.Correct {
quiz.Correct = a // s.readAnswer(answerID) quiz.Correct = a
} }
quiz.Answers = append(quiz.Answers, a) quiz.Answers = append(quiz.Answers, a)
} }