import { render } from 'react-thermal-printer';
import { DrinkTicket } from '../../components';
const drinkPrint = ({ children }: { children: React.ReactNode }) => {
const onClickPrint = async () => {
let port: SerialPort | null = null;
let writer: WritableStreamDefaultWriter<Uint8Array> | null = null;
try {
const ports = await navigator.serial.getPorts();
port = ports[0];
if (!port) {
port = await navigator.serial.requestPort({
filters: [],
});
}
if (!port.writable) {
await port.open({ baudRate: 9600 });
}
writer = port.writable!.getWriter();
try {
try {
const printTicket = await render(Ticket(countArray));
const ticketArray = new Uint8Array(printTicket);
await writer.write(ticketArray);
if (drinkCheck) {
const printDrinkTicket = await render(DrinkTicket());
const drinkArray = new Uint8Array(printDrinkTicket);
await writer.write(drinkArray);
}
await new Promise((resolve) => setTimeout(resolve, 1500));
} catch (error) {
setIsModal('printFail');
navigate('/');
}
try {
const printData = await render(DrinkTicket());
const uint8Array = new Uint8Array(printData);
await writer.write(uint8Array);
await new Promise((resolve) => setTimeout(resolve, 1500));
for (let i = 0; i < ticketId().count; i++) {
try {
const printData = await render(
Ticket({
data: data[i],
count: i + 1,
})
);
const uint8Array = new Uint8Array(printData);
await writer.write(uint8Array);
await new Promise((resolve) => setTimeout(resolve, 1500));
} catch (e) {
setIsModal('printFail');
navigate('/');
}
}
} catch (e) {
console.log('error');
}
} catch (e) {
console.log('error');
}
} finally {
if (writer) {
writer.releaseLock();
}
if (port?.writable) {
await port.close();
}
}
};
return <button onClick={onClickPrint}>{children}</button>;
};
export default drinkPrint;
import { Br, Cut, Line, Printer, Row, Text } from 'react-thermal-printer';
import useGetDate from '../../../utils/hooks/useGetDate';
const DrinkTicket = () => {
const companyInfo = [
{ label: '사업자번호', content: '111-11-11111' },
{ label: '상호', content: '(주)가나다라마바사' },
{ label: '주소', content: '서울시 성동구 왕십리' },
{ label: '대표자', content: '뚜뚜' },
];
return (
<Printer type="epson" width={42} characterSet="korea">
<li>
{}
<Text align="center" size={{ width: 2, height: 2 }}>
음료 교환권
</Text>
<Br />
<Br />
<Row
left={
<Text bold size={{ width: 1, height: 1 }}>
발권 일시
</Text>
}
right={useGetDate()}
/>
<Line />
{}
{companyInfo.map((info, index) => (
<Row
key={index}
left={
<Text bold size={{ width: 1, height: 1 }}>
{info.label}
</Text>
}
right={info.content}
/>
))}
<Br />
{}
<Line />
<Row
left={
<Text bold size={{ width: 1, height: 1 }}>
옵션명
</Text>
}
right={
<Text bold size={{ width: 1, height: 1 }}>
수량
</Text>
}
/>
<Line />
<Row left="뚜뚜-건강검진" right={`${1}`} />
<Row left="뚜뚜-심장사상충약-넥스가드" right={`${2}`} />
<Br />
<Line />
<Br />
{}
<Row
left={
<Text bold size={{ width: 1, height: 1 }}>
문의
</Text>
}
right="1661-3624"
/>
<Br />
<Text align="left" size={{ width: 1, height: 1 }}>
뚜뚜 건강하세요
</Text>
{}
<Cut />
</li>
</Printer>
);
};
export default DrinkTicket;