From 43f4474e97d475833683706e6f93efd7153ffe00 Mon Sep 17 00:00:00 2001 From: David Rosendorf Date: Tue, 3 Sep 2024 18:40:16 +0100 Subject: [PATCH] UI: Blacklist app crash fix and other changes (#666) * fix: handle when no view blacklists auth * ui: blacklist preset time menu to buttons, minor fixes --- .../Blacklist/BlacklistRecordCreateDialog.js | 67 +++++++------------ .../Blacklist/BlacklistRecordGrid.js | 14 ++-- rcongui/src/components/GameView/index.js | 6 +- .../map-rotation/map-rotation-config.jsx | 8 ++- .../src/components/PlayerView/playerView.js | 28 +++++--- rcongui/src/components/VipDialog/index.js | 46 ++----------- .../components/shared/time-picker-buttons.jsx | 44 ++++++++++++ 7 files changed, 105 insertions(+), 108 deletions(-) create mode 100644 rcongui/src/components/shared/time-picker-buttons.jsx diff --git a/rcongui/src/components/Blacklist/BlacklistRecordCreateDialog.js b/rcongui/src/components/Blacklist/BlacklistRecordCreateDialog.js index 29bbf5239..2a3ec1cca 100644 --- a/rcongui/src/components/Blacklist/BlacklistRecordCreateDialog.js +++ b/rcongui/src/components/Blacklist/BlacklistRecordCreateDialog.js @@ -18,22 +18,13 @@ import { import moment from "moment"; import { getServerStatus, getSharedMessages } from "../../utils/fetchUtils"; import TextHistory from "../textHistory"; +import { TimePickerButtons } from "../shared/time-picker-buttons"; -const ONE_HOUR = 60 * 60; -const ONE_DAY = ONE_HOUR * 24; const presetTimes = [ - { label: "1 hour", value: ONE_HOUR }, - { label: "6 hours", value: ONE_HOUR * 6 }, - { label: "12 hours", value: ONE_HOUR * 12 }, - { label: "1 day", value: ONE_DAY }, - { label: "2 days", value: ONE_DAY * 2 }, - { label: "3 days", value: ONE_DAY * 3 }, - { label: "5 days", value: ONE_DAY * 5 }, - { label: "7 days", value: ONE_DAY * 7 }, - { label: "14 days", value: ONE_DAY * 14 }, - { label: "30 days", value: ONE_DAY * 30 }, - { label: "365 days", value: ONE_DAY * 365 }, - { label: "Never", value: null }, + [1, "hour"], + [1, "day"], + [1, "week"], + [1, "month"], ]; function BlacklistServerWarning({ blacklist, currentServer }) { @@ -104,11 +95,14 @@ export default function BlacklistRecordCreateDialog({ React.useEffect(() => { if (open) { const messageType = "punishments"; + const locallyStoredMessages = new TextHistory(messageType).getTexts(); + setPunishMessages(locallyStoredMessages); + getServerStatus() .then((server) => setCurrentServer(server)) .catch(() => setCurrentServer({})); + getSharedMessages(messageType).then((sharedMessages) => { - const locallyStoredMessages = new TextHistory(messageType).getTexts(); setPunishMessages(sharedMessages.concat(locallyStoredMessages)); }); } @@ -134,7 +128,7 @@ export default function BlacklistRecordCreateDialog({ }, [open]); React.useEffect(() => { - if (blacklists.length == 1) { + if (blacklists?.length == 1) { setBlacklist(blacklists[0]); } }); @@ -212,7 +206,7 @@ export default function BlacklistRecordCreateDialog({ /> {/* EXPIRY */} - + - - + + {presetTimes.map(([amount, unit], index) => ( + { + setExpiresAt( + moment(timestamp).format("YYYY-MM-DDTHH:mm") + ); + }} + /> + ))} {/* REASON */} diff --git a/rcongui/src/components/Blacklist/BlacklistRecordGrid.js b/rcongui/src/components/Blacklist/BlacklistRecordGrid.js index ff5c23e3b..71cdc3e84 100644 --- a/rcongui/src/components/Blacklist/BlacklistRecordGrid.js +++ b/rcongui/src/components/Blacklist/BlacklistRecordGrid.js @@ -38,12 +38,10 @@ const BlacklistRecordTile = ({ onDelete, }) => { const [showAll, setShowAll] = React.useState(false); - const [isExpired, setIsExpired] = React.useState(!record.get("is_active")); - const [expiresAt, setExpiresAt] = React.useState( - record.get("expires_at") ? moment(record.get("expires_at")) : null - ); const myClasses = useStyles(); + const expiresAt = record.get("expires_at") ? moment(record.get("expires_at")) : null + const isExpired = !record.get("is_active") const player = record.get("player"); const playerNames = player.get("names", List()); const firstName = playerNames.get(0) ? playerNames.get(0).get("name") : undefined; @@ -243,8 +241,6 @@ const BlacklistRecordTile = ({ isExpired={isExpired} onEdit={() => onEdit(record)} onExpire={() => { - setIsExpired(true); - setExpiresAt(now); onExpire(record); }} onDelete={() => onDelete(record)} @@ -288,8 +284,8 @@ const BlacklistRecordGrid = withWidth()( .then((response) => showResponse(response, `Record ${recordId} was edited`, true) ) + .then(result => result && !result.failed && onRefresh()) .catch(handle_http_errors) - .then(onRefresh); } function onExpireRecord(record) { @@ -300,8 +296,8 @@ const BlacklistRecordGrid = withWidth()( .then((response) => showResponse(response, `Record ${record.get("id")} was edited`, true) ) + .then(result => result && !result.failed && onRefresh()) .catch(handle_http_errors) - .then(onRefresh); } function onDeleteRecord(record) { @@ -311,8 +307,8 @@ const BlacklistRecordGrid = withWidth()( .then((response) => showResponse(response, `Record ${record.get("id")} was deleted`, true) ) + .then(result => result && !result.failed && onRefresh()) .catch(handle_http_errors) - .then(onRefresh); } const size = { diff --git a/rcongui/src/components/GameView/index.js b/rcongui/src/components/GameView/index.js index ce73064ef..65d60e698 100644 --- a/rcongui/src/components/GameView/index.js +++ b/rcongui/src/components/GameView/index.js @@ -577,8 +577,10 @@ const GameView = ({ classes: globalClasses }) => { async function handleBlacklistOpen(player) { const blacklists = await getBlacklists(); - setBlacklists(blacklists); - setBlacklistDialogOpen(true) + if (blacklists) { + setBlacklists(blacklists); + setBlacklistDialogOpen(true) + } } function selectedPlayersToRows() { diff --git a/rcongui/src/components/MapManager/map-rotation/map-rotation-config.jsx b/rcongui/src/components/MapManager/map-rotation/map-rotation-config.jsx index 78faf6902..639d88204 100644 --- a/rcongui/src/components/MapManager/map-rotation/map-rotation-config.jsx +++ b/rcongui/src/components/MapManager/map-rotation/map-rotation-config.jsx @@ -48,9 +48,11 @@ const MapRotationSettings = () => { Will reset to default enabled when server restarts. } checked={shuffleEnabled} - handleChange={(v) => { - toggleShuffleEnabled(!shuffleEnabled); - setShuffleEnabled(v); + handleChange={async (enabled) => { + const result = await toggleShuffleEnabled(!shuffleEnabled); + if (result && !result.failed) { + setShuffleEnabled(enabled); + } }} /> diff --git a/rcongui/src/components/PlayerView/playerView.js b/rcongui/src/components/PlayerView/playerView.js index 7d50d7def..04d6fbe18 100644 --- a/rcongui/src/components/PlayerView/playerView.js +++ b/rcongui/src/components/PlayerView/playerView.js @@ -119,7 +119,9 @@ class PlayerView extends Component { async handleBlacklistOpen(player) { const blacklists = await getBlacklists(); - this.setState({ blacklists, blacklistOpen: true, playerTarget: player }) + if (blacklists) { + this.setState({ blacklists, blacklistOpen: true, playerTarget: player }) + } } handleVipDialogOpen(player) { @@ -131,28 +133,34 @@ class PlayerView extends Component { async getVips() { const vips = await getVips(); - this.setState({ vipPlayers: vips }) + if (vips) { + this.setState({ vipPlayers: vips }) + } } async addPlayerVip(player, expiresAt, forward) { // action - await addPlayerVip({ + const result = await addPlayerVip({ player_id: player.get("player_id"), description: player.get("name"), expiration: expiresAt, forward: forward, }) - // update state - await this.loadPlayers() - await this.getVips() + if (result) { + // update state + await this.loadPlayers() + await this.getVips() + } } async removePlayerVip(player) { // action - await removePlayerVip({ player_id: player.get("player_id") }) - // update state - await this.loadPlayers() - await this.getVips() + const result = await removePlayerVip({ player_id: player.get("player_id") }) + if (result) { + // update state + await this.loadPlayers() + await this.getVips() + } } unBan(ban) { diff --git a/rcongui/src/components/VipDialog/index.js b/rcongui/src/components/VipDialog/index.js index b6dbf2385..c1959cff5 100644 --- a/rcongui/src/components/VipDialog/index.js +++ b/rcongui/src/components/VipDialog/index.js @@ -2,7 +2,6 @@ import React, { useEffect, useState } from "react"; import { Box, Button, - ButtonGroup, Dialog, DialogActions, DialogContent, @@ -14,52 +13,15 @@ import MomentUtils from "@date-io/moment"; import moment from "moment"; import { PlayerVipSummary } from "./PlayerVipSummary"; import { ForwardCheckBox } from "../commonComponent"; +import { TimePickerButtons } from "../shared/time-picker-buttons"; -const vipButtons = [ +const presetTimes = [ [2, "hours"], [1, "day"], [1, "week"], [1, "month"] ]; -const VipTimeButtons = ({ - amount, - unit, - expirationTimestamp, - setExpirationTimestamp, -}) => { - - const adjustTimestamp = (amount, unit) => { - const after = moment(expirationTimestamp).add(amount, unit); - const now = moment(); - - if (after.isBefore(now)) { - setExpirationTimestamp(now.format()) - return; - } - - setExpirationTimestamp(after.format()); - }; - - const setTimestamp = (amount, unit) => { - setExpirationTimestamp(moment().add(amount, unit).format()); - }; - - return ( - - - - - - ); -}; - /** * * @param open [NEW] boolean - signaling opened/closed dialog window @@ -133,8 +95,8 @@ export function VipExpirationDialog({ open, vips, onDeleteVip, handleClose, hand - {vipButtons.map(([amount, unit], index) => ( - ( + { + + const adjustTimestamp = (amount, unit) => { + const after = moment(expirationTimestamp).add(amount, unit); + const now = moment(); + + if (after.isBefore(now)) { + setExpirationTimestamp(now.format()) + return; + } + + setExpirationTimestamp(after.format()); + }; + + const setTimestamp = (amount, unit) => { + setExpirationTimestamp(moment().add(amount, unit).format()); + }; + + return ( + + + + + + ); +}; \ No newline at end of file