-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a2c3bb
commit 7125663
Showing
7 changed files
with
140 additions
and
7 deletions.
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
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
121 changes: 121 additions & 0 deletions
121
app/src/views/AccountMyFormsDref/DownloadImportTemplateButton/index.tsx
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,121 @@ | ||
import { | ||
useCallback, | ||
useState, | ||
} from 'react'; | ||
import { Button } from '@ifrc-go/ui'; | ||
import xlsx from 'exceljs'; | ||
import FileSaver from 'file-saver'; | ||
|
||
import ifrcLogoFile from '#assets/icons/ifrc-square.png'; | ||
import useNationalSociety from '#hooks/domain/useNationalSociety'; | ||
import { COLOR_RED } from '#utils/constants'; | ||
|
||
const fieldNameToCellAddressMap = { | ||
national_society: { | ||
row: 6, | ||
}, | ||
}; | ||
|
||
function DownloadImportTemplateButton() { | ||
const [generationPending, setGenerationPending] = useState(false); | ||
const nationalSocieties = useNationalSociety(); | ||
|
||
const handleClick = useCallback( | ||
() => { | ||
async function generateTemplate() { | ||
const workbook = new xlsx.Workbook(); | ||
const now = new Date(); | ||
workbook.created = now; | ||
const response = await fetch(ifrcLogoFile); | ||
const buffer = await response.arrayBuffer(); | ||
|
||
const ifrcLogo = workbook.addImage({ | ||
buffer, | ||
extension: 'png', | ||
}); | ||
|
||
const coverWorksheet = workbook.addWorksheet('DREF Import'); | ||
|
||
const overviewWorksheet = workbook.addWorksheet('Operation Overview'); | ||
const eventDetailsWorksheet = workbook.addWorksheet('Event Detail'); | ||
const actionsNeedsWorksheet = workbook.addWorksheet('Actions-Needs'); | ||
const operationWorksheet = workbook.addWorksheet('Operation'); | ||
const timeframeAndContactsWorksheet = workbook.addWorksheet('Operational timeframes and contacts'); | ||
|
||
coverWorksheet.addImage(ifrcLogo, 'A1:B6'); | ||
coverWorksheet.getCell('C1').value = 'DISASTER RESPONSE EMERGENCY FUND'; | ||
coverWorksheet.mergeCells('C1:L3'); | ||
coverWorksheet.getCell('C1:L3').style = { | ||
font: { | ||
name: 'Montserrat', | ||
family: 2, | ||
bold: true, | ||
size: 20, | ||
color: { argb: COLOR_RED }, | ||
}, | ||
alignment: { horizontal: 'center', vertical: 'middle' }, | ||
}; | ||
coverWorksheet.addRow(''); | ||
coverWorksheet.addRow(''); | ||
coverWorksheet.addRow(''); | ||
coverWorksheet.addRow(''); | ||
coverWorksheet.mergeCells('C4:L6'); | ||
coverWorksheet.getCell('C4').value = 'Import template'; | ||
coverWorksheet.getCell('C4').style = { | ||
font: { | ||
bold: true, size: 18, name: 'Montserrat', family: 2, | ||
}, | ||
alignment: { horizontal: 'center', vertical: 'middle' }, | ||
}; | ||
|
||
overviewWorksheet.columns = [ | ||
{ header: 'ID', key: 'id' }, | ||
{ header: 'Field', key: 'field' }, | ||
{ header: 'Value', key: 'value' }, | ||
]; | ||
|
||
const idColumn = overviewWorksheet.getColumn('id'); | ||
const fieldColumn = overviewWorksheet.getColumn('field'); | ||
idColumn.protection = { | ||
locked: true, | ||
hidden: true, | ||
}; | ||
fieldColumn.protection = { locked: true }; | ||
overviewWorksheet.addRow({ | ||
values: { | ||
id: 'country', | ||
field: 'Country', | ||
}, | ||
}); | ||
|
||
await workbook.xlsx.writeBuffer().then((sheet) => FileSaver.saveAs( | ||
new Blob([sheet], { type: 'application/vnd.ms-excel;charset=utf-8' }), | ||
`DREF import template ${now.toLocaleString()}.xlsx`, | ||
)); | ||
|
||
setGenerationPending(false); | ||
} | ||
|
||
setGenerationPending((alreadyGenerating) => { | ||
if (!alreadyGenerating) { | ||
generateTemplate(); | ||
} | ||
|
||
return true; | ||
}); | ||
}, | ||
[nationalSocieties], | ||
); | ||
|
||
return ( | ||
<Button | ||
onClick={handleClick} | ||
name={undefined} | ||
disabled={generationPending} | ||
> | ||
Download Import Template | ||
</Button> | ||
); | ||
} | ||
|
||
export default DownloadImportTemplateButton; |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
|
||
.name { | ||
display: flex; | ||
word-break: break-all; | ||
word-break: break-word; | ||
} | ||
} | ||
|
||
|
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