Move quizzes fetching in App.svelte component

This commit is contained in:
Andrea Fazzi 2023-04-19 10:47:06 +02:00
parent 8fa98b6782
commit 2060123d47
5 changed files with 65 additions and 25 deletions

27
app.go
View file

@ -49,3 +49,30 @@ func (a *App) Markdown(quiz Quiz) string {
}
return question + strings.Join(answers, "\n")
}
func (a *App) ParseMarkdown(markdown string) Quiz {
lines := strings.Split(markdown, "\n")
question := ""
answers := []string{}
for _, line := range lines {
trimmedLine := strings.TrimSpace(line)
if trimmedLine == "" {
continue
}
if strings.HasPrefix(trimmedLine, "#") {
question = strings.TrimPrefix(trimmedLine, "#")
question = strings.TrimSpace(question)
} else if strings.HasPrefix(trimmedLine, "-") {
answer := strings.TrimPrefix(trimmedLine, "-")
answer = strings.TrimSpace(answer)
answers = append(answers, answer)
}
}
return Quiz{
Question: question,
Answers: answers,
}
}

View file

@ -1,10 +1,20 @@
<script lang="ts">
import { onMount } from "svelte";
import { FetchQuizzes } from "../wailsjs/go/main/App";
import QuizCardList from "./QuizCardList.svelte";
let quizzes: main.Quiz[] = [];
onMount(async () => {
FetchQuizzes().then(
(result) => {
quizzes = result;
});
});
</script>
<main>
<QuizCardList />
<QuizCardList {quizzes} />
</main>

View file

@ -3,45 +3,42 @@
import { markdown } from "@codemirror/lang-markdown";
import QuizCard from "./QuizCard.svelte";
import { main } from "../wailsjs/go/models";
import { onMount } from "svelte";
import { FetchQuizzes, Markdown } from "../wailsjs/go/main/App";
import { Markdown, ParseMarkdown } from "../wailsjs/go/main/App";
let quizzes: main.Quiz[] = [];
export let quizzes: main.Quiz[];
onMount(async () => {
FetchQuizzes().then(
let value: string;
let editingIndex: number;
function editQuiz(quiz: Quiz, index: number) {
Markdown(quiz).then(
(result) => {
console.log(result);
quizzes = result;
});
});
let editingIndex;
function showEditor(index) {
value = result;
}
)
editingIndex = index;
}
function hideEditor() {
function updateQuiz(index: number) {
ParseMarkdown(value).then(
(result) => {
quizzes[index] = result;
}
)
editingIndex = -1;
}
function updateItem(index, value) {
quizzes[index] = value;
// hideEditor();
}
</script>
<div>
{#each quizzes as quiz, index}
{#if editingIndex === index}
<div class="editor">
<CodeMirror lang={ markdown() } />
<button on:click="{ ()=>hideEditor() }">Save</button>
<CodeMirror bind:value lang={ markdown() } />
<button on:click="{ ()=>updateQuiz(index) }">Save</button>
</div>
{:else}
<div class="item" on:click="{() => showEditor(index)}">
<div class="item" on:click="{() => editQuiz(quiz, index)}">
<QuizCard {quiz} />
</div>
{/if}

View file

@ -5,3 +5,5 @@ import {main} from '../models';
export function FetchQuizzes():Promise<Array<main.Quiz>>;
export function Markdown(arg1:main.Quiz):Promise<string>;
export function ParseMarkdown(arg1:string):Promise<main.Quiz>;

View file

@ -9,3 +9,7 @@ export function FetchQuizzes() {
export function Markdown(arg1) {
return window['go']['main']['App']['Markdown'](arg1);
}
export function ParseMarkdown(arg1) {
return window['go']['main']['App']['ParseMarkdown'](arg1);
}