-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from akvo/develop
Develop
- Loading branch information
Showing
21 changed files
with
1,525 additions
and
569 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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,163 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { Col, Form, Input, Upload, message } from 'antd'; | ||
import { FieldLabel } from '../support'; | ||
import DraggerText from '../support/DraggerText'; | ||
import ImagePreview from '../support/ImagePreview'; | ||
|
||
const { Dragger } = Upload; | ||
|
||
const FILE_TYPES = ['image/png', 'image/jpeg', 'image/jpg']; | ||
|
||
const getImageBase64 = (file) => { | ||
return new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.readAsDataURL(file); | ||
reader.onload = () => { | ||
const base64String = reader.result; | ||
resolve(base64String); | ||
}; | ||
reader.onerror = (error) => { | ||
reject(error); | ||
}; | ||
}); | ||
}; | ||
|
||
const convertImageToBase64 = (imgUrl) => { | ||
return new Promise((resolve, reject) => { | ||
const image = new Image(); | ||
image.crossOrigin = 'anonymous'; | ||
image.onload = () => { | ||
const canvas = document.createElement('canvas'); | ||
const ctx = canvas.getContext('2d'); | ||
canvas.height = image.naturalHeight; | ||
canvas.width = image.naturalWidth; | ||
ctx.drawImage(image, 0, 0); | ||
const dataUrl = canvas.toDataURL(); | ||
resolve(dataUrl); | ||
}; | ||
image.src = imgUrl; | ||
image.onerror = (error) => { | ||
reject(error); | ||
}; | ||
}); | ||
}; | ||
|
||
const TypeImage = ({ | ||
id, | ||
name, | ||
keyform, | ||
required, | ||
rules, | ||
tooltip, | ||
requiredSign, | ||
initialValue = null, | ||
limit = 2, | ||
}) => { | ||
const [fileList, setFileList] = useState([]); | ||
const [preview, setPreview] = useState(null); | ||
const [visible, setVisible] = useState(false); | ||
const form = Form.useFormInstance(); | ||
|
||
useEffect(() => { | ||
if (initialValue && fileList.length === 0) { | ||
convertImageToBase64(initialValue).then((initialBase64) => { | ||
form.setFieldsValue({ [id]: initialBase64 }); | ||
}); | ||
|
||
setFileList([ | ||
{ | ||
uid: '1', | ||
status: 'done', | ||
name: initialValue, | ||
url: initialValue, | ||
}, | ||
]); | ||
} | ||
}, [initialValue, fileList]); | ||
|
||
const fileListExists = fileList.filter((f) => f?.status !== 'removed'); | ||
return ( | ||
<Col> | ||
<Form.Item | ||
className="arf-field" | ||
label={ | ||
<FieldLabel | ||
keyform={keyform} | ||
content={name} | ||
requiredSign={required ? requiredSign : null} | ||
/> | ||
} | ||
tooltip={tooltip?.text} | ||
required={required} | ||
> | ||
<Form.Item | ||
className="arf-field-image" | ||
name={id} | ||
rules={rules} | ||
required={required} | ||
noStyle | ||
> | ||
<Input | ||
disabled | ||
hidden | ||
/> | ||
</Form.Item> | ||
<Dragger | ||
multiple={false} | ||
listType="picture" | ||
fileList={fileListExists} | ||
customRequest={({ onSuccess }) => { | ||
onSuccess('ok'); | ||
}} | ||
beforeUpload={(file) => { | ||
const fileMB = file.size / (1024 * 1024); | ||
const validate = fileMB <= limit && FILE_TYPES.includes(file.type); | ||
if (validate) { | ||
setFileList([ | ||
{ | ||
...file, | ||
name: file.name, | ||
url: URL.createObjectURL(file), | ||
}, | ||
]); | ||
} | ||
if (!validate) { | ||
setFileList([]); | ||
message.error( | ||
`File size exceeds the limit. Please upload a file smaller than ${limit} MB.` | ||
); | ||
} | ||
return validate; | ||
}} | ||
onChange={({ file: { status, originFileObj } }) => { | ||
if (fileList.length) { | ||
setFileList([ | ||
{ | ||
...fileList[0], | ||
status, | ||
}, | ||
]); | ||
} | ||
if (originFileObj && (status === 'success' || status === 'done')) { | ||
getImageBase64(originFileObj).then((imageBase64String) => { | ||
form.setFieldsValue({ [id]: imageBase64String }); | ||
}); | ||
} | ||
}} | ||
onPreview={({ url }) => { | ||
setPreview(url); | ||
setVisible(true); | ||
}} | ||
> | ||
<DraggerText limit={limit} /> | ||
</Dragger> | ||
<ImagePreview | ||
visible={visible} | ||
src={preview} | ||
onChange={setVisible} | ||
/> | ||
</Form.Item> | ||
</Col> | ||
); | ||
}; | ||
export default TypeImage; |
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
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
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 |
---|---|---|
@@ -1,22 +1,30 @@ | ||
{ | ||
"submit": "Absenden", | ||
"add": "Weitere", | ||
"addAnother": "Weitere hinzufügen", | ||
"cancel": "Stornieren", | ||
"delete": "Löschen", | ||
"download": "Herunterladen", | ||
"save": "Speichern", | ||
"print": "Drucken", | ||
"edit": "Bearbeiten", | ||
"errorDecimal": "Dezimalwerte sind für diese Frage nicht erlaubt", | ||
"errorIsRequired": "ist erforderlich", | ||
"errorMax": "Der Wert muss kleiner oder gleich sein als", | ||
"errorMin": "Der Wert muss größer oder gleich sein als", | ||
"errorMinMax": "Der Wert muss zwischen liegen", | ||
"formOverview": "Formularübersicht", | ||
"loadingInitialData": "Lade Initialdaten", | ||
"submissionSaved": "Einreichung gespeichert", | ||
"addAnother": "Weitere hinzufügen", | ||
"selectLevel": "Level auswählen", | ||
"selectDate": "Datum auswählen", | ||
"pleaseSelect": "Bitte auswählen", | ||
"ok": "ok", | ||
"pleaseEnterItem": "Bitte geben Sie den Artikel ein", | ||
"pleaseInput": "Bitte eingeben", | ||
"pleaseSelect": "Bitte auswählen", | ||
"pleaseTypeOtherOption": "Bitte geben Sie eine andere Option ein", | ||
"pleaseEnterItem": "Bitte geben Sie den Artikel ein", | ||
"useMyLocation": "Meinen Standort verwenden", | ||
"errorIsRequired": "ist erforderlich", | ||
"errorDecimal": "Dezimalwerte sind für diese Frage nicht erlaubt", | ||
"errorMin": "Der Wert muss größer oder gleich sein als", | ||
"errorMax": "Der Wert muss kleiner oder gleich sein als", | ||
"errorMinMax": "Der Wert muss zwischen liegen" | ||
"print": "Drucken", | ||
"remove": "Entfernen", | ||
"save": "Speichern", | ||
"selectDate": "Datum auswählen", | ||
"selectLevel": "Level auswählen", | ||
"submissionSaved": "Einreichung gespeichert", | ||
"submit": "Absenden", | ||
"sureToCancel": "Sicher absagen?", | ||
"sureToDelete": "Sicher löschen?", | ||
"useMyLocation": "Meinen Standort verwenden" | ||
} |
Oops, something went wrong.