Skip to content

Commit

Permalink
Avoid mutating react state, change exclude from string[] to Set<string>
Browse files Browse the repository at this point in the history
Also have /mirrors/errors link to /mirrors/edit
  • Loading branch information
serprex committed Jan 24, 2024
1 parent 23a97b1 commit 07f4551
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 34 deletions.
2 changes: 1 addition & 1 deletion ui/app/dto/MirrorsDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type TableMapRow = {
source: string;
destination: string;
partitionKey: string;
exclude: string[];
exclude: Set<string>;
selected: boolean;
canMirror: boolean;
};
Expand Down
19 changes: 11 additions & 8 deletions ui/app/mirrors/create/cdc/columnbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,21 @@ export default function ColumnBox({
include: boolean
) => {
const currRows = [...rows];
const rowOfSource = currRows.find((row) => row.source === source);
if (rowOfSource) {
const rowIndex = currRows.findIndex((row) => row.source === source);
if (rowIndex !== -1) {
const sourceRow = currRows[rowIndex],
newExclude = new Set(sourceRow.exclude);
if (include) {
const updatedExclude = rowOfSource.exclude.filter(
(col) => col !== column
);
rowOfSource.exclude = updatedExclude;
newExclude.delete(column);
} else {
rowOfSource.exclude.push(column);
newExclude.add(column);
}
currRows[rowIndex] = {
...sourceRow,
exclude: newExclude,
};
setRows(currRows);
}
setRows(currRows);
};

const columnExclusion = new Set(tableRow.exclude);
Expand Down
12 changes: 4 additions & 8 deletions ui/app/mirrors/create/cdc/schemabox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ const SchemaBox = ({
const [expandedSchemas, setExpandedSchemas] = useState<string[]>([]);
const [tableQuery, setTableQuery] = useState<string>('');

const [handlingAll, setHandlingAll] = useState(false);

const searchedTables = useMemo(() => {
const tableQueryLower = tableQuery.toLowerCase();
return rows
Expand Down Expand Up @@ -125,17 +123,16 @@ const SchemaBox = ({
e: React.MouseEvent<HTMLInputElement, MouseEvent>,
schemaName: string
) => {
setHandlingAll(true);
const newRows = [...rows];
for (const row of newRows) {
for (let i = 0; i < newRows.length; i++) {
const row = newRows[i];
if (row.schema === schemaName) {
row.selected = e.currentTarget.checked;
newRows[i] = { ...row, selected: e.currentTarget.checked };
if (e.currentTarget.checked) addTableColumns(row.source);
else removeTableColumns(row.source);
}
}
setRows(newRows);
setHandlingAll(false);
};

const rowsDoNotHaveSchemaTables = (schema: string) => {
Expand Down Expand Up @@ -199,8 +196,7 @@ const SchemaBox = ({
</div>
</div>
{/* TABLE BOX */}
{handlingAll && <BarLoader />}
{!handlingAll && schemaIsExpanded(schema) && (
{schemaIsExpanded(schema) && (
<div className='ml-5 mt-3' style={{ width: '90%' }}>
{searchedTables.length ? (
searchedTables.map((row) => {
Expand Down
20 changes: 8 additions & 12 deletions ui/app/mirrors/create/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,13 @@ interface TableMapping {
}
const reformattedTableMapping = (tableMapping: TableMapRow[]) => {
const mapping = tableMapping
.map((row) => {
if (row?.selected === true) {
return {
sourceTableIdentifier: row.source,
destinationTableIdentifier: row.destination,
partitionKey: row.partitionKey,
exclude: row.exclude,
};
}
})
.filter(Boolean);
.filter((row) => row?.selected === true)
.map((row) => ({
sourceTableIdentifier: row.source,
destinationTableIdentifier: row.destination,
partitionKey: row.partitionKey,
exclude: Array.from(row.exclude),
}));
return mapping;
};

Expand Down Expand Up @@ -306,7 +302,7 @@ export const fetchTables = async (
source: `${schemaName}.${tableObject.tableName}`,
destination: dstName,
partitionKey: '',
exclude: [],
exclude: new Set(),
selected: false,
canMirror: tableObject.canMirror,
});
Expand Down
5 changes: 3 additions & 2 deletions ui/app/mirrors/create/mirrorcards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ const MirrorCards = ({
>
{cards.map((card, index) => {
return (
<div
<label
key={index}
style={{
display: 'block',
padding: '0.5rem',
width: '35%',
minHeight: '22vh',
Expand Down Expand Up @@ -79,7 +80,7 @@ const MirrorCards = ({
>
Learn more
</Label>
</div>
</label>
);
})}
</div>
Expand Down
6 changes: 5 additions & 1 deletion ui/app/mirrors/errors/[mirrorName]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ export default function MirrorError() {
<Label variant='body'>
<b>Mirror name</b>:
</Label>
<Label variant='body'>{params.mirrorName}</Label>
<Label variant='body'>
<a href={`/mirrors/edit/${params.mirrorName}`}>
{params.mirrorName}
</a>
</Label>

<div>
<Label as='label' style={{ fontSize: 14, marginTop: '1rem' }}>
Expand Down
3 changes: 1 addition & 2 deletions ui/app/mirrors/status/qrep/[mirrorId]/qrepStatusTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ export default function QRepStatusTable({
);
const [sortDir, setSortDir] = useState<'asc' | 'dsc'>('dsc');
const displayedPartitions = useMemo(() => {
let currentPartitions = [...visiblePartitions];
currentPartitions = currentPartitions.filter(
const currentPartitions = visiblePartitions.filter(
(partition: QRepPartitionStatus) => {
return partition.partitionId
.toLowerCase()
Expand Down

0 comments on commit 07f4551

Please sign in to comment.