From ba8984b7c5fc0bc41d8872360cfc9cdbd1f4f9bb Mon Sep 17 00:00:00 2001 From: amel Date: Fri, 25 Apr 2025 17:37:06 +0800 Subject: [PATCH] upd:banner home deskripsi: - load banner di halaman home - nb : blm selesai No Issues --- components/home/carouselHome.tsx | 18 +++++++++++++++++- constants/Styles.ts | 1 + lib/api.ts | 7 ++++++- lib/bannerSlice.ts | 29 +++++++++++++++++++++++++++++ lib/store.ts | 11 +++++++++-- 5 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 lib/bannerSlice.ts diff --git a/components/home/carouselHome.tsx b/components/home/carouselHome.tsx index a466451..f3d3351 100644 --- a/components/home/carouselHome.tsx +++ b/components/home/carouselHome.tsx @@ -1,14 +1,26 @@ import Styles from "@/constants/Styles"; +import { apiGetBanner } from "@/lib/api"; +import { setEntities } from "@/lib/bannerSlice"; +import { useAuthSession } from "@/providers/AuthProvider"; import React from "react"; import { Dimensions, Text, View } from "react-native"; import { useSharedValue } from "react-native-reanimated"; import Carousel, { ICarouselInstance } from "react-native-reanimated-carousel"; +import { useDispatch, useSelector } from "react-redux"; export default function CaraouselHome() { + const { decryptToken, token } = useAuthSession() const ref = React.useRef(null); const width = Dimensions.get("window").width; const data = [...new Array(6).keys()]; const progress = useSharedValue(0); + const dispatch = useDispatch() + const entities = useSelector((state: any) => state.banner) + + async function handleBannerView() { + const hasil = await decryptToken(String(token?.current)) + apiGetBanner({ user: hasil }).then((data) => dispatch(setEntities(data.data))); + } return ( @@ -16,12 +28,16 @@ export default function CaraouselHome() { ref={ref} width={width} height={width / 2.5} - data={data} + data={entities} loop={true} autoPlay={true} autoPlayInterval={5000} onProgressChange={progress} renderItem={({ index }) => ( + // BANNER DARMASABA diff --git a/constants/Styles.ts b/constants/Styles.ts index c3be293..37b29b3 100644 --- a/constants/Styles.ts +++ b/constants/Styles.ts @@ -257,6 +257,7 @@ const Styles = StyleSheet.create({ borderRadius: 15, backgroundColor: '#19345E', display: 'flex', + width: '92%' }, wrapGridContent: { shadowColor: '#171717', diff --git a/lib/api.ts b/lib/api.ts index 0124323..f46e780 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -20,11 +20,16 @@ export const apiGetProfile = async ({ id }: { id: string }) => { return response.data; }; -export const apiGetSearch = async ({ text, user }: { text: string, user:string }) => { +export const apiGetSearch = async ({ text, user }: { text: string, user: string }) => { const response = await api.get(`mobile/home/search?search=${text}&user=${user}`); return response.data; }; +export const apiGetBanner = async ({ user }: { user: string }) => { + const response = await api.get(`mobile/banner?user=${user}`); + return response.data; +}; + // export const createEntity = async (newEntity: any) => { // const response = await api.post('/entities', newEntity); diff --git a/lib/bannerSlice.ts b/lib/bannerSlice.ts new file mode 100644 index 0000000..0c8b057 --- /dev/null +++ b/lib/bannerSlice.ts @@ -0,0 +1,29 @@ +import { createSlice } from '@reduxjs/toolkit'; + +const bannerSlice = createSlice({ + name: 'banner', + initialState: [], + reducers: { + setEntities: (state, action) => { + return action.payload; + }, + + addEntity: (state: any, action: any) => { + state.push(action.payload); + }, + updateEntity: (state: any, action) => { + const { id, updatedEntity } = action.payload; + const index = state.findIndex((entity: any) => entity.id === id); + if (index !== -1) { + state[index] = updatedEntity; + } + }, + // removeEntity: (state, action) => { + // const idToRemove = action.payload; + // return state.filter((entity: any) => entity.id !== idToRemove); + // }, + }, +}); + +export const { setEntities, addEntity, updateEntity } = bannerSlice.actions; +export default bannerSlice.reducer; \ No newline at end of file diff --git a/lib/store.ts b/lib/store.ts index 740086e..2ed2e86 100644 --- a/lib/store.ts +++ b/lib/store.ts @@ -1,11 +1,18 @@ -import { configureStore } from '@reduxjs/toolkit'; +import { combineReducers, combineSlices, configureStore } from '@reduxjs/toolkit'; +import bannerReducer from './bannerSlice'; import entitiesReducer from './entitiesSlice'; + +const reducer = combineSlices({ + entitiesReducer,bannerReducer +}) + const store = configureStore({ reducer: { entities: entitiesReducer, + banner: bannerReducer, + } // Add other reducers as needed - }, }); export default store; \ No newline at end of file