fix: fetch states only when the dropdown is opened (#1684)

This commit is contained in:
Aaryan Khandelwal 2023-07-26 23:20:44 +05:30 committed by GitHub
parent fd9dcfa2ec
commit d6f3c2515a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 51 additions and 38 deletions

View File

@ -37,8 +37,7 @@ export const AutoCloseAutomation: React.FC<Props> = ({ projectDetails, handleCha
? () => stateService.getStates(workspaceSlug as string, projectId as string) ? () => stateService.getStates(workspaceSlug as string, projectId as string)
: null : null
); );
const states = getStatesList(stateGroups);
const states = getStatesList(stateGroups ?? {});
const options = states const options = states
?.filter((state) => state.group === "cancelled") ?.filter((state) => state.group === "cancelled")
@ -53,14 +52,14 @@ export const AutoCloseAutomation: React.FC<Props> = ({ projectDetails, handleCha
), ),
})); }));
const multipleOptions = options.length > 1; const multipleOptions = (options ?? []).length > 1;
const defaultState = stateGroups && stateGroups.cancelled ? stateGroups.cancelled[0].id : null; const defaultState = stateGroups && stateGroups.cancelled ? stateGroups.cancelled[0].id : null;
const selectedOption = states?.find( const selectedOption = states?.find(
(s) => s.id === projectDetails?.default_state ?? defaultState (s) => s.id === projectDetails?.default_state ?? defaultState
); );
const currentDefaultState = states.find((s) => s.id === defaultState); const currentDefaultState = states?.find((s) => s.id === defaultState);
const initialValues: Partial<IProject> = { const initialValues: Partial<IProject> = {
close_in: 1, close_in: 1,

View File

@ -34,7 +34,7 @@ export const ChangeIssueState: React.FC<Props> = ({ setIsPaletteOpen, issue, use
? () => stateService.getStates(workspaceSlug as string, projectId as string) ? () => stateService.getStates(workspaceSlug as string, projectId as string)
: null : null
); );
const states = getStatesList(stateGroups ?? {}); const states = getStatesList(stateGroups);
const submitChanges = useCallback( const submitChanges = useCallback(
async (formData: Partial<IIssue>) => { async (formData: Partial<IIssue>) => {

View File

@ -75,7 +75,7 @@ export const AllViews: React.FC<Props> = ({
? () => stateService.getStates(workspaceSlug as string, projectId as string) ? () => stateService.getStates(workspaceSlug as string, projectId as string)
: null : null
); );
const states = getStatesList(stateGroups ?? {}); const states = getStatesList(stateGroups);
const handleTrashBox = useCallback( const handleTrashBox = useCallback(
(isDragging: boolean) => { (isDragging: boolean) => {

View File

@ -29,7 +29,7 @@ import { PlusIcon } from "@heroicons/react/24/outline";
import { getStatesList } from "helpers/state.helper"; import { getStatesList } from "helpers/state.helper";
import { orderArrayBy } from "helpers/array.helper"; import { orderArrayBy } from "helpers/array.helper";
// types // types
import { IIssue, IIssueFilterOptions } from "types"; import { IIssue, IIssueFilterOptions, IState } from "types";
// fetch-keys // fetch-keys
import { import {
CYCLE_DETAILS, CYCLE_DETAILS,
@ -96,7 +96,7 @@ export const IssuesView: React.FC<Props> = ({
? () => stateService.getStates(workspaceSlug as string, projectId as string) ? () => stateService.getStates(workspaceSlug as string, projectId as string)
: null : null
); );
const states = getStatesList(stateGroups ?? {}); const states = getStatesList(stateGroups);
const { data: labels } = useSWR( const { data: labels } = useSWR(
workspaceSlug && projectId ? PROJECT_ISSUE_LABELS(projectId.toString()) : null, workspaceSlug && projectId ? PROJECT_ISSUE_LABELS(projectId.toString()) : null,
@ -184,7 +184,10 @@ export const IssuesView: React.FC<Props> = ({
// dragged item(or issue) // dragged item(or issue)
if (selectedGroup === "priority") draggedItem.priority = destinationGroup; if (selectedGroup === "priority") draggedItem.priority = destinationGroup;
else if (selectedGroup === "state") draggedItem.state = destinationGroup; else if (selectedGroup === "state") {
draggedItem.state = destinationGroup;
draggedItem.state_detail = states?.find((s) => s.id === destinationGroup) as IState;
}
} }
const sourceGroup = source.droppableId; // source group id const sourceGroup = source.droppableId; // source group id
@ -200,8 +203,8 @@ export const IssuesView: React.FC<Props> = ({
(prevData) => { (prevData) => {
if (!prevData) return prevData; if (!prevData) return prevData;
const sourceGroupArray = prevData[sourceGroup]; const sourceGroupArray = [...groupedByIssues[sourceGroup]];
const destinationGroupArray = groupedByIssues[destinationGroup]; const destinationGroupArray = [...groupedByIssues[destinationGroup]];
sourceGroupArray.splice(source.index, 1); sourceGroupArray.splice(source.index, 1);
destinationGroupArray.splice(destination.index, 0, draggedItem); destinationGroupArray.splice(destination.index, 0, draggedItem);
@ -229,7 +232,9 @@ export const IssuesView: React.FC<Props> = ({
user user
) )
.then((response) => { .then((response) => {
const sourceStateBeforeDrag = states.find((state) => state.name === source.droppableId); const sourceStateBeforeDrag = states?.find(
(state) => state.name === source.droppableId
);
if ( if (
sourceStateBeforeDrag?.group !== "completed" && sourceStateBeforeDrag?.group !== "completed" &&

View File

@ -34,7 +34,7 @@ export const IssueStateSelect: React.FC<Props> = ({ setIsOpen, value, onChange,
? () => stateService.getStates(workspaceSlug as string, projectId) ? () => stateService.getStates(workspaceSlug as string, projectId)
: null : null
); );
const states = getStatesList(stateGroups ?? {}); const states = getStatesList(stateGroups);
const options = states?.map((state) => ({ const options = states?.map((state) => ({
value: state.id, value: state.id,
@ -48,7 +48,7 @@ export const IssueStateSelect: React.FC<Props> = ({ setIsOpen, value, onChange,
})); }));
const selectedOption = states?.find((s) => s.id === value); const selectedOption = states?.find((s) => s.id === value);
const currentDefaultState = states.find((s) => s.default); const currentDefaultState = states?.find((s) => s.default);
return ( return (
<CustomSearchSelect <CustomSearchSelect

View File

@ -41,7 +41,7 @@ export const SidebarStateSelect: React.FC<Props> = ({
? () => stateService.getStates(workspaceSlug as string, projectId as string) ? () => stateService.getStates(workspaceSlug as string, projectId as string)
: null : null
); );
const states = getStatesList(stateGroups ?? {}); const states = getStatesList(stateGroups);
const selectedState = states?.find((s) => s.id === value); const selectedState = states?.find((s) => s.id === value);

View File

@ -52,7 +52,7 @@ export const ViewStateSelect: React.FC<Props> = ({
? () => stateService.getStates(workspaceSlug as string, issue.project) ? () => stateService.getStates(workspaceSlug as string, issue.project)
: null : null
); );
const states = getStatesList(stateGroups ?? {}); const states = getStatesList(stateGroups);
const options = states?.map((state) => ({ const options = states?.map((state) => ({
value: state.id, value: state.id,
@ -88,11 +88,13 @@ export const ViewStateSelect: React.FC<Props> = ({
className={className} className={className}
value={issue.state} value={issue.state}
onChange={(data: string) => { onChange={(data: string) => {
const oldState = states?.find((s) => s.id === issue.state);
const newState = states?.find((s) => s.id === data);
partialUpdateIssue( partialUpdateIssue(
{ {
state: data, state: data,
priority: issue.priority, state_detail: newState,
target_date: issue.target_date,
}, },
issue issue
); );
@ -109,9 +111,6 @@ export const ViewStateSelect: React.FC<Props> = ({
user user
); );
const oldState = states.find((s) => s.id === issue.state);
const newState = states.find((s) => s.id === data);
if (oldState?.group !== "completed" && newState?.group !== "completed") { if (oldState?.group !== "completed" && newState?.group !== "completed") {
trackEventServices.trackIssueMarkedAsDoneEvent( trackEventServices.trackIssueMarkedAsDoneEvent(
{ {

View File

@ -67,7 +67,7 @@ export const ViewForm: React.FC<Props> = ({
? () => stateService.getStates(workspaceSlug as string, projectId as string) ? () => stateService.getStates(workspaceSlug as string, projectId as string)
: null : null
); );
const states = getStatesList(stateGroups ?? {}); const states = getStatesList(stateGroups);
const { data: labels } = useSWR( const { data: labels } = useSWR(
workspaceSlug && projectId && (filters.labels ?? []).length > 0 workspaceSlug && projectId && (filters.labels ?? []).length > 0

View File

@ -49,7 +49,7 @@ export const SelectFilters: React.FC<Props> = ({
? () => stateService.getStates(workspaceSlug as string, projectId as string) ? () => stateService.getStates(workspaceSlug as string, projectId as string)
: null : null
); );
const statesList = getStatesList(states ?? {}); const statesList = getStatesList(states);
const { data: members } = useSWR( const { data: members } = useSWR(
projectId ? PROJECT_MEMBERS(projectId as string) : null, projectId ? PROJECT_MEMBERS(projectId as string) : null,
@ -103,7 +103,7 @@ export const SelectFilters: React.FC<Props> = ({
label: "State", label: "State",
value: statesList, value: statesList,
hasChildren: true, hasChildren: true,
children: statesList.map((state) => ({ children: statesList?.map((state) => ({
id: state.id, id: state.id,
label: ( label: (
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">

View File

@ -1,16 +1,25 @@
// types // types
import { IState, IStateResponse } from "types"; import { IState, IStateResponse } from "types";
export const orderStateGroups = (unorderedStateGroups: IStateResponse) => export const orderStateGroups = (
Object.assign( unorderedStateGroups: IStateResponse | undefined
): IStateResponse | undefined => {
if (!unorderedStateGroups) return undefined;
return Object.assign(
{ backlog: [], unstarted: [], started: [], completed: [], cancelled: [] }, { backlog: [], unstarted: [], started: [], completed: [], cancelled: [] },
unorderedStateGroups unorderedStateGroups
); );
};
export const getStatesList = (stateGroups: IStateResponse | undefined): IState[] | undefined => {
if (!stateGroups) return undefined;
export const getStatesList = (stateGroups: any): IState[] => {
// order the unordered state groups first // order the unordered state groups first
const orderedStateGroups = orderStateGroups(stateGroups); const orderedStateGroups = orderStateGroups(stateGroups);
if (!orderedStateGroups) return undefined;
// extract states from the groups and return them // extract states from the groups and return them
return Object.keys(orderedStateGroups) return Object.keys(orderedStateGroups)
.map((group) => [...orderedStateGroups[group].map((state: IState) => state)]) .map((group) => [...orderedStateGroups[group].map((state: IState) => state)])

View File

@ -125,21 +125,22 @@ const useIssuesView = () => {
? () => stateService.getStates(workspaceSlug as string, projectId as string) ? () => stateService.getStates(workspaceSlug as string, projectId as string)
: null : null
); );
const statesList = getStatesList(states ?? {}); const statesList = getStatesList(states);
const activeStatesList = statesList.filter( const activeStatesList = statesList?.filter(
(state) => state.group === "started" || state.group === "unstarted" (state) => state.group === "started" || state.group === "unstarted"
); );
const backlogStatesList = statesList.filter((state) => state.group === "backlog"); const backlogStatesList = statesList?.filter((state) => state.group === "backlog");
const stateIds = const stateIds =
filters && filters?.type === "active" filters && filters?.type === "active"
? activeStatesList.map((state) => state.id) ? activeStatesList?.map((state) => state.id)
: filters?.type === "backlog" : filters?.type === "backlog"
? backlogStatesList.map((state) => state.id) ? backlogStatesList?.map((state) => state.id)
: statesList.map((state) => state.id); : statesList?.map((state) => state.id);
const filteredStateIds = const filteredStateIds =
filters && filters?.state ? stateIds.filter((s) => filters.state?.includes(s)) : stateIds; (filters && filters?.state ? stateIds?.filter((s) => filters.state?.includes(s)) : stateIds) ??
[];
const emptyStatesObject: { [key: string]: [] } = {}; const emptyStatesObject: { [key: string]: [] } = {};
for (let i = 0; i < filteredStateIds.length; i++) { for (let i = 0; i < filteredStateIds.length; i++) {

View File

@ -49,8 +49,8 @@ const StatesSettings: NextPage = () => {
? () => stateService.getStates(workspaceSlug as string, projectId as string) ? () => stateService.getStates(workspaceSlug as string, projectId as string)
: null : null
); );
const orderedStateGroups = orderStateGroups(states ?? {}); const orderedStateGroups = orderStateGroups(states);
const statesList = getStatesList(orderedStateGroups ?? {}); const statesList = getStatesList(orderedStateGroups);
return ( return (
<> <>
@ -79,7 +79,7 @@ const StatesSettings: NextPage = () => {
<p className="text-custom-text-200">Manage the states of this project.</p> <p className="text-custom-text-200">Manage the states of this project.</p>
</div> </div>
<div className="col-span-12 space-y-8 sm:col-span-7"> <div className="col-span-12 space-y-8 sm:col-span-7">
{states && projectDetails ? ( {states && projectDetails && orderedStateGroups ? (
Object.keys(orderedStateGroups).map((key) => { Object.keys(orderedStateGroups).map((key) => {
if (orderedStateGroups[key].length !== 0) if (orderedStateGroups[key].length !== 0)
return ( return (
@ -114,7 +114,7 @@ const StatesSettings: NextPage = () => {
key={state.id} key={state.id}
index={index} index={index}
state={state} state={state}
statesList={statesList} statesList={statesList ?? []}
handleEditState={() => setSelectedState(state.id)} handleEditState={() => setSelectedState(state.id)}
handleDeleteState={() => setSelectDeleteState(state.id)} handleDeleteState={() => setSelectDeleteState(state.id)}
user={user} user={user}