-
Notifications
You must be signed in to change notification settings - Fork 0
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 #8 from gunet/wwwallet-sync
Wwwallet sync
- Loading branch information
Showing
32 changed files
with
360 additions
and
258 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,62 @@ | ||
import axios from 'axios'; | ||
import jsonpointer from 'jsonpointer'; | ||
import { formatDate } from '../../functions/DateFormat'; | ||
import customTemplate from '../../assets/images/custom_template.svg'; | ||
|
||
async function getBase64Image(url) { | ||
if (!url) return null; | ||
try { | ||
const response = await axios.get(url, { responseType: 'blob' }); | ||
const blob = response.data; | ||
|
||
return new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.onloadend = () => resolve(reader.result); | ||
reader.onerror = reject; | ||
reader.readAsDataURL(blob); | ||
}); | ||
} catch (error) { | ||
console.error("Failed to load image", url); | ||
return null; | ||
} | ||
} | ||
|
||
const renderCustomSvgTemplate = async ({ beautifiedForm, name, description, logoURL, logoAltText, backgroundColor, textColor, backgroundImageURL }) => { | ||
try { | ||
const response = await fetch(customTemplate); | ||
if (!response.ok) throw new Error("Failed to fetch SVG template"); | ||
|
||
let svgContent = await response.text(); | ||
|
||
const backgroundImageBase64 = await getBase64Image(backgroundImageURL); | ||
const logoBase64 = await getBase64Image(logoURL); | ||
|
||
svgContent = svgContent | ||
.replace(/{{backgroundColor}}/g, backgroundColor) | ||
.replace( | ||
/{{backgroundImageBase64}}/g, | ||
backgroundImageBase64 | ||
? `<image xlink:href="${backgroundImageBase64}" x="0" y="0" width="100%" height="100%" preserveAspectRatio="xMidYMid slice" />` | ||
: '' | ||
) | ||
.replace( | ||
/{{logoBase64}}/g, | ||
logoBase64 | ||
? `<image xlink:href="${logoBase64}" x="50" y="380" height="20%"><title>${logoAltText}</title></image>` | ||
: '' | ||
) | ||
.replace(/{{name}}/g, name) | ||
.replace(/{{textColor}}/g, textColor) | ||
.replace(/{{description}}/g, description); | ||
|
||
const expiryDate = jsonpointer.get(beautifiedForm, "/expiry_date"); | ||
svgContent = svgContent.replace(/{{\/expiry_date}}/g, expiryDate ? `Expiry Date: ${formatDate(expiryDate, 'date')}` : ''); | ||
|
||
return `data:image/svg+xml;utf8,${encodeURIComponent(svgContent)}`; | ||
} catch (error) { | ||
console.error("Error rendering SVG template", error); | ||
return null; | ||
} | ||
}; | ||
|
||
export default renderCustomSvgTemplate; |
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,48 +1,34 @@ | ||
import { useState, useEffect } from 'react'; | ||
import axios from 'axios'; | ||
import jsonpointer from 'jsonpointer'; | ||
import { formatDate } from '../../functions/DateFormat'; | ||
|
||
const RenderSvgTemplate = ({ credential, onSvgGenerated }) => { | ||
const [svgContent, setSvgContent] = useState(null); | ||
|
||
useEffect(() => { | ||
const fetchSvgContent = async () => { | ||
try { | ||
const response = await fetch(credential.credentialImage.credentialImageSvgTemplateURL); | ||
if (!response.ok) { | ||
throw new Error(`Failed to fetch SVG from ${credential.credentialImage.credentialImageSvgTemplateURL}`); | ||
} | ||
|
||
const svgText = await response.text(); | ||
setSvgContent(svgText); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
if (credential.credentialImage.credentialImageSvgTemplateURL) { | ||
fetchSvgContent(); | ||
const renderSvgTemplate = async ({ beautifiedForm, credentialImageSvgTemplateURL }) => { | ||
let svgContent = null; | ||
try { | ||
const response = await axios.get(credentialImageSvgTemplateURL); | ||
if (response.status !== 200) { | ||
throw new Error(`Failed to fetch SVG`); | ||
} | ||
}, [credential.credentialImage.credentialImageSvgTemplateURL]); | ||
|
||
useEffect(() => { | ||
if (svgContent) { | ||
const regex = /{{([^}]+)}}/g; | ||
|
||
const replacedSvgText = svgContent.replace(regex, (_match, content) => { | ||
let res = jsonpointer.get(credential.beautifiedForm, content.trim()); | ||
if (res !== undefined) { | ||
res = formatDate(res, 'date'); | ||
return res; | ||
} | ||
return '-'; | ||
}); | ||
const dataUri = `data:image/svg+xml;utf8,${encodeURIComponent(replacedSvgText)}`; | ||
onSvgGenerated(dataUri); | ||
} | ||
}, [svgContent, credential.beautifiedForm, onSvgGenerated]); | ||
svgContent = response.data; | ||
} catch (error) { | ||
return null; // Return null if fetching fails | ||
} | ||
|
||
if (svgContent) { | ||
const regex = /{{([^}]+)}}/g; | ||
const replacedSvgText = svgContent.replace(regex, (_match, content) => { | ||
let res = jsonpointer.get(beautifiedForm, content.trim()); | ||
if (res !== undefined) { | ||
res = formatDate(res, 'date'); | ||
return res; | ||
} | ||
return '-'; | ||
}); | ||
const dataUri = `data:image/svg+xml;utf8,${encodeURIComponent(replacedSvgText)}`; | ||
return dataUri; // Return the data URI for the SVG | ||
} | ||
|
||
return null; | ||
return null; // Return null if no SVG content is available | ||
}; | ||
|
||
export default RenderSvgTemplate; | ||
export default renderSvgTemplate; |
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
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
Oops, something went wrong.