Skip to content

Commit

Permalink
feat: download redemption codes as file (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
songquanpeng committed Apr 26, 2023
1 parent 9e2f238 commit f62a671
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 9 additions & 0 deletions web/src/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,13 @@ export function timestamp2string(timestamp) {
':' +
second
);
}

export function downloadTextAsFile(text, filename) {
let blob = new Blob([text], { type: 'text/plain;charset=utf-8' });
let url = URL.createObjectURL(blob);
let a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
}
15 changes: 11 additions & 4 deletions web/src/pages/Redemption/EditRedemption.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState } from 'react';
import { Button, Form, Header, Segment } from 'semantic-ui-react';
import { useParams } from 'react-router-dom';
import { API, isAdmin, showError, showSuccess, quotatamp2string } from '../../helpers';
import { API, downloadTextAsFile, showError, showSuccess } from '../../helpers';

const EditRedemption = () => {
const params = useParams();
Expand All @@ -11,7 +11,7 @@ const EditRedemption = () => {
const originInputs = {
name: '',
quota: 100,
count: 1,
count: 1
};
const [inputs, setInputs] = useState(originInputs);
const { name, quota, count } = inputs;
Expand Down Expand Up @@ -46,10 +46,10 @@ const EditRedemption = () => {
res = await API.put(`/api/redemption/`, { ...localInputs, id: parseInt(redemptionId) });
} else {
res = await API.post(`/api/redemption/`, {
...localInputs,
...localInputs
});
}
const { success, message } = res.data;
const { success, message, data } = res.data;
if (success) {
if (isEdit) {
showSuccess('兑换码更新成功!');
Expand All @@ -60,6 +60,13 @@ const EditRedemption = () => {
} else {
showError(message);
}
if (!isEdit && data) {
let text = "";
for (let i = 0; i < data.length; i++) {
text += data[i] + "\n";
}
downloadTextAsFile(text, `${inputs.name}.txt`);
}
};

return (
Expand Down

0 comments on commit f62a671

Please sign in to comment.