round 3
This commit is contained in:
@@ -1,45 +1,188 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted } from 'vue'
|
||||
import { useRoute } from 'vue-router'
|
||||
import { ref, onMounted, computed, onUnmounted } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { isParentAuthenticated } from '../../stores/auth'
|
||||
import ChildDetailCard from './ChildDetailCard.vue'
|
||||
import ChildTaskList from '../task/ChildTaskList.vue'
|
||||
import ChildRewardList from '../reward/ChildRewardList.vue'
|
||||
import AssignTaskButton from '../AssignTaskButton.vue'
|
||||
|
||||
interface Child {
|
||||
id: string | number
|
||||
name: string
|
||||
age: number
|
||||
points?: number
|
||||
}
|
||||
import { eventBus } from '@/common/eventBus'
|
||||
import type {
|
||||
Task,
|
||||
Child,
|
||||
Event,
|
||||
Reward,
|
||||
TaskUpdateEventPayload,
|
||||
RewardUpdateEventPayload,
|
||||
ChildUpdateEventPayload,
|
||||
ChildDeleteEventPayload,
|
||||
} from '@/common/models'
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
|
||||
const child = ref<Child | null>(null)
|
||||
const tasks = ref<string[]>([])
|
||||
const loading = ref(true)
|
||||
const error = ref<string | null>(null)
|
||||
const rewardListRef = ref()
|
||||
const showConfirm = ref(false)
|
||||
const selectedTask = ref<Task | null>(null)
|
||||
const showRewardConfirm = ref(false)
|
||||
const selectedReward = ref<Reward | null>(null)
|
||||
|
||||
onMounted(async () => {
|
||||
function handlePointsUpdate(event: Event) {
|
||||
const payload = event.payload as TaskUpdateEventPayload | RewardUpdateEventPayload
|
||||
if (child.value && payload.child_id == child.value.id) {
|
||||
child.value.points = payload.points
|
||||
}
|
||||
}
|
||||
|
||||
function handleServerChange(event: Event) {
|
||||
const payload = event.payload as
|
||||
| TaskUpdateEventPayload
|
||||
| RewardUpdateEventPayload
|
||||
| ChildUpdateEventPayload
|
||||
if (child.value && payload.child_id == child.value.id) {
|
||||
fetchChildData(child.value.id)
|
||||
}
|
||||
}
|
||||
|
||||
function handleChildDeletion(event: Event) {
|
||||
const payload = event.payload as ChildDeleteEventPayload
|
||||
if (child.value && payload.child_id == child.value.id) {
|
||||
// Navigate away back to children list
|
||||
router.push({ name: 'ChildrenListView' })
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchChildData(id: string | number) {
|
||||
loading.value = true
|
||||
try {
|
||||
const resp = await fetch(`/api/child/${route.params.id}`)
|
||||
const resp = await fetch(`/api/child/${id}`)
|
||||
if (!resp.ok) throw new Error(`HTTP ${resp.status}`)
|
||||
const data = await resp.json()
|
||||
child.value = data.children ? data.children : data
|
||||
tasks.value = data.tasks || []
|
||||
error.value = null
|
||||
} catch (err) {
|
||||
error.value = err instanceof Error ? err.message : 'Failed to fetch child'
|
||||
console.error(err)
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
eventBus.on('task_update', handlePointsUpdate)
|
||||
eventBus.on('reward_update', handlePointsUpdate)
|
||||
eventBus.on('task_set', handleServerChange)
|
||||
eventBus.on('reward_set', handleServerChange)
|
||||
eventBus.on('child_update', handleServerChange)
|
||||
eventBus.on('child_delete', handleChildDeletion)
|
||||
|
||||
if (route.params.id) {
|
||||
const idParam = Array.isArray(route.params.id) ? route.params.id[0] : route.params.id
|
||||
if (idParam !== undefined) {
|
||||
fetchChildData(idParam)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error in onMounted:', err)
|
||||
}
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
eventBus.off('task_update', handlePointsUpdate)
|
||||
eventBus.off('reward_update', handlePointsUpdate)
|
||||
eventBus.off('task_set', handleServerChange)
|
||||
eventBus.off('reward_set', handleServerChange)
|
||||
eventBus.off('child_update', handleServerChange)
|
||||
eventBus.off('child_delete', handleChildDeletion)
|
||||
})
|
||||
|
||||
const refreshRewards = () => {
|
||||
rewardListRef.value?.refresh()
|
||||
}
|
||||
|
||||
const handleTriggerTask = (task: Task) => {
|
||||
selectedTask.value = task
|
||||
showConfirm.value = true
|
||||
}
|
||||
|
||||
const confirmTriggerTask = async () => {
|
||||
if (!child.value?.id || !selectedTask.value) return
|
||||
try {
|
||||
const resp = await fetch(`/api/child/${child.value.id}/trigger-task`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ task_id: selectedTask.value.id }),
|
||||
})
|
||||
if (!resp.ok) return
|
||||
const data = await resp.json()
|
||||
console.log('Trigger task response data:', child.value.id, data.id)
|
||||
if (child.value && child.value.id === data.id) child.value.points = data.points
|
||||
} catch (err) {
|
||||
console.error('Failed to trigger task:', err)
|
||||
} finally {
|
||||
showConfirm.value = false
|
||||
selectedTask.value = null
|
||||
}
|
||||
}
|
||||
|
||||
const handleTriggerReward = (reward: Reward, redeemable: boolean) => {
|
||||
console.log('Handle trigger reward:', reward, redeemable)
|
||||
if (!redeemable) return
|
||||
selectedReward.value = reward
|
||||
showRewardConfirm.value = true
|
||||
}
|
||||
|
||||
const confirmTriggerReward = async () => {
|
||||
if (!child.value?.id || !selectedReward.value) return
|
||||
try {
|
||||
const resp = await fetch(`/api/child/${child.value.id}/trigger-reward`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ reward_id: selectedReward.value.id }),
|
||||
})
|
||||
if (!resp.ok) return
|
||||
const data = await resp.json()
|
||||
if (child.value && child.value.id === data.id) child.value.points = data.points
|
||||
} catch (err) {
|
||||
console.error('Failed to trigger reward:', err)
|
||||
} finally {
|
||||
showRewardConfirm.value = false
|
||||
selectedReward.value = null
|
||||
}
|
||||
}
|
||||
|
||||
function goToAssignTasks() {
|
||||
if (child.value?.id) {
|
||||
router.push({ name: 'TaskAssignView', params: { id: child.value.id, type: 'good' } })
|
||||
}
|
||||
}
|
||||
|
||||
function goToAssignBadHabits() {
|
||||
if (child.value?.id) {
|
||||
router.push({ name: 'TaskAssignView', params: { id: child.value.id, type: 'bad' } })
|
||||
}
|
||||
}
|
||||
|
||||
function goToAssignRewards() {
|
||||
if (child.value?.id) {
|
||||
router.push({ name: 'RewardAssignView', params: { id: child.value.id } })
|
||||
}
|
||||
}
|
||||
|
||||
const handleTaskPointsUpdated = ({ id, points }: { id: string | number; points: number }) => {
|
||||
if (child.value && child.value.id === id) child.value.points = points
|
||||
refreshRewards()
|
||||
}
|
||||
const handleRewardPointsUpdated = ({ id, points }: { id: string | number; points: number }) => {
|
||||
if (child.value && child.value.id === id) child.value.points = points
|
||||
}
|
||||
|
||||
const childId = computed(() => child.value?.id ?? null)
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -51,31 +194,118 @@ const refreshRewards = () => {
|
||||
<div class="main">
|
||||
<ChildDetailCard :child="child" />
|
||||
<ChildTaskList
|
||||
title="Chores"
|
||||
:task-ids="tasks"
|
||||
:child-id="child ? child.id : null"
|
||||
:child-id="childId"
|
||||
:is-parent-authenticated="isParentAuthenticated"
|
||||
@points-updated="
|
||||
({ id, points }) => {
|
||||
if (child && child.id === id) child.points = points
|
||||
refreshRewards()
|
||||
}
|
||||
"
|
||||
:filter-type="1"
|
||||
@points-updated="handleTaskPointsUpdated"
|
||||
@trigger-task="handleTriggerTask"
|
||||
/>
|
||||
<ChildTaskList
|
||||
title="Bad Habits"
|
||||
:task-ids="tasks"
|
||||
:child-id="childId"
|
||||
:is-parent-authenticated="isParentAuthenticated"
|
||||
:filter-type="2"
|
||||
@points-updated="handleTaskPointsUpdated"
|
||||
@trigger-task="handleTriggerTask"
|
||||
/>
|
||||
<ChildRewardList
|
||||
ref="rewardListRef"
|
||||
:child-id="child ? child.id : null"
|
||||
:is-parent-authenticated="isParentAuthenticated"
|
||||
@points-updated="
|
||||
({ id, points }) => {
|
||||
if (child && child.id === id) child.points = points
|
||||
refreshRewards()
|
||||
}
|
||||
"
|
||||
:child-id="childId"
|
||||
:child-points="child?.points ?? 0"
|
||||
:is-parent-authenticated="false"
|
||||
@points-updated="handleRewardPointsUpdated"
|
||||
@trigger-reward="handleTriggerReward"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Place the AssignTaskButton here, outside .main but inside .container -->
|
||||
<AssignTaskButton :child-id="child ? child.id : null" />
|
||||
<div class="assign-buttons">
|
||||
<button v-if="child" class="assign-task-btn" @click="goToAssignTasks">Assign Tasks</button>
|
||||
<button v-if="child" class="assign-bad-btn" @click="goToAssignBadHabits">
|
||||
Assign Bad Habits
|
||||
</button>
|
||||
<button v-if="child" class="assign-reward-btn" @click="goToAssignRewards">
|
||||
Assign Rewards
|
||||
</button>
|
||||
</div>
|
||||
<div v-if="showConfirm && selectedTask" class="modal-backdrop">
|
||||
<div class="modal">
|
||||
<div class="task-info">
|
||||
<img
|
||||
v-if="selectedTask.image_url"
|
||||
:src="selectedTask.image_url"
|
||||
alt="Task Image"
|
||||
class="task-image"
|
||||
/>
|
||||
<div class="task-details">
|
||||
<div class="task-name">{{ selectedTask.name }}</div>
|
||||
<div class="task-points" :class="selectedTask.is_good ? 'good' : 'bad'">
|
||||
{{ selectedTask.points }} pts
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="dialog-message" style="margin-bottom: 1.2rem">
|
||||
{{ selectedTask.is_good ? 'Add' : 'Subtract' }} these points
|
||||
{{ selectedTask.is_good ? 'to' : 'from' }}
|
||||
<span class="child-name">{{ child?.name }}</span>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<button @click="confirmTriggerTask">Yes</button>
|
||||
<button
|
||||
@click="
|
||||
() => {
|
||||
showConfirm = false
|
||||
selectedTask = null
|
||||
}
|
||||
"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-if="showRewardConfirm && selectedReward" class="modal-backdrop">
|
||||
<div class="modal">
|
||||
<div class="reward-info">
|
||||
<img
|
||||
v-if="selectedReward.image_id"
|
||||
:src="selectedReward.image_id"
|
||||
alt="Reward Image"
|
||||
class="reward-image"
|
||||
/>
|
||||
<div class="reward-details">
|
||||
<div class="reward-name">{{ selectedReward.name }}</div>
|
||||
<div class="reward-points">
|
||||
{{
|
||||
selectedReward.points_needed === 0
|
||||
? 'Reward Ready!'
|
||||
: selectedReward.points_needed + ' pts needed'
|
||||
}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="dialog-message" style="margin-bottom: 1.2rem">
|
||||
Redeem this reward for <span class="child-name">{{ child?.name }}</span
|
||||
>?
|
||||
</div>
|
||||
<div class="actions">
|
||||
<button @click="confirmTriggerReward">Yes</button>
|
||||
<button
|
||||
@click="
|
||||
() => {
|
||||
showRewardConfirm = false
|
||||
selectedReward = null
|
||||
}
|
||||
"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -89,16 +319,6 @@ const refreshRewards = () => {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
box-sizing: border-box;
|
||||
}
|
||||
.back-btn {
|
||||
background: white;
|
||||
border: 0;
|
||||
padding: 0.6rem 1rem;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
margin-bottom: 1.5rem;
|
||||
color: #667eea;
|
||||
font-weight: 600;
|
||||
}
|
||||
.loading,
|
||||
.error {
|
||||
color: white;
|
||||
@@ -118,9 +338,6 @@ const refreshRewards = () => {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: flex-start;
|
||||
/* Remove grid styles */
|
||||
/* grid-template-columns: 1fr 320px; */
|
||||
/* gap: 1.5rem; */
|
||||
}
|
||||
.main {
|
||||
display: flex;
|
||||
@@ -130,22 +347,84 @@ const refreshRewards = () => {
|
||||
width: 100%;
|
||||
max-width: 600px; /* or whatever width fits your content best */
|
||||
}
|
||||
.side {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
.placeholder {
|
||||
background: rgba(255, 255, 255, 0.08);
|
||||
color: white;
|
||||
padding: 1rem;
|
||||
border-radius: 8px;
|
||||
min-height: 120px;
|
||||
|
||||
/* Modal styles */
|
||||
.modal-backdrop {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
background: rgba(0, 0, 0, 0.45);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 1200;
|
||||
}
|
||||
.modal {
|
||||
background: #fff;
|
||||
color: #222;
|
||||
padding: 1.5rem 2rem;
|
||||
border-radius: 12px;
|
||||
min-width: 240px;
|
||||
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.2);
|
||||
text-align: center;
|
||||
}
|
||||
.task-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
.task-image {
|
||||
width: 72px;
|
||||
height: 72px;
|
||||
object-fit: cover;
|
||||
border-radius: 8px;
|
||||
background: #eee;
|
||||
}
|
||||
.task-details {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
}
|
||||
.task-name {
|
||||
font-weight: 600;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
.task-points,
|
||||
.task-points.good,
|
||||
.task-points.bad {
|
||||
font-weight: 600;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
.task-points.good {
|
||||
color: #38c172;
|
||||
}
|
||||
.task-points.bad {
|
||||
color: #ef4444;
|
||||
}
|
||||
.actions {
|
||||
margin-top: 1.2rem;
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
justify-content: center;
|
||||
}
|
||||
.actions button {
|
||||
padding: 0.5rem 1.2rem;
|
||||
border-radius: 8px;
|
||||
border: none;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
.actions button:first-child {
|
||||
background: #667eea;
|
||||
color: #fff;
|
||||
}
|
||||
.actions button:last-child {
|
||||
background: #f3f3f3;
|
||||
color: #666;
|
||||
}
|
||||
.actions button:last-child:hover {
|
||||
background: #e2e8f0;
|
||||
}
|
||||
|
||||
/* Mobile adjustments */
|
||||
@media (max-width: 900px) {
|
||||
@@ -158,17 +437,83 @@ const refreshRewards = () => {
|
||||
.container {
|
||||
padding: 1rem;
|
||||
}
|
||||
.back-btn {
|
||||
padding: 0.45rem 0.75rem;
|
||||
font-size: 0.95rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
.main {
|
||||
gap: 1rem;
|
||||
}
|
||||
.placeholder {
|
||||
padding: 0.75rem;
|
||||
min-height: 80px;
|
||||
}
|
||||
}
|
||||
|
||||
.dialog-message {
|
||||
font-size: 1.08rem;
|
||||
color: #444;
|
||||
font-weight: 500;
|
||||
}
|
||||
.dialog-message .child-name {
|
||||
color: #667eea;
|
||||
font-weight: 700;
|
||||
margin-left: 2px;
|
||||
}
|
||||
.reward-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
.reward-image {
|
||||
width: 72px;
|
||||
height: 72px;
|
||||
object-fit: cover;
|
||||
border-radius: 8px;
|
||||
background: #eee;
|
||||
}
|
||||
.reward-details {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
}
|
||||
.reward-name {
|
||||
font-weight: 600;
|
||||
font-size: 1.1rem;
|
||||
}
|
||||
.reward-points {
|
||||
color: #667eea;
|
||||
font-weight: 500;
|
||||
font-size: 1rem;
|
||||
}
|
||||
.assign-buttons {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
justify-content: center;
|
||||
margin: 2rem 0;
|
||||
}
|
||||
.assign-task-btn,
|
||||
.assign-bad-btn,
|
||||
.assign-reward-btn {
|
||||
font-weight: 600;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
padding: 0.7rem 1.5rem;
|
||||
font-size: 1.1rem;
|
||||
cursor: pointer;
|
||||
box-shadow: 0 2px 8px rgba(102, 126, 234, 0.08);
|
||||
transition: background 0.18s;
|
||||
color: #fff;
|
||||
background: #667eea;
|
||||
}
|
||||
.assign-task-btn:hover,
|
||||
.assign-bad-btn:hover,
|
||||
.assign-reward-btn:hover {
|
||||
background: #5a67d8;
|
||||
}
|
||||
.assign-bad-btn {
|
||||
background: #ef4444;
|
||||
}
|
||||
.assign-bad-btn:hover {
|
||||
background: #dc2626;
|
||||
}
|
||||
.assign-reward-btn {
|
||||
background: #38c172;
|
||||
}
|
||||
.assign-reward-btn:hover {
|
||||
background: #2f855a;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user