From 36594133fc916c4fb7421350da59ad6c262f96ba Mon Sep 17 00:00:00 2001 From: Tachibana Shin Date: Mon, 8 May 2023 00:11:29 +0700 Subject: [PATCH] error not found if enter to ep in season splited (#120) * add prop `chapsSibs` for `seasons * fix: leak memory * [replace router ep name]: use `currentMetaChap` replaced check `route.params` * before emit to `not_found` check the seasons guys to see if this ep exists * fix path route `watch-anime` --- src/pages/phim/_season.vue | 108 +++++++++++++++---------- src/pages/phim/response-data-season.ts | 6 +- src/router/routes.ts | 2 +- 3 files changed, 73 insertions(+), 43 deletions(-) diff --git a/src/pages/phim/_season.vue b/src/pages/phim/_season.vue index 4504f0cf..ccca086f 100644 --- a/src/pages/phim/_season.vue +++ b/src/pages/phim/_season.vue @@ -708,36 +708,38 @@ async function fetchSeason(season: string) { const nameSeason = seasons.value[indexMetaSeason].name + const seasonsSplited: Season[] = [] + unflat(chaps, 50).forEach((chapsSplited, index) => { + const value = index === 0 ? realIdSeason : `${realIdSeason}$${index}` + const name = `${nameSeason} (${chapsSplited[0].name} - ${ + chapsSplited[chapsSplited.length - 1].name + })` + + console.log("set %s by %s", value, chapsSplited[0].id) + + const dataOnCache = _cacheDataSeasons.get(value) + const newData: ResponseDataSeasonSuccess = { + status: "success", + response: { + ...response, + chaps: chapsSplited, + ssSibs: seasonsSplited, + }, + } + if (dataOnCache) { + Object.assign(dataOnCache, newData) + } else { + _cacheDataSeasons.set(value, newData) + } + + seasonsSplited.push({ + name, + value, + }) + }) const newSeasons = [ ...seasons.value.slice(0, indexMetaSeason), - ...unflat(chaps, 50).map((chaps, index) => { - const value = - index === 0 ? realIdSeason : `${realIdSeason}$${index}` - const name = `${nameSeason} (${chaps[0].name} - ${ - chaps[chaps.length - 1].name - })` - - console.log("set %s by %s", value, chaps[0].id) - - const dataOnCache = _cacheDataSeasons.get(value) - const newData: ResponseDataSeasonSuccess = { - status: "success", - response: { - ...response, - chaps, - }, - } - if (dataOnCache) { - Object.assign(dataOnCache, newData) - } else { - _cacheDataSeasons.set(value, newData) - } - - return { - name, - value, - } - }), + ...seasonsSplited, ...seasons.value.slice(indexMetaSeason + 1), ] console.log("current seasons: ", seasons.value) @@ -895,23 +897,47 @@ watch( ) watchEffect(() => { // currentChap != undefined because is load done from firestore and ready show but in chaps not found (!currentMetaChap.value) - const chaps = currentDataSeason.value?.chaps - if (!chaps) return + if (!currentDataSeason.value) return - const { chap: epId } = route.params + if (!currentMetaChap.value) { + const epId = currentChap.value - if (!epId) return + // search on all season siblings (season splited with `$`) + const seasonAccuracy = currentDataSeason.value.ssSibs?.find((season) => { + const cache = _cacheDataSeasons.get(season.value) - if (!chaps.some((item) => item.id === epId)) { - if (import.meta.env.DEV) console.warn("Redirect to not_found") - router.replace({ - name: "not_found", - params: { - catchAll: route.path.split("/").slice(1), - }, - query: route.query, - hash: route.hash, + if (cache?.status !== "success") return false + + if (cache.response.chaps.some((item) => item.id === epId)) { + return true + } + + return false }) + + if (seasonAccuracy) { + if (import.meta.env.DEV) + console.log("Redirect to season %s", seasonAccuracy.value) + router.replace({ + name: "watch-anime", + params: { + ...route.params, + season: seasonAccuracy.value, + }, + query: route.query, + hash: route.hash, + }) + } else { + if (import.meta.env.DEV) console.warn("Redirect to not_found") + router.replace({ + name: "not_found", + params: { + catchAll: route.path.split("/").slice(1), + }, + query: route.query, + hash: route.hash, + }) + } } }) // TOOD: check chapName in url is chapName diff --git a/src/pages/phim/response-data-season.ts b/src/pages/phim/response-data-season.ts index c7c0ae56..3e92d740 100644 --- a/src/pages/phim/response-data-season.ts +++ b/src/pages/phim/response-data-season.ts @@ -1,11 +1,15 @@ import type PhimIdChap from "src/apis/parser/phim/[id]/[chap]" +import type { Season } from "./_season.interface" + export interface ResponseDataSeasonPending { status: "pending" } export interface ResponseDataSeasonSuccess { status: "success" - response: Awaited> + response: Awaited> & { + ssSibs?: Season[] + } } export interface ResponseDataSeasonError { status: "error" diff --git a/src/router/routes.ts b/src/router/routes.ts index bbc2cc80..253cb516 100644 --- a/src/router/routes.ts +++ b/src/router/routes.ts @@ -155,7 +155,7 @@ const routes: RouteRecordRaw[] = [ { name: "watch-anime", - path: "/phim/:season/:chapName(?:(.*\\)-)?:chap(\\d+)?", // [feature or defect] + path: "/phim/:season/:chapName(.+)-:chap(\\d+)", // [feature or defect] alias: ["/phim/:season/:chapName(\\0)?:chap(\\d+)?", "/phim/:season"], component: () => import("pages/phim/_season.vue"), meta: {