Skip to content

Commit

Permalink
fix issue on announcements
Browse files Browse the repository at this point in the history
  • Loading branch information
zuies committed Jan 22, 2024
1 parent 9208266 commit 827d489
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 70 deletions.
105 changes: 58 additions & 47 deletions src/components/announcements/TcAnnouncementsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -318,55 +318,66 @@ function TcAnnouncementsTable({
const renderTableBody = () => {
if (isLoading) {
return (
<TableRow>
<TableCell
colSpan={6}
style={{ textAlign: 'center' }}
sx={{ borderBottom: 'none' }}
className="min-h-[70vh] pt-[25dvh]"
data-testid="loading-indicator"
>
<Loading />
</TableCell>
</TableRow>
);
}

return announcements.map((announcement, index) => (
<TableRow
key={announcement.id}
className={`my-5 ${index % 2 === 0 ? 'bg-gray-100' : ''}`}
>
{['title', 'channels', 'users', 'roles', 'scheduledAt', 'actions'].map(
(cellType, cellIndex, array) => (
<TableBody>
<TableRow>
<TableCell
key={cellType}
sx={{
padding: '14px 16px',
borderBottom: index % 2 !== 0 ? '1px solid #f3f4f6' : 'none',
borderTop: index % 2 !== 0 ? '1px solid #f3f4f6' : 'none',
borderLeft:
cellIndex === 0 && index % 2 !== 0
? '1px solid #f3f4f6'
: 'none',
borderRight:
cellIndex === array.length - 1 && index % 2 !== 0
? '1px solid #f3f4f6'
: 'none',
borderTopLeftRadius: cellIndex === 0 ? '5px' : '0',
borderBottomLeftRadius: cellIndex === 0 ? '5px' : '0',
borderTopRightRadius:
cellIndex === array.length - 1 ? '5px' : '0',
borderBottomRightRadius:
cellIndex === array.length - 1 ? '5px' : '0',
}}
colSpan={6}
style={{ textAlign: 'center' }}
sx={{ borderBottom: 'none' }}
className="min-h-[70vh] pt-[25dvh]"
data-testid="loading-indicator"
>
{renderTableCell(announcement, cellType)}
<Loading />
</TableCell>
)
)}
</TableRow>
));
</TableRow>
</TableBody>
);
}

return (
<TableBody>
{announcements.map((announcement, index) => (
<TableRow
key={announcement.id}
className={`my-5 ${index % 2 === 0 ? 'bg-gray-100' : ''}`}
>
{[
'title',
'channels',
'users',
'roles',
'scheduledAt',
'actions',
].map((cellType, cellIndex, array) => (
<TableCell
key={cellType}
sx={{
padding: '14px 16px',
borderBottom: index % 2 !== 0 ? '1px solid #f3f4f6' : 'none',
borderTop: index % 2 !== 0 ? '1px solid #f3f4f6' : 'none',
borderLeft:
cellIndex === 0 && index % 2 !== 0
? '1px solid #f3f4f6'
: 'none',
borderRight:
cellIndex === array.length - 1 && index % 2 !== 0
? '1px solid #f3f4f6'
: 'none',
borderTopLeftRadius: cellIndex === 0 ? '5px' : '0',
borderBottomLeftRadius: cellIndex === 0 ? '5px' : '0',
borderTopRightRadius:
cellIndex === array.length - 1 ? '5px' : '0',
borderBottomRightRadius:
cellIndex === array.length - 1 ? '5px' : '0',
}}
>
{renderTableCell(announcement, cellType)}
</TableCell>
))}
</TableRow>
))}
</TableBody>
);
};

return (
Expand Down Expand Up @@ -452,7 +463,7 @@ function TcAnnouncementsTable({
></TableCell>
</TableRow>
</TableHead>
<TableBody> {renderTableBody()}</TableBody>
{renderTableBody()}
</Table>
<TcDialog
sx={{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Chip } from '@mui/material';
import React, { useEffect, useState } from 'react';
import TcAutocomplete from '../../../shared/TcAutocomplete';
import { useToken } from '../../../../context/TokenContext';
import useAppStore from '../../../../store/useStore';
import { FetchedData, IRoles } from '../../../../utils/interfaces';
import { useToken } from '../../../../context/TokenContext';
import { debounce, hexToRGBA, isDarkColor } from '../../../../helpers/helper';
import { debounce } from '../../../../helpers/helper';
import TcAutocomplete from '../../../shared/TcAutocomplete';
import { Chip } from '@mui/material';

interface ITcRolesAutoCompleteProps {
isEdit?: boolean;
Expand All @@ -29,7 +29,7 @@ function TcRolesAutoComplete({
const [selectedRoles, setSelectedRoles] = useState<IRoles[]>([]);

const [fetchedRoles, setFetchedRoles] = useState<FetchedData>({
limit: 8,
limit: 100,
page: 1,
results: [],
totalPages: 0,
Expand Down Expand Up @@ -92,16 +92,16 @@ function TcRolesAutoComplete({
if (inputValue === '') {
setFilteredRolesByName('');
setFetchedRoles({
limit: 8,
limit: 100,
page: 1,
results: [],
totalPages: 0,
totalResults: 0,
});

debouncedFetchDiscordRoles(platformId, 1, 8);
debouncedFetchDiscordRoles(platformId, 1, 100);
} else {
debouncedFetchDiscordRoles(platformId, 1, 8, inputValue);
debouncedFetchDiscordRoles(platformId, 1, 100, inputValue);
}
};

Expand Down Expand Up @@ -153,7 +153,13 @@ function TcRolesAutoComplete({
value={selectedRoles}
onChange={handleChange}
onInputChange={handleSearchChange}
isOptionEqualToValue={(option, value) => option.roleId === value.roleId}
disableCloseOnSelect
renderOption={(props, option) => (
<li {...props} key={option.roleId}>
{option.name}
</li>
)}
renderTags={(value, getTagProps) =>
value.map((option, index) => (
<Chip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ function TcUsersAutoComplete({
const [selectedUsers, setSelectedUsers] = useState<IUser[]>([]);

const [fetchedUsers, setFetchedUsers] = useState<FetchedData>({
limit: 8,
limit: 100,
page: 1,
results: [],
totalPages: 0,
totalResults: 0,
});
const [filteredRolesByName, setFilteredRolesByName] = useState<string>('');
const [filteredUsersByName, setFilteredUsersByName] = useState<string>('');
const [isInitialized, setIsInitialized] = useState<boolean>(false);

const fetchDiscordUsers = async (
Expand All @@ -54,7 +54,7 @@ function TcUsersAutoComplete({
});

if (ngu) {
setFilteredRolesByName(ngu);
setFilteredUsersByName(ngu);
setFetchedUsers(fetchedUsers);
} else {
setFetchedUsers((prevData: { results: any }) => {
Expand Down Expand Up @@ -90,18 +90,18 @@ function TcUsersAutoComplete({
if (!platformId) return;

if (inputValue === '') {
setFilteredRolesByName('');
setFilteredUsersByName('');
setFetchedUsers({
limit: 8,
limit: 100,
page: 1,
results: [],
totalPages: 0,
totalResults: 0,
});

debouncedFetchDiscordUsers(platformId, 1, 8);
debouncedFetchDiscordUsers(platformId, 1, 100);
} else {
debouncedFetchDiscordUsers(platformId, 1, 8, inputValue);
debouncedFetchDiscordUsers(platformId, 1, 100, inputValue);
}
};

Expand Down Expand Up @@ -153,7 +153,15 @@ function TcUsersAutoComplete({
value={selectedUsers}
onChange={handleChange}
onInputChange={handleSearchChange}
isOptionEqualToValue={(option, value) =>
option.discordId === value.discordId
}
disableCloseOnSelect
renderOption={(props, option) => (
<li {...props} key={option.discordId}>
{option.ngu}
</li>
)}
renderTags={(value, getTagProps) =>
value.map((option, index) => (
<Chip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ function TcScheduleAnnouncement({
}, [selectedDate, selectedTime]);

useEffect(() => {
if (isEdit) {
if (isEdit && preSelectedTime) {
const date = moment(preSelectedTime);

setSelectedDate(date.toDate());
setSelectedTime(date.toDate());
setDateTimeDisplay(date.format('D MMMM YYYY @ h A'));
}
}, [isEdit]);
}, [isEdit, preSelectedTime]);

return (
<div className="space-y-3">
Expand Down
2 changes: 0 additions & 2 deletions src/components/pages/pageIndex/HeatmapChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ const HeatmapChart = () => {
const fetchData = async () => {
setLoading(true);
try {
console.log({ selectedSubChannels });

if (platformId) {
const data = await fetchHeatmapData(
platformId,
Expand Down
6 changes: 4 additions & 2 deletions src/pages/announcements/edit-announcements/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ function Index() {
(channel) => channel.channelId
);
}
console.log({ channelIds });

await refreshData(platformId, 'channel', channelIds, undefined, false);
}
Expand All @@ -127,7 +126,9 @@ function Index() {
if (!id) return;
const fetchAnnouncement = async () => {
const data = await retrieveAnnouncementById(id);

setFetchedAnnouncements(data);
setScheduledAt(data.scheduledAt);
};

fetchAnnouncement();
Expand Down Expand Up @@ -155,9 +156,10 @@ function Index() {
try {
setLoading(true);
const data = await patchExistingAnnouncement(id, announcementsPayload);

if (data) {
showMessage('Announcement updated successfully', 'success');
router.push('/announcements');
location.replace('/announcements');
}
} catch (error) {
showMessage('Failed to create announcement', 'error');
Expand Down
2 changes: 0 additions & 2 deletions src/store/slices/announcementsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ const createAnnouncementsSlice: StateCreator<IAnnouncements> = (set, get) => ({
announcementPayload: CreateAnnouncementsPayload
) => {
try {
console.log({ id });

const { data } = await axiosInstance.patch(
`/announcements/${id}`,
announcementPayload
Expand Down

0 comments on commit 827d489

Please sign in to comment.