This commit is contained in:
Pieter Vander Vennet 2024-03-28 02:23:13 +01:00
parent 5f967555eb
commit e11442e100
4 changed files with 80 additions and 33 deletions

View file

@ -672,6 +672,8 @@
"i_am_affiliated_explanation": "Check if you are an owner, creator, employee, …",
"name_required": "A name is required in order to display and create reviews",
"no_reviews_yet": "There are no reviews yet. Be the first to write one and help open data and the business!",
"non_place_review": "One review is not about a place and is not shown here.",
"non_place_reviews": "{n} reviews are not about a place and are not shown here.",
"question": "How would you rate {title()}?",
"question_opinion": "How was your experience?",
"rate": "Rate {n} stars",
@ -683,6 +685,7 @@
"save": "Save review",
"saved": "Review saved. Thanks for sharing!",
"saving_review": "Saving…",
"see_all": "See all your reviews on mangrove.reviews",
"title": "{count} reviews",
"title_singular": "One review",
"too_long": "At most {max} characters are allowed. Your review has {amount} characters.",

View file

@ -3,6 +3,7 @@ import { MangroveReviews, Review } from "mangrove-reviews-typescript"
import { Utils } from "../../Utils"
import { Feature, Position } from "geojson"
import { GeoOperations } from "../GeoOperations"
import ScriptUtils from "../../../scripts/ScriptUtils"
export class MangroveIdentity {
private readonly keypair: Store<CryptoKeyPair>
@ -70,32 +71,53 @@ export class MangroveIdentity {
return this.key_id
}
private geoReviewsById: Store<(Review & { kid: string; signature: string })[]> =
undefined
public getGeoReviews(): Store<(Review & { kid: string, signature: string })[] | undefined> {
if (!this.geoReviewsById) {
const all = this.getAllReviews()
this.geoReviewsById = this.getAllReviews().mapD(reviews => reviews.filter(
review => {
try{
const subjectUrl = new URL(review.sub)
console.log(">>>", subjectUrl)
return subjectUrl.protocol === "geo:"
}catch (e) {
return false
}
}
))
}
return this.geoReviewsById
}
private allReviewsById: UIEventSource<(Review & { kid: string; signature: string })[]> =
undefined
/**
* Gets all reviews that are made for the current identity.
* The returned store will contain `undefined` if still loading
*/
public getAllReviews(): Store<(Review & { kid: string; signature: string })[]> {
public getAllReviews(): Store<(Review & { kid: string; signature: string })[] | undefined> {
if (this.allReviewsById !== undefined) {
return this.allReviewsById
}
this.allReviewsById = new UIEventSource([])
this.key_id.map((pem) => {
this.allReviewsById = new UIEventSource(undefined)
this.key_id.map(async (pem) => {
if (pem === undefined) {
return []
}
MangroveReviews.getReviews({
kid: pem,
}).then((allReviews) => {
const allReviews = await MangroveReviews.getReviews({
kid: pem
})
this.allReviewsById.setData(
allReviews.reviews.map((r) => ({
...r,
...r.payload,
...r.payload
}))
)
})
})
return this.allReviewsById
}
@ -243,7 +265,7 @@ export default class FeatureReviews {
}
const r: Review = {
sub: this.subjectUri.data,
...review,
...review
}
const keypair: CryptoKeyPair = await this._identity.getKeypair()
const jwt = await MangroveReviews.signReview(keypair, r)
@ -253,7 +275,7 @@ export default class FeatureReviews {
...r,
kid,
signature: jwt,
madeByLoggedInUser: new ImmutableStore(true),
madeByLoggedInUser: new ImmutableStore(true)
}
this._reviews.data.push(reviewWithKid)
this._reviews.ping()
@ -301,7 +323,7 @@ export default class FeatureReviews {
signature: reviewData.signature,
madeByLoggedInUser: this._identity.getKeyId().map((user_key_id) => {
return reviewData.kid === user_key_id
}),
})
})
hasNew = true
}

View file

@ -6,12 +6,15 @@
import LoginButton from "../Base/LoginButton.svelte"
import SingleReview from "./SingleReview.svelte"
import Mangrove_logo from "../../assets/svg/Mangrove_logo.svelte"
import Loading from "../Base/Loading.svelte"
/**
* A panel showing all the reviews by the logged-in user
*/
export let state: SpecialVisualizationState
let reviews = state.userRelatedState.mangroveIdentity.getAllReviews()
let allReviews = state.userRelatedState.mangroveIdentity.getAllReviews()
let reviews = state.userRelatedState.mangroveIdentity.getGeoReviews()
let kid = state.userRelatedState.mangroveIdentity.getKeyId()
const t = Translations.t.reviews
</script>
@ -22,8 +25,11 @@
</LoginButton>
</div>
{#if $reviews === undefined}
<Loading />
{:else}
{#if $reviews?.length > 0}
<div class="flex flex-col" on:keypress={(e) => console.log("Got keypress", e)}>
<div class="flex flex-col gap-y-1" on:keypress={(e) => console.log("Got keypress", e)}>
{#each $reviews as review (review.sub)}
<SingleReview {review} showSub={true} {state} />
{/each}
@ -31,6 +37,21 @@
{:else}
<Tr t={t.your_reviews_empty} />
{/if}
{#if $allReviews?.length > $reviews?.length}
{#if $allReviews?.length - $reviews?.length === 1}
<Tr t={t.non_place_review} />
{:else}
<Tr t={t.non_place_reviews.Subs({n:$allReviews?.length - $reviews?.length })} />
{/if}
<a target="_blank"
class="link-underline"
rel="noopener nofollow"
href={`https://mangrove.reviews/list?kid=${encodeURIComponent($kid)}`}
>
<Tr t={t.see_all} />
</a>
{/if}
<a
class="link-underline"
href="https://github.com/pietervdvn/MapComplete/issues/1782"
@ -39,6 +60,7 @@
>
<Tr t={t.reviews_bug} />
</a>
{/if}
<div class="flex justify-end">
<Mangrove_logo class="h-12 w-12 shrink-0 p-1" />
<Tr cls="text-sm subtle" t={t.attribution} />