-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Basic import for select in CSV #6047
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { | |
Column, | ||
ColumnType, | ||
MatchColumnsStepProps, | ||
MatchedOptions, | ||
} from '@/spreadsheet-import/steps/components/MatchColumnsStep/MatchColumnsStep'; | ||
import { Field } from '@/spreadsheet-import/types'; | ||
|
||
|
@@ -12,33 +13,56 @@ export const setColumn = <T extends string>( | |
field?: Field<T>, | ||
data?: MatchColumnsStepProps<T>['data'], | ||
): Column<T> => { | ||
switch (field?.fieldType.type) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why removing the switch case pattern here? If it's too big maybe we can split the code inside functions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tbh I'm not sure why but the switch/case caused a wired typescript issue which I didn't figure out |
||
case 'select': | ||
return { | ||
...oldColumn, | ||
type: ColumnType.matchedSelect, | ||
value: field.key, | ||
matchedOptions: uniqueEntries(data || [], oldColumn.index), | ||
}; | ||
case 'checkbox': | ||
return { | ||
index: oldColumn.index, | ||
type: ColumnType.matchedCheckbox, | ||
value: field.key, | ||
header: oldColumn.header, | ||
}; | ||
case 'input': | ||
return { | ||
index: oldColumn.index, | ||
type: ColumnType.matched, | ||
value: field.key, | ||
header: oldColumn.header, | ||
}; | ||
default: | ||
return { | ||
index: oldColumn.index, | ||
header: oldColumn.header, | ||
type: ColumnType.empty, | ||
}; | ||
if (field?.fieldType.type === 'select') { | ||
const fieldOptions = field.fieldType.options; | ||
const uniqueData = uniqueEntries( | ||
data || [], | ||
oldColumn.index, | ||
) as MatchedOptions<T>[]; | ||
const matchedOptions = uniqueData.map((record) => { | ||
const value = fieldOptions.find( | ||
(fieldOption) => | ||
fieldOption.value === record.entry || | ||
fieldOption.label === record.entry, | ||
)?.value; | ||
Comment on lines
+23
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧠 logic: Potential issue if |
||
return value | ||
? ({ ...record, value } as MatchedOptions<T>) | ||
: (record as MatchedOptions<T>); | ||
}); | ||
const allMatched = | ||
matchedOptions.filter((o) => o.value).length === uniqueData?.length; | ||
Comment on lines
+32
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🪶 style: Consider optimizing the filter and length comparison for better performance. |
||
|
||
return { | ||
...oldColumn, | ||
type: allMatched | ||
? ColumnType.matchedSelectOptions | ||
: ColumnType.matchedSelect, | ||
value: field.key, | ||
matchedOptions, | ||
}; | ||
} | ||
|
||
if (field?.fieldType.type === 'checkbox') { | ||
return { | ||
index: oldColumn.index, | ||
type: ColumnType.matchedCheckbox, | ||
value: field.key, | ||
header: oldColumn.header, | ||
}; | ||
} | ||
|
||
if (field?.fieldType.type === 'input') { | ||
return { | ||
index: oldColumn.index, | ||
type: ColumnType.matched, | ||
value: field.key, | ||
header: oldColumn.header, | ||
}; | ||
} | ||
|
||
return { | ||
index: oldColumn.index, | ||
header: oldColumn.header, | ||
type: ColumnType.empty, | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For more clarity? Wdyt? Not sure haha
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much nicer 😍 thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if I'm missing something but I feel like often when I try to do these kind of optimizations then Typescript gets lost
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.