MapComplete/src/UI/Popup/Notes/AddNoteComment.svelte

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

130 lines
3.9 KiB
Svelte
Raw Normal View History

2023-12-26 22:30:27 +01:00
<script lang="ts">
import type { SpecialVisualizationState } from "../../SpecialVisualization"
import { Store, UIEventSource } from "../../../Logic/UIEventSource"
import { placeholder } from "../../../Utils/placeholder"
import Translations from "../../i18n/Translations"
import Speech_bubble from "../../../assets/svg/Speech_bubble.svelte"
import Tr from "../../Base/Tr.svelte"
import NoteCommentElement from "./NoteCommentElement"
import Resolved from "../../../assets/svg/Resolved.svelte"
import Note from "../../../assets/svg/Note.svelte"
import LoginToggle from "../../Base/LoginToggle.svelte"
2024-07-09 00:36:49 +02:00
import { writable } from "svelte/store"
import Loading from "../../Base/Loading.svelte"
2023-12-26 22:30:27 +01:00
export let state: SpecialVisualizationState
export let tags: UIEventSource<Record<string, string>>
let id = tags.data.id
$: {
id = $tags.id
}
let txt: UIEventSource<string> = new UIEventSource(undefined)
2023-12-26 22:30:27 +01:00
let _txt: string = undefined
2023-12-30 15:24:30 +01:00
txt.addCallbackD((t) => {
2023-12-26 22:30:27 +01:00
_txt = t
})
$: {
txt.setData(_txt)
}
const t = Translations.t.notes
let isClosed: Store<boolean> = tags.map((tags) => (tags?.["closed_at"] ?? "") !== "")
2024-07-09 00:36:49 +02:00
let isProcessing = writable(false)
2023-12-26 22:30:27 +01:00
async function addComment() {
if ((txt.data ?? "") == "") {
return
}
2024-07-09 00:36:49 +02:00
isProcessing.set(true)
2023-12-26 22:30:27 +01:00
if (isClosed.data) {
await state.osmConnection.reopenNote(id, txt.data)
await state.osmConnection.closeNote(id)
} else {
await state.osmConnection.addCommentToNote(id, txt.data)
}
NoteCommentElement.addCommentTo(txt.data, tags, state)
txt.setData("")
2024-07-09 00:36:49 +02:00
isProcessing.set(false)
2023-12-26 22:30:27 +01:00
}
async function closeNote() {
2024-07-09 00:36:49 +02:00
isProcessing.set(true)
2023-12-26 22:30:27 +01:00
await state.osmConnection.closeNote(id, txt.data)
2024-07-09 10:54:41 +02:00
isProcessing.set(false)
2023-12-26 22:30:27 +01:00
tags.data["closed_at"] = new Date().toISOString()
2024-06-22 18:50:46 +02:00
NoteCommentElement.addCommentTo(txt.data, tags, state)
2023-12-26 22:30:27 +01:00
tags.ping()
}
async function reopenNote() {
2024-07-09 00:36:49 +02:00
isProcessing.set(true)
2023-12-26 22:30:27 +01:00
await state.osmConnection.reopenNote(id, txt.data)
tags.data["closed_at"] = undefined
2024-06-22 18:50:46 +02:00
NoteCommentElement.addCommentTo(txt.data, tags, state)
2023-12-26 22:30:27 +01:00
tags.ping()
2024-07-09 00:36:49 +02:00
isProcessing.set(false)
2023-12-26 22:30:27 +01:00
}
</script>
<LoginToggle ignoreLoading={true} {state}>
<Tr slot="not-logged-in" t={t.loginToAddComment} />
2023-12-30 15:24:30 +01:00
<form
class="interactive border-interactive m-0 flex flex-col rounded-xl border-2 border-black px-2 py-1"
on:submit|preventDefault={() => addComment()}
>
2023-12-26 22:30:27 +01:00
<label class="neutral-label font-bold">
<Tr t={t.addAComment} />
2023-12-30 15:24:30 +01:00
<textarea
bind:value={_txt}
class="border-grey h-24 w-full rounded-l border"
rows="3"
use:placeholder={t.addCommentPlaceholder}
/>
2023-12-26 22:30:27 +01:00
</label>
2024-07-09 00:36:49 +02:00
{#if $isProcessing}
2024-07-09 13:42:08 +02:00
<Loading />
{:else}
<div class="flex flex-col">
{#if $txt?.length > 0}
<button class="primary flex" on:click|preventDefault={() => addComment()}>
<!-- Add a comment -->
<Speech_bubble class="h-7 w-7 pr-2" />
<Tr t={t.addCommentPlaceholder} />
</button>
{:else}
<div class="alert w-full">
<Tr t={t.typeText} />
</div>
{/if}
2023-12-26 22:30:27 +01:00
2024-07-09 13:42:08 +02:00
{#if !$isClosed}
<button class="flex items-center" on:click|preventDefault={() => closeNote()}>
<Resolved class="h-8 w-8 pr-2" />
<!-- Close note -->
{#if $txt === undefined || $txt === ""}
<Tr t={t.closeNote} />
{:else}
<Tr t={t.addCommentAndClose} />
{/if}
</button>
{:else}
<button class="flex items-center" on:click|preventDefault={() => reopenNote()}>
<!-- Reopen -->
<Note class="h-7 w-7 pr-2" />
{#if $txt === undefined || $txt === ""}
<Tr t={t.reopenNote} />
{:else}
<Tr t={t.reopenNoteAndComment} />
{/if}
</button>
{/if}
</div>
{/if}
2023-12-26 22:30:27 +01:00
</form>
</LoginToggle>