Ignore:
Timestamp:
06/24/26 16:51:52 (11 days ago)
Author:
kikisrbinoska <srbinoskakristina07@…>
Branches:
main
Children:
3ae4bab
Parents:
99c1e45
Message:

Fixed views count

File:
1 edited

Legend:

Unmodified
Added
Removed
  • chapterx-frontend/src/pages/story/ChapterReadPage.tsx

    r99c1e45 ra8f4a2d  
    11import React, { useEffect, useState } from 'react'
    22import { useParams, useNavigate } from 'react-router-dom'
    3 import { ArrowLeft, ArrowRight, BookOpen, Eye, ChevronLeft, ChevronRight } from 'lucide-react'
     3import { ArrowLeft, ChevronLeft, ChevronRight } from 'lucide-react'
     4import axios from 'axios'
    45import { useStoryStore } from '../../store/storyStore'
     6import { Chapter } from '../../types'
    57import { Button } from '../../components/ui/Button'
     8
     9const API = 'https://localhost:7125/api'
     10
     11function mapChapter(c: any): Chapter {
     12  return {
     13    chapter_id: c.id ?? c.chapter_id,
     14    story_id: c.storyId ?? c.story_id,
     15    title: c.title ?? c.name ?? '',
     16    content: c.content ?? '',
     17    chapter_number: c.number ?? c.chapter_number ?? 0,
     18    word_count: c.wordCount ?? c.word_count ?? 0,
     19    view_count: c.viewCount ?? c.view_count ?? 0,
     20    is_published: true,
     21    created_at: c.createdAt ?? c.created_at ?? '',
     22    updated_at: c.updatedAt ?? c.updated_at ?? '',
     23  }
     24}
    625
    726export const ChapterReadPage: React.FC = () => {
     
    1130  const [scrollProgress, setScrollProgress] = useState(0)
    1231  const [viewed, setViewed] = useState(false)
     32  const [chapter, setChapter] = useState<Chapter | null>(null)
     33  const [loading, setLoading] = useState(true)
     34
     35  useEffect(() => {
     36    setLoading(true)
     37    setViewed(false)
     38    setScrollProgress(0)
     39    axios.get(`${API}/chapters/${chapterId}`)
     40      .then(res => {
     41        const data = res.data?.chapter ?? res.data
     42        setChapter(mapChapter(data))
     43      })
     44      .catch(() => {
     45        const fallback = chapters.find(c => c.chapter_id === Number(chapterId))
     46        if (fallback) setChapter(fallback)
     47      })
     48      .finally(() => setLoading(false))
     49  }, [chapterId])
    1350
    1451  const story = stories.find(s => s.story_id === Number(storyId))
    15   const chapter = chapters.find(c => c.chapter_id === Number(chapterId))
    1652  const storyChapters = chapters
    1753    .filter(c => c.story_id === Number(storyId) && c.is_published)
     
    3773    return () => window.removeEventListener('scroll', handler)
    3874  }, [chapter, viewed, incrementViewCount])
     75
     76  if (loading) {
     77    return (
     78      <div className="max-w-4xl mx-auto px-4 py-20 text-center">
     79        <p className="text-slate-400">Loading chapter...</p>
     80      </div>
     81    )
     82  }
    3983
    4084  if (!story || !chapter) {
     
    73117          </div>
    74118
    75           <div className="flex items-center gap-1 text-slate-400 text-xs">
    76             <Eye size={12} />
    77             {scrollProgress.toFixed(0)}%
    78           </div>
     119          <div />
    79120        </div>
    80121      </div>
     
    90131            {chapter.title}
    91132          </h1>
    92           <div className="flex items-center justify-center gap-4 text-slate-500 text-sm">
    93             <span className="flex items-center gap-1">
    94               <BookOpen size={13} />
    95               {chapter.word_count.toLocaleString()} words
    96             </span>
    97             <span className="flex items-center gap-1">
    98               <Eye size={13} />
    99               {chapter.view_count.toLocaleString()} reads
    100             </span>
    101           </div>
    102133          <div className="mt-4 w-16 h-px bg-gradient-to-r from-transparent via-indigo-500 to-transparent mx-auto" />
    103134        </div>
Note: See TracChangeset for help on using the changeset viewer.