-
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
da3413a
commit f9dc2f6
Showing
5 changed files
with
162 additions
and
2 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
154 changes: 154 additions & 0 deletions
154
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,154 @@ | ||
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'; | ||
import { PartialDref } from '#views/DrefApplicationForm/schema'; | ||
|
||
type FormFieldKeys = keyof PartialDref; | ||
interface FormField { | ||
name: FormFieldKeys, | ||
row: number, | ||
label: string, | ||
} | ||
|
||
const formFields: FormField[] = [ | ||
{ | ||
name: 'national_society', | ||
row: 2, | ||
label: 'National Society', | ||
}, | ||
{ | ||
name: 'country', | ||
row: 3, | ||
label: 'Country', | ||
}, | ||
]; | ||
|
||
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('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: 'Form Name', key: 'name' }, | ||
{ header: 'Label', key: 'label' }, | ||
{ header: 'Value', key: 'value' }, | ||
]; | ||
|
||
const nameColumn = overviewWorksheet.getColumn('name'); | ||
const labelColumn = overviewWorksheet.getColumn('label'); | ||
nameColumn.protection = { | ||
locked: true, | ||
hidden: true, | ||
}; | ||
|
||
labelColumn.protection = { locked: true }; | ||
formFields.forEach((formField) => { | ||
const row = overviewWorksheet.getRow(formField.row); | ||
row.values = { | ||
name: formField.name, | ||
label: formField.label, | ||
}; | ||
const nameCell = row.getCell('name'); | ||
nameCell.protection = { | ||
locked: true, | ||
hidden: true, | ||
}; | ||
|
||
const valueCell = row.getCell('value'); | ||
valueCell.dataValidation = { | ||
type: 'list', | ||
allowBlank: false, | ||
formulae: ['"Nepal, India, China"'], | ||
}; | ||
}); | ||
|
||
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