Skip to content

Commit

Permalink
fix: keep change request type in table
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Nov 25, 2024
1 parent 9a269e3 commit 38f5678
Showing 1 changed file with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface IChangeRequestTableProps {

const defaultSort: SortingRule<string> & {
columns?: string[];
type?: 'open' | 'closed';
} = { id: 'createdAt', desc: true };

const StyledTabContainer = styled('div')({
Expand Down Expand Up @@ -76,6 +77,12 @@ export const ChangeRequestsTabs = ({
const { value: storedParams, setValue: setStoredParams } =
createLocalStorage(`${projectId}:ProjectChangeRequest`, defaultSort);

const initialChangeRequestType =
searchParams.get('type') || storedParams.type;
const [changeRequestType, setChangeRequestType] = useState<
'open' | 'closed'
>(initialChangeRequestType === 'closed' ? 'closed' : 'open');

const [openChangeRequests, closedChangeRequests] = useMemo(() => {
const open = changeRequests.filter(
(changeRequest) =>
Expand All @@ -97,14 +104,16 @@ export const ChangeRequestsTabs = ({
{
title: 'Change requests',
data: openChangeRequests,
type: 'open' as const,
},
{
title: 'Closed',
data: closedChangeRequests,
type: 'closed' as const,
},
];

const [activeTab, setActiveTab] = useState(0);
const activeTab =
tabs.find((tab) => tab.type === changeRequestType) || tabs[0];

const columns = useMemo(
() => [
Expand Down Expand Up @@ -193,7 +202,7 @@ export const ChangeRequestsTabs = ({
data: searchedData,
getSearchText,
getSearchContext,
} = useSearch(columns, searchValue, tabs[activeTab]?.data);
} = useSearch(columns, searchValue, activeTab?.data);

const data = useMemo(
() => (loading ? featuresPlaceholder : searchedData),
Expand Down Expand Up @@ -259,6 +268,7 @@ export const ChangeRequestsTabs = ({
if (searchValue) {
tableState.search = searchValue;
}
tableState.type = changeRequestType;

setSearchParams(tableState, {
replace: true,
Expand All @@ -267,9 +277,10 @@ export const ChangeRequestsTabs = ({
...params,
id: sortBy[0].id,
desc: sortBy[0].desc || false,
type: changeRequestType,
}));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [loading, sortBy, searchValue, setSearchParams]);
}, [loading, sortBy, searchValue, setSearchParams, changeRequestType]);

return (
<PageContent
Expand All @@ -281,18 +292,20 @@ export const ChangeRequestsTabs = ({
titleElement={
<StyledTabContainer>
<Tabs
value={tabs[activeTab]?.title}
value={activeTab?.title}
indicatorColor='primary'
textColor='primary'
variant='scrollable'
allowScrollButtonsMobile
>
{tabs.map((tab, index) => (
{tabs.map((tab) => (
<StyledTabButton
key={tab.title}
label={`${tab.title} (${tab.data.length})`}
value={tab.title}
onClick={() => setActiveTab(index)}
onClick={() =>
setChangeRequestType(tab.type)
}
/>
))}
</Tabs>
Expand Down

0 comments on commit 38f5678

Please sign in to comment.