From 3cfc4a4afa5d1251245c8bf9e275248c86f5f37b Mon Sep 17 00:00:00 2001 From: Sev Furneaux Date: Tue, 19 Nov 2024 12:08:24 +0000 Subject: [PATCH] Add moderator like --- .../react/livequestions/QuestionBox.jsx | 2 + .../react/livequestions/QuestionModerator.jsx | 179 ++++++++++-------- .../react/livequestions/StatisticsBox.jsx | 4 + 3 files changed, 105 insertions(+), 80 deletions(-) diff --git a/meinberlin/react/livequestions/QuestionBox.jsx b/meinberlin/react/livequestions/QuestionBox.jsx index 1f5affad3d..38a36e47ce 100644 --- a/meinberlin/react/livequestions/QuestionBox.jsx +++ b/meinberlin/react/livequestions/QuestionBox.jsx @@ -251,6 +251,8 @@ export default class QuestionBox extends React.Component { questions_api_url={this.props.questions_api_url} category_dict={this.props.category_dict} isModerator={this.props.isModerator} + handleLike={this.handleLike.bind(this)} + hasLikingPermission={this.props.hasLikingPermission} />} ) diff --git a/meinberlin/react/livequestions/QuestionModerator.jsx b/meinberlin/react/livequestions/QuestionModerator.jsx index 80033c173e..11b2b39fa7 100644 --- a/meinberlin/react/livequestions/QuestionModerator.jsx +++ b/meinberlin/react/livequestions/QuestionModerator.jsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react' +import React, { useState, useEffect, useCallback } from 'react' import django from 'django' import LikeCard from './LikeCard' import { classNames } from '../contrib/helpers' @@ -37,7 +37,7 @@ const QuestionModerator = (props) => { useEffect(() => { setLikeCount(props.likes.count) - setSessionLike(props.session_like) + setSessionLike(props.likes.session_like) }, [props.likes]) const toggleIsOnShortList = () => { @@ -83,87 +83,106 @@ const QuestionModerator = (props) => { .then(() => props.togglePollingPaused()) } + const handleErrors = (response) => { + if (!response.ok) { + throw new Error(response.statusText) + } + return response + } + + const handleLikeClick = useCallback(() => { + const newSessionLike = !sessionLike + props.handleLike(props.id, newSessionLike) + .then(handleErrors) + .then(() => { + setSessionLike(newSessionLike) + setLikeCount((prevLikes) => prevLikes + (newSessionLike ? 1 : -1)) + }) + .catch((error) => { + console.log(error.message) + }) + }, [sessionLike, props.handleLike, props.id]) + return ( - <> - -
- {props.displayIsHidden && ( -