From 7a1b3fbdefbf2c93571a3e80ac00641b90bdec11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chlo=C3=A9=20JACOB?= <chloe.jacob4@etu.unistra.fr> Date: Mon, 27 Feb 2023 15:45:53 +0100 Subject: [PATCH] automatiser le like sur la vue 'post/X' --- src/redux/post/reducer.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/redux/post/reducer.ts b/src/redux/post/reducer.ts index 04adc12..a92bde8 100644 --- a/src/redux/post/reducer.ts +++ b/src/redux/post/reducer.ts @@ -2,6 +2,7 @@ import { Instalike } from '@jmetterrothan/instalike'; import { Reducer } from 'redux'; import { PostAction, SET_POST } from './actions'; +import { SetLikeFeedAction, SetUnlikeFeedAction, LIKE_POST_FEED, UNLIKE_POST_FEED } from '../feed/actions' type PostState = { data?: Instalike.Post; @@ -11,10 +12,20 @@ const intialState: PostState = { data: undefined, }; -const postReducer: Reducer<PostState, PostAction> = (state = intialState, action) => { +const postReducer: Reducer<PostState, PostAction | SetLikeFeedAction | SetUnlikeFeedAction> = (state = intialState, action) => { switch (action.type) { case SET_POST: return { ...state, data: action.payload }; + case LIKE_POST_FEED: + if(state.data) { + return {...state, data: {...state.data, viewerHasLiked: true, likesCount: state.data.likesCount + 1 } }; + } + return state; + case UNLIKE_POST_FEED: + if(state.data) { + return {...state, data: {...state.data, viewerHasLiked: false, likesCount: state.data.likesCount - 1 } }; + } + return state; default: return state; } -- GitLab