From a37d1548a1a876614734768069526bcb489bbe57 Mon Sep 17 00:00:00 2001 From: zuies Date: Mon, 5 Feb 2024 12:44:46 +0300 Subject: [PATCH] update payload for privateMessage --- .../create-new-announcements.tsx | 48 +++++++++-------- .../edit-announcements/index.tsx | 51 +++++++++---------- 2 files changed, 48 insertions(+), 51 deletions(-) diff --git a/src/pages/announcements/create-new-announcements.tsx b/src/pages/announcements/create-new-announcements.tsx index d086fa7f..b0e73836 100644 --- a/src/pages/announcements/create-new-announcements.tsx +++ b/src/pages/announcements/create-new-announcements.tsx @@ -160,48 +160,46 @@ function CreateNewAnnouncements() { }) => { if (!platformId) return; - let rolePrivateAnnouncements; - let userPrivateAnnouncements; - const commonData = { platformId: platformId, template: message, }; + let privateAnnouncementsOptions: { + roleIds: string[]; + userIds: string[]; + } = { + roleIds: [], + userIds: [], + }; + if (selectedRoles && selectedRoles.length > 0) { setRoles(selectedRoles); - - rolePrivateAnnouncements = { - ...commonData, - options: { - roleIds: selectedRoles.map((role) => - role.roleId.toString() - ), - }, - }; + privateAnnouncementsOptions.roleIds = selectedRoles.map( + (role) => role.roleId.toString() + ); } if (selectedUsers && selectedUsers.length > 0) { setUsers(selectedUsers); + privateAnnouncementsOptions.userIds = selectedUsers.map( + (user) => user.discordId + ); + } - userPrivateAnnouncements = { + if ( + privateAnnouncementsOptions.roleIds.length > 0 || + privateAnnouncementsOptions.userIds.length > 0 + ) { + const combinedPrivateAnnouncement = { ...commonData, - options: { - userIds: selectedUsers.map((user) => user.discordId), - }, + options: privateAnnouncementsOptions, }; - } - - const announcements = []; - if (rolePrivateAnnouncements) - announcements.push(rolePrivateAnnouncements); - if (userPrivateAnnouncements) - announcements.push(userPrivateAnnouncements); - setPrivateAnnouncements(announcements); + setPrivateAnnouncements([combinedPrivateAnnouncement]); + } }} /> - { setScheduledAt(selectedTime); diff --git a/src/pages/announcements/edit-announcements/index.tsx b/src/pages/announcements/edit-announcements/index.tsx index 315e1f35..7b0bc067 100644 --- a/src/pages/announcements/edit-announcements/index.tsx +++ b/src/pages/announcements/edit-announcements/index.tsx @@ -2,7 +2,7 @@ import React, { useContext, useEffect, useMemo, useState } from 'react'; import { defaultLayout } from '../../../layouts/defaultLayout'; import SEO from '../../../components/global/SEO'; import { useRouter } from 'next/router'; -import TcPrivateMessaageContainer from '../../../components/announcements/create/privateMessaageContainer'; +import TcPrivateMessageContainer from '../../../components/announcements/create/privateMessaageContainer'; import TcPublicMessaageContainer from '../../../components/announcements/create/publicMessageContainer'; import TcScheduleAnnouncement from '../../../components/announcements/create/scheduleAnnouncement'; import TcSelectPlatform from '../../../components/announcements/create/selectPlatform'; @@ -207,7 +207,7 @@ function Index() { }); }} /> - { if (!platformId) return; - let rolePrivateAnnouncements; - let userPrivateAnnouncements; - const commonData = { platformId: platformId, template: message, }; + let privateAnnouncementsOptions: { + roleIds: string[]; + userIds: string[]; + } = { + roleIds: [], + userIds: [], + }; + if (selectedRoles && selectedRoles.length > 0) { setRoles(selectedRoles); - - rolePrivateAnnouncements = { - ...commonData, - options: { - roleIds: selectedRoles.map((role) => - role.roleId.toString() - ), - }, - }; + privateAnnouncementsOptions.roleIds = selectedRoles.map( + (role) => role.roleId.toString() + ); } if (selectedUsers && selectedUsers.length > 0) { setUsers(selectedUsers); + privateAnnouncementsOptions.userIds = selectedUsers.map( + (user) => user.discordId + ); + } - userPrivateAnnouncements = { + if ( + privateAnnouncementsOptions.roleIds.length > 0 || + privateAnnouncementsOptions.userIds.length > 0 + ) { + const combinedPrivateAnnouncement = { ...commonData, - options: { - userIds: selectedUsers.map((user) => user.discordId), - }, + options: privateAnnouncementsOptions, }; - } - const announcements = []; - if (rolePrivateAnnouncements) - announcements.push(rolePrivateAnnouncements); - if (userPrivateAnnouncements) - announcements.push(userPrivateAnnouncements); - - setPrivateAnnouncements(announcements); + setPrivateAnnouncements([combinedPrivateAnnouncement]); + } }} />