From 29604a1fc3b630e143beb887cb3002ef5b7f996b Mon Sep 17 00:00:00 2001 From: arraa Date: Thu, 21 Mar 2024 02:10:02 +0700 Subject: [PATCH] fix recommendations component, move component in folder page, and fix crud for history --- .../home => components/movies}/addMovie.tsx | 0 src/components/movies/cardMovieItem.tsx | 1 - .../home => components/movies}/nowPlaying.tsx | 0 src/components/movies/recommendations.tsx | 9 ++-- src/{pages/home => components/movies}/top.tsx | 0 .../home => components/movies}/topThree.tsx | 0 .../home => components/movies}/upcoming.tsx | 0 src/models/products/commands.ts | 44 ------------------- src/models/products/reducers.ts | 32 -------------- src/models/products/types.ts | 11 ----- src/pages/add/index.tsx | 2 +- src/pages/home/index.tsx | 10 ++--- src/pages/movies/cardMovie.tsx | 9 +++- src/pages/movies/{id}.tsx | 4 +- src/pages/update/{id}.tsx | 5 +-- 15 files changed, 22 insertions(+), 105 deletions(-) rename src/{pages/home => components/movies}/addMovie.tsx (100%) rename src/{pages/home => components/movies}/nowPlaying.tsx (100%) rename src/{pages/home => components/movies}/top.tsx (100%) rename src/{pages/home => components/movies}/topThree.tsx (100%) rename src/{pages/home => components/movies}/upcoming.tsx (100%) diff --git a/src/pages/home/addMovie.tsx b/src/components/movies/addMovie.tsx similarity index 100% rename from src/pages/home/addMovie.tsx rename to src/components/movies/addMovie.tsx diff --git a/src/components/movies/cardMovieItem.tsx b/src/components/movies/cardMovieItem.tsx index 81f217c..7f78edf 100644 --- a/src/components/movies/cardMovieItem.tsx +++ b/src/components/movies/cardMovieItem.tsx @@ -51,7 +51,6 @@ const CardMovieItem = ({ product }: CardMovieItemProps) => { const handleDelete = () => { if (product?.id) { - dispatch(command.products.delete(product?.id)); dispatch(command.history.delete(product?.id)); navigate('/movies'); } diff --git a/src/pages/home/nowPlaying.tsx b/src/components/movies/nowPlaying.tsx similarity index 100% rename from src/pages/home/nowPlaying.tsx rename to src/components/movies/nowPlaying.tsx diff --git a/src/components/movies/recommendations.tsx b/src/components/movies/recommendations.tsx index 3f0b581..b0b70b2 100644 --- a/src/components/movies/recommendations.tsx +++ b/src/components/movies/recommendations.tsx @@ -54,8 +54,9 @@ const Recommendations = (Recommendations: RecommendationsProps) => { return ( <> - {recommendations - ? ( + {recommendations?.length === 0 + ? no data + : ( { ))} - ) - : no data } + )} + ); }; diff --git a/src/pages/home/top.tsx b/src/components/movies/top.tsx similarity index 100% rename from src/pages/home/top.tsx rename to src/components/movies/top.tsx diff --git a/src/pages/home/topThree.tsx b/src/components/movies/topThree.tsx similarity index 100% rename from src/pages/home/topThree.tsx rename to src/components/movies/topThree.tsx diff --git a/src/pages/home/upcoming.tsx b/src/components/movies/upcoming.tsx similarity index 100% rename from src/pages/home/upcoming.tsx rename to src/components/movies/upcoming.tsx diff --git a/src/models/products/commands.ts b/src/models/products/commands.ts index 1bf34ea..2acc282 100644 --- a/src/models/products/commands.ts +++ b/src/models/products/commands.ts @@ -16,8 +16,6 @@ import { ProductsActionType } from './types.js'; import type { nowPlaying, - Product, - ProductDeleted, ProductDetailModel, ProductsAction, ProductsModel, @@ -26,34 +24,6 @@ import type { } from './types.js'; const productsCommand = { - delete: (id: number) => { - return (dispatch) => { - if (id) { - const value: ProductDeleted = { - id - }; - - dispatch({ - type: ProductsActionType.Delete, - value - }); - } - }; - }, - create: (product: Product) => { - return (dispatch) => { - if (product) { - const value: ProductsModel = { - products: [product] - }; - - dispatch({ - type: ProductsActionType.Create, - value - }); - } - }; - }, load: (options?: Readonly) => { return async (dispatch) => { try { @@ -163,20 +133,6 @@ const productsCommand = { console.error(err); } }; - }, - update: (product: Product) => { - return (dispatch) => { - if (product) { - const value: ProductsModel = { - products: [product] - }; - - dispatch({ - type: ProductsActionType.Update, - value - }); - } - }; } } satisfies Command; diff --git a/src/models/products/reducers.ts b/src/models/products/reducers.ts index ba6d410..679aab2 100644 --- a/src/models/products/reducers.ts +++ b/src/models/products/reducers.ts @@ -5,7 +5,6 @@ import { ProductsActionType } from './types.js'; import type { nowPlaying, - Product, ProductDetailModel, ProductsAction, ProductsModel, @@ -17,40 +16,9 @@ const productsReducer = ( state: ProductsModel = {}, action: Readonly ): ProductsModel => { - let newProducts: Product[] = []; switch (action.type) { case ProductsActionType.Load: return !state.products ? { ...action.value } : { ...state }; - case ProductsActionType.Delete: - const productIdToDelete = action.value; // Assuming action.value contains the ID of the product to delete - - return { - ...state, - products: state.products - ? state.products.filter( - (product) => product.id !== productIdToDelete.id - ) - : [] - }; - case ProductsActionType.Create: - newProducts = action.value?.products ?? []; - - return { - ...state, - products: state.products - ? [...state.products, ...newProducts] - : newProducts - }; - case ProductsActionType.Update: - const updatedProducts = action.value?.products ?? []; - const updatedProductId = updatedProducts[0]?.id; - - return { - ...state, - products: (state.products ?? []).map((product: Product) => (product.id === updatedProductId - ? { ...product, ...updatedProducts[0] } - : product)) - }; default: return state; diff --git a/src/models/products/types.ts b/src/models/products/types.ts index 06e017a..72f2c33 100644 --- a/src/models/products/types.ts +++ b/src/models/products/types.ts @@ -53,22 +53,13 @@ interface ProductDetailModel { enum ProductsActionType { Load = 'products-load', - Delete = 'product-delete', Detail = 'products-detail', - Create = 'products-create', - Update = 'products-update', NowPlayingLoad = 'products-now_playing', UpcomingLoad = 'products-upcoming', TopratedLoad = 'product-toprated' } type ProductsAction = { - type: ProductsActionType.Create - value?: ProductsModel -} | { - type: ProductsActionType.Delete - value: ProductDeleted -} | { type: ProductsActionType.Detail value?: ProductDetailModel } | { @@ -83,8 +74,6 @@ type ProductsAction = { } | { type: ProductsActionType.UpcomingLoad value?: upComing -} | { type: ProductsActionType.Update - value?: ProductsModel } ; export { ProductsActionType }; diff --git a/src/pages/add/index.tsx b/src/pages/add/index.tsx index 97006f8..ec20b31 100644 --- a/src/pages/add/index.tsx +++ b/src/pages/add/index.tsx @@ -135,7 +135,7 @@ const AddMovie: PageComponent = () => { title: newTitle }; - dispatch(command.products.create(product)); + // dispatch(command.products.create(product)); dispatch(command.history.create(product)); }; diff --git a/src/pages/home/index.tsx b/src/pages/home/index.tsx index 0e7a1da..4898746 100644 --- a/src/pages/home/index.tsx +++ b/src/pages/home/index.tsx @@ -6,11 +6,11 @@ import type { PageComponent } from '@nxweb/react'; import { useSettings } from '@hooks/use-settings'; -import AddMovie from './addMovie'; -import NowPlaying from './nowPlaying'; -import TopSec from './top'; -import TopThree from './topThree'; -import Upcoming from './upcoming'; +import AddMovie from '../../components/movies/addMovie'; +import NowPlaying from '../../components/movies/nowPlaying'; +import TopSec from '../../components/movies/top'; +import TopThree from '../../components/movies/topThree'; +import Upcoming from '../../components/movies/upcoming'; const Home: PageComponent = () => { const { diff --git a/src/pages/movies/cardMovie.tsx b/src/pages/movies/cardMovie.tsx index da7824f..157fd37 100644 --- a/src/pages/movies/cardMovie.tsx +++ b/src/pages/movies/cardMovie.tsx @@ -6,21 +6,26 @@ import { useEffect } from 'react'; import { Grid } from '@mui/material'; import CardMovieItem from '@components/movies/cardMovieItem'; +import type { Product } from '@models/products/types'; import { useCommand, useStore } from '@models/store.js'; const CardMovie = () => { - const [state, dispatch] = useStore((store) => store.products); + const [state, dispatch] = useStore((store) => store); const command = useCommand((cmd) => cmd); useEffect(() => { dispatch(command.products.load()).catch((err: unknown) => { console.error(err); }); + + dispatch(command.history.load()); }, []); + const product: Product[] = [...state.products?.products ?? [], ...state.history?.history ?? []]; + return ( - {state?.products?.map((row) => ( + {product.map((row) => ( diff --git a/src/pages/movies/{id}.tsx b/src/pages/movies/{id}.tsx index afa3271..f74b6be 100644 --- a/src/pages/movies/{id}.tsx +++ b/src/pages/movies/{id}.tsx @@ -33,7 +33,7 @@ const Product: PageComponent = () => { const command = useCommand((cmd) => cmd); const product = useMemo(() => { - let product = state?.products?.products?.find( + let product = state?.history?.history?.find( (o) => o.id.toString() === id ); if (!product) { @@ -54,7 +54,7 @@ const Product: PageComponent = () => { const handleDelete = () => { if (product?.id) { - dispatch(command.products.delete(product?.id)); + // dispatch(command.products.delete(product?.id)); dispatch(command.history.delete(product?.id)); navigate('/movies'); } diff --git a/src/pages/update/{id}.tsx b/src/pages/update/{id}.tsx index f053fe6..48cf8ac 100644 --- a/src/pages/update/{id}.tsx +++ b/src/pages/update/{id}.tsx @@ -43,11 +43,11 @@ const UpdateMovie: PageComponent = () => { const command = useCommand((cmd) => cmd); useEffect(() => { - dispatch(command.products.load()); + dispatch(command.history.load()); }, []); const currentMovie = useMemo( - () => state?.products?.products?.find((current) => current.id?.toString() === id), + () => state?.history?.history?.find((current) => current.id?.toString() === id), [state, id] ); @@ -174,7 +174,6 @@ const UpdateMovie: PageComponent = () => { title: newTitle }; - dispatch(command.products.update(product)); dispatch(command.history.update(product)); } };