-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Spreadsheet import front module #2862
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
23051ca
Spreadsheet import front module
gitstart-twenty 2571775
Merge commit '6c8395363339b7261e86d9cfed5c83f46d0eb6be' of https://gi…
gitstart-twenty 7972b2a
Automatically update table
gitstart-twenty 0fe2703
Add company import
gitstart-twenty 0c3a04c
Merge branch 'main' into TWNTY-2473
charlesBochet 7cacb08
Fixes
charlesBochet c0cebe1
Hide import options on custom objects
charlesBochet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,4 +179,4 @@ | |
"msw": { | ||
"workerDirectory": "public" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
front/src/modules/companies/hooks/useSpreadsheetCompanyImport.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Company } from '@/companies/types/Company'; | ||
import { useCreateManyRecords } from '@/object-record/hooks/useCreateManyRecords'; | ||
import { useSpreadsheetImport } from '@/spreadsheet-import/hooks/useSpreadsheetImport'; | ||
import { SpreadsheetOptions } from '@/spreadsheet-import/types'; | ||
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar'; | ||
|
||
import { fieldsForCompany } from '../utils/fieldsForCompany'; | ||
|
||
export type FieldCompanyMapping = (typeof fieldsForCompany)[number]['key']; | ||
|
||
export const useSpreadsheetCompanyImport = () => { | ||
const { openSpreadsheetImport } = useSpreadsheetImport<FieldCompanyMapping>(); | ||
const { enqueueSnackBar } = useSnackBar(); | ||
|
||
const { createManyRecords: createManyCompanies } = | ||
useCreateManyRecords<Company>({ | ||
objectNameSingular: 'company', | ||
}); | ||
|
||
const openCompanySpreadsheetImport = ( | ||
options?: Omit< | ||
SpreadsheetOptions<FieldCompanyMapping>, | ||
'fields' | 'isOpen' | 'onClose' | ||
>, | ||
) => { | ||
openSpreadsheetImport({ | ||
...options, | ||
onSubmit: async (data) => { | ||
// TODO: Add better type checking in spreadsheet import later | ||
const createInputs = data.validData.map((company) => ({ | ||
name: company.name as string | undefined, | ||
domainName: company.domainName as string | undefined, | ||
...(company.linkedinUrl | ||
? { | ||
linkedinLink: { | ||
label: 'linkedinUrl', | ||
url: company.linkedinUrl as string | undefined, | ||
}, | ||
} | ||
: {}), | ||
...(company.annualRecurringRevenue | ||
? { | ||
annualRecurringRevenue: { | ||
amountMicros: Number(company.annualRecurringRevenue), | ||
currencyCode: 'USD', | ||
}, | ||
} | ||
: {}), | ||
idealCustomerProfile: | ||
company.idealCustomerProfile && | ||
['true', true].includes(company.idealCustomerProfile), | ||
...(company.xUrl | ||
? { | ||
xLink: { | ||
label: 'xUrl', | ||
url: company.xUrl as string | undefined, | ||
}, | ||
} | ||
: {}), | ||
address: company.address as string | undefined, | ||
employees: company.employees ? Number(company.employees) : undefined, | ||
})); | ||
// TODO: abstract this part for any object | ||
try { | ||
await createManyCompanies(createInputs); | ||
} catch (error: any) { | ||
enqueueSnackBar(error?.message || 'Something went wrong', { | ||
variant: 'error', | ||
}); | ||
} | ||
}, | ||
fields: fieldsForCompany, | ||
}); | ||
}; | ||
|
||
return { openCompanySpreadsheetImport }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
front/src/modules/object-record/hooks/useCreateManyRecords.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { useApolloClient } from '@apollo/client'; | ||
import { v4 } from 'uuid'; | ||
|
||
import { useOptimisticEffect } from '@/apollo/optimistic-effect/hooks/useOptimisticEffect'; | ||
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; | ||
import { ObjectMetadataItemIdentifier } from '@/object-metadata/types/ObjectMetadataItemIdentifier'; | ||
import { useGenerateEmptyRecord } from '@/object-record/hooks/useGenerateEmptyRecord'; | ||
import { capitalize } from '~/utils/string/capitalize'; | ||
|
||
export const useCreateManyRecords = <T>({ | ||
objectNameSingular, | ||
}: ObjectMetadataItemIdentifier) => { | ||
const { triggerOptimisticEffects } = useOptimisticEffect({ | ||
objectNameSingular, | ||
}); | ||
|
||
const { objectMetadataItem, createManyRecordsMutation } = | ||
useObjectMetadataItem({ | ||
objectNameSingular, | ||
}); | ||
|
||
const { generateEmptyRecord } = useGenerateEmptyRecord({ | ||
objectMetadataItem, | ||
}); | ||
|
||
const apolloClient = useApolloClient(); | ||
|
||
const createManyRecords = async (data: Record<string, any>[]) => { | ||
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. very nice! |
||
const withIds = data.map((record) => ({ | ||
...record, | ||
id: (record.id as string) ?? v4(), | ||
})); | ||
|
||
withIds.forEach((record) => { | ||
triggerOptimisticEffects( | ||
`${capitalize(objectMetadataItem.nameSingular)}Edge`, | ||
generateEmptyRecord({ id: record.id }), | ||
); | ||
}); | ||
|
||
const createdObjects = await apolloClient.mutate({ | ||
mutation: createManyRecordsMutation, | ||
variables: { | ||
data: withIds, | ||
}, | ||
optimisticResponse: { | ||
[`create${capitalize(objectMetadataItem.namePlural)}`]: withIds.map( | ||
(record) => generateEmptyRecord({ id: record.id }), | ||
), | ||
}, | ||
}); | ||
|
||
if (!createdObjects.data) { | ||
return null; | ||
} | ||
|
||
const createdRecords = | ||
(createdObjects.data[ | ||
`create${capitalize(objectMetadataItem.namePlural)}` | ||
] as T[]) ?? []; | ||
|
||
createdRecords.forEach((record) => { | ||
triggerOptimisticEffects( | ||
`${capitalize(objectMetadataItem.nameSingular)}Edge`, | ||
record, | ||
); | ||
}); | ||
|
||
return createdRecords; | ||
}; | ||
|
||
return { createManyRecords }; | ||
}; |
30 changes: 30 additions & 0 deletions
30
front/src/modules/object-record/hooks/useGenerateCreateManyRecordMutation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { gql } from '@apollo/client'; | ||
|
||
import { useMapFieldMetadataToGraphQLQuery } from '@/object-metadata/hooks/useMapFieldMetadataToGraphQLQuery'; | ||
import { EMPTY_MUTATION } from '@/object-metadata/hooks/useObjectMetadataItem'; | ||
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem'; | ||
import { capitalize } from '~/utils/string/capitalize'; | ||
|
||
export const useGenerateCreateManyRecordMutation = ({ | ||
objectMetadataItem, | ||
}: { | ||
objectMetadataItem: ObjectMetadataItem; | ||
}) => { | ||
const mapFieldMetadataToGraphQLQuery = useMapFieldMetadataToGraphQLQuery(); | ||
|
||
if (!objectMetadataItem) { | ||
return EMPTY_MUTATION; | ||
} | ||
|
||
return gql` | ||
mutation Create${capitalize( | ||
objectMetadataItem.namePlural, | ||
)}($data: [${capitalize(objectMetadataItem.nameSingular)}CreateInput!]!) { | ||
create${capitalize(objectMetadataItem.namePlural)}(data: $data) { | ||
id | ||
${objectMetadataItem.fields | ||
.map((field) => mapFieldMetadataToGraphQLQuery(field)) | ||
.join('\n')} | ||
} | ||
}`; | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think it is acceptable at this stage to implement a company and a person version but we will want to re-implement it for any standard or custom object in a more abstract way
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.
Makes sense