Skip to content

Commit

Permalink
fix(IdP-config): fix users upload functionality for json format (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhigarg-bmw authored Feb 20, 2024
1 parent aa2a2d9 commit 30c91be
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/components/overlays/AddusersIDP/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ export const AddusersIDP = ({ id }: { id: string }) => {
'ProviderUserName',
]

const jsonHeaderList = [
'companyUserId',
'firstName',
'lastName',
'email',
'identityProviders',
]

const CSV_COLUMNS = useMemo(
() => [
{ name: 'UserId', width: 37 },
Expand All @@ -245,15 +253,10 @@ export const AddusersIDP = ({ id }: { id: string }) => {
if (!idpData || !userContent?.data) return
setLoading(true)
const postdata = new FormData()
const prettyVal = pretty ? 2 : undefined
const fileBlob =
format === FileFormat.CSV
? json2csv(store2data(userContent.data))
: JSON.stringify(userContent.data, null, prettyVal)
postdata.append(
'document',
new Blob([fileBlob], {
type: format === FileFormat.CSV ? 'text/csv' : 'application/json',
new Blob([json2csv(store2data(userContent.data))], {
type: 'text/csv',
})
)
fetch(
Expand Down Expand Up @@ -444,13 +447,13 @@ export const AddusersIDP = ({ id }: { id: string }) => {
setUploadedFile(undefined)
return
}
const content = JSON.stringify(reader.result)
if (format === FileFormat.JSON && typeof reader.result === 'string') {
//if file is JSON
const content = JSON.stringify(json2csv(JSON.parse(reader.result)))
const JSONData = JSON.parse(reader.result)
const jsonKeys = JSONData.map((obj: FileData) => Object.keys(obj))[0]
if (
!csvHeaderList.reduce(
!jsonHeaderList.reduce(
(a, c, i) => a && jsonKeys[i].toLowerCase() === c.toLowerCase(),
true
)
Expand All @@ -463,8 +466,11 @@ export const AddusersIDP = ({ id }: { id: string }) => {
setUploadedFile(undefined)
return
}
setCsvData(csv2json(content))
storeData(JSON.stringify(csv2json(content)))
} else {
//if file is CSV
const content = JSON.stringify(reader.result)
Papa.parse(acceptedFile, {
skipEmptyLines: true,
complete: function (results) {
Expand All @@ -487,13 +493,9 @@ export const AddusersIDP = ({ id }: { id: string }) => {
}
},
})
setCsvData(csv2json(content))
storeData(JSON.stringify(csv2json(content)))
}
setCsvData(csv2json(content))
storeData(
acceptedFile.type === 'text/csv'
? JSON.stringify(csv2json(content))
: content
)
}
setUploadedFile(acceptedFile)
reader.readAsText(acceptedFile)
Expand Down

0 comments on commit 30c91be

Please sign in to comment.