Set owner of sticker.
Owner is property of sticker. Stickers are a reference for users, so flip the API.
This commit is contained in:
parent
4ab5bcf367
commit
e9da81daca
4 changed files with 32 additions and 15 deletions
|
|
@ -14,6 +14,7 @@ const stickers = {
|
||||||
color: "#000088",
|
color: "#000088",
|
||||||
layout: "layout1",
|
layout: "layout1",
|
||||||
message1: "I am an activist!",
|
message1: "I am an activist!",
|
||||||
|
owner: null,
|
||||||
uuid: "88ae126f-b19a-4287-abe0-a8f5ac763cb7",
|
uuid: "88ae126f-b19a-4287-abe0-a8f5ac763cb7",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,42 @@
|
||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
|
|
||||||
|
import { useUserStore } from "@/stores/userStore";
|
||||||
|
|
||||||
export const useStickerStore = defineStore('sticker', {
|
export const useStickerStore = defineStore('sticker', {
|
||||||
actions: {
|
actions: {
|
||||||
async fetch(uuid: string) {
|
async fetch(uuid: string) {
|
||||||
console.log('fetch');
|
|
||||||
// TODO: Check if we have it already and if it's stale.
|
// TODO: Check if we have it already and if it's stale.
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`/api/sticker/${uuid}`,
|
`/api/sticker/${uuid}`,
|
||||||
);
|
);
|
||||||
const json = await response.json();
|
const json = await response.json();
|
||||||
console.log(json);
|
|
||||||
// this.stickers.push(json);
|
// this.stickers.push(json);
|
||||||
this.sticker = json;
|
this.sticker = json;
|
||||||
console.log('done in fetch');
|
},
|
||||||
|
async put(uuid: string) {
|
||||||
|
const response = await fetch(
|
||||||
|
`/api/sticker/${uuid}`,
|
||||||
|
{
|
||||||
|
body: JSON.stringify(this.sticker),
|
||||||
|
method: 'PUT',
|
||||||
|
},
|
||||||
|
);
|
||||||
|
const json = await response.json();
|
||||||
|
// Disable this for now so we don't clobber the new owner.
|
||||||
|
// this.sticker = json;
|
||||||
|
},
|
||||||
|
setOwner(user) {
|
||||||
|
if (!this.sticker) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// this.sticker.owner = user.username; does not work
|
||||||
|
this.sticker = {
|
||||||
|
...this.sticker,
|
||||||
|
owner: user.username,
|
||||||
|
}
|
||||||
|
this.put(this.sticker.uuid);
|
||||||
|
const userStore = useUserStore();
|
||||||
|
userStore.addSticker(this.sticker);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
state: () => {
|
state: () => {
|
||||||
|
|
|
||||||
|
|
@ -8,17 +8,8 @@ const anonymous = {
|
||||||
|
|
||||||
export const useUserStore = defineStore('user', {
|
export const useUserStore = defineStore('user', {
|
||||||
actions: {
|
actions: {
|
||||||
async addSticker(sticker) {
|
addSticker(sticker) {
|
||||||
const response = await fetch(
|
this.user.stickers.push(sticker);
|
||||||
`/api/user/${this.user.username}/stickers`,
|
|
||||||
{
|
|
||||||
body: JSON.stringify({uuid: sticker.uuid}),
|
|
||||||
method: 'POST',
|
|
||||||
},
|
|
||||||
);
|
|
||||||
if (response.ok) {
|
|
||||||
this.user.stickers.push(sticker);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
async login(username: string, password: string) {
|
async login(username: string, password: string) {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ const second = computed(() => {
|
||||||
})
|
})
|
||||||
|
|
||||||
function claimSticker() {
|
function claimSticker() {
|
||||||
userStore.addSticker(stickerStore.sticker);
|
stickerStore.setOwner(userStore.user);
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
@ -57,6 +57,7 @@ function claimSticker() {
|
||||||
</div>
|
</div>
|
||||||
<Button
|
<Button
|
||||||
v-bind:disabled="userStore.isAnonymous"
|
v-bind:disabled="userStore.isAnonymous"
|
||||||
|
v-if="stickerStore.sticker?.owner == null"
|
||||||
v-on:click="claimSticker">
|
v-on:click="claimSticker">
|
||||||
This is my sticker!
|
This is my sticker!
|
||||||
</Button>
|
</Button>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue