Skip to content

Commit

Permalink
Fix sort.tsx to always register one processor
Browse files Browse the repository at this point in the history
  • Loading branch information
afshinm committed Dec 30, 2023
1 parent 20b2787 commit 1609c8c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/hooks/useSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function useSelector<T>(selector: (state) => T) {
});

return unsubscribe;
}, []);
}, [store, current]);

Check warning on line 18 in src/hooks/useSelector.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

return current;
}
4 changes: 2 additions & 2 deletions src/view/plugin/sort/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const SortColumn =
compare?: Comparator<TCell>,
) =>
(state) => {
let columns = state.sort ? [...state.sort.columns] : [];
let columns = state.sort ? structuredClone(state.sort.columns) : [];
const count = columns.length;
const column = columns.find((x) => x.index === index);
const exists = column !== undefined;
Expand Down Expand Up @@ -86,7 +86,7 @@ export const SortColumn =

export const SortToggle =
(index: number, multi: boolean, compare?: Comparator<TCell>) => (state) => {
const columns = state.sort ? [...state.sort.columns] : [];
const columns = state.sort ? structuredClone(state.sort.columns) : [];
const column = columns.find((x) => x.index === index);

if (!column) {
Expand Down
85 changes: 42 additions & 43 deletions src/view/plugin/sort/sort.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { h, JSX } from 'preact';

import { classJoin, className } from '../../../util/className';
import { ProcessorType } from '../../../pipeline/processor';
import { PipelineProcessor, ProcessorType } from '../../../pipeline/processor';

Check warning on line 4 in src/view/plugin/sort/sort.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
import NativeSort from '../../../pipeline/sort/native';
import { Comparator, TCell, TColumnSort } from '../../../types';
import * as actions from './actions';
Expand Down Expand Up @@ -38,25 +38,52 @@ export function Sort(
} & SortConfig,
) {
const config = useConfig();
const { dispatch } = useStore();

Check warning on line 41 in src/view/plugin/sort/sort.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const _ = useTranslator();
const [direction, setDirection] = useState(0);
const [processor, setProcessor] = useState<NativeSort | ServerSort>(
undefined,
);
const state = useSelector((state) => state.sort);
const { dispatch } = useStore();
const sortConfig = config.sort as GenericSortConfig;
const state = useSelector((state) => state.sort);

Check warning on line 45 in src/view/plugin/sort/sort.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 45 in src/view/plugin/sort/sort.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const processorType =
typeof sortConfig?.server === 'object'
? ProcessorType.ServerSort
: ProcessorType.Sort;

Check warning on line 49 in src/view/plugin/sort/sort.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

useEffect(() => {
const processor = getOrCreateSortProcessor();
if (processor) setProcessor(processor);
}, []);
const getSortProcessor = () => {
const processors = config.pipeline.getStepsByType(processorType);

Check warning on line 52 in src/view/plugin/sort/sort.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
if (processors.length) {
return processors[0];

Check warning on line 54 in src/view/plugin/sort/sort.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 55 in src/view/plugin/sort/sort.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
return undefined;

Check warning on line 56 in src/view/plugin/sort/sort.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
};

Check warning on line 57 in src/view/plugin/sort/sort.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

const createSortProcessor = () => {
if (processorType === ProcessorType.ServerSort) {
return new ServerSort({
columns: state ? state.columns : [],
...sortConfig.server,
});
}

return new NativeSort({
columns: state ? state.columns : [],
});
};

const getOrCreateSortProcessor = (): PipelineProcessor<any, any> => {
const existingSortProcessor = getSortProcessor();
if (existingSortProcessor) {
return existingSortProcessor;
}

return createSortProcessor();
};

useEffect(() => {
config.pipeline.register(processor);
const processor = getOrCreateSortProcessor();
config.pipeline.tryRegister(processor);

return () => config.pipeline.unregister(processor);
}, [config, processor]);
}, [config]);

/**
* Sets the internal state of component
Expand All @@ -74,6 +101,8 @@ export function Sort(
}, [state]);

useEffect(() => {
const processor = getSortProcessor();

if (!processor) return;
if (!state) return;

Expand All @@ -82,36 +111,6 @@ export function Sort(
});
}, [state]);

const getOrCreateSortProcessor = (): NativeSort | null => {
let processorType = ProcessorType.Sort;

if (sortConfig && typeof sortConfig.server === 'object') {
processorType = ProcessorType.ServerSort;
}

const processors = config.pipeline.getStepsByType(processorType);

if (processors.length === 0) {
// my assumption is that we only have ONE sorting processor in the
// entire pipeline and that's why I'm displaying a warning here
let processor;

if (processorType === ProcessorType.ServerSort) {
processor = new ServerSort({
columns: state ? state.columns : [],
...sortConfig.server,
});
} else {
processor = new NativeSort({
columns: state ? state.columns : [],
});
}
return processor;
}

return null;
};

const changeDirection = (e: JSX.TargetedMouseEvent<HTMLButtonElement>) => {
e.preventDefault();
e.stopPropagation();
Expand All @@ -125,7 +124,7 @@ export function Sort(
);
};

const getSortClassName = (direction) => {
const getSortClassName = (direction: number) => {
if (direction === 1) {
return 'asc';
} else if (direction === -1) {
Expand Down

0 comments on commit 1609c8c

Please sign in to comment.