Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor/#79] 영수증, 갤러리 ocr 인식 수정 #80

Merged
merged 1 commit into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 15 additions & 35 deletions src/components/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,17 @@ export interface ScanResult {
}

const Home = () => {
const { send, receive } = useAppBridge();
const { send } = useAppBridge();

const { setScanData } = useScanDataStore();
const { scanData } = useScanDataStore();

const { navigateToReceiptEdit } = useRoute();

const handleScanResult = (jsonData: string) => {
try {
const data: ScanResult[] = JSON.parse(jsonData);

receive({
type: AppBridgeMessageType.RECEIVE_SCAN_RESULT,
payload: data,
});

setScanData(data);

navigateToReceiptEdit();
} catch (error) {
console.error("스캔 결과 JSON 파싱 오류:", error);
alert("스캔 데이터를 처리하는 중 오류가 발생했습니다. 다시 시도해 주세요.");
}
};

useEffect(() => {
const responseHandler = {
receiveScanResult: handleScanResult,
};

if (typeof window !== "undefined") {
window.response = Object.assign({}, window.response, responseHandler);
if (scanData.length > 0) {
navigateToReceiptEdit();
}

return () => {
if (typeof window !== "undefined") {
delete window.response;
}
};
}, [receive, navigateToReceiptEdit, setScanData]);
}, [scanData]);

return (
<div className={styles.Home}>
Expand All @@ -72,12 +44,20 @@ const Home = () => {
<IconButton
text="갤러리"
iconName="gallery"
onClick={() => send({ type: AppBridgeMessageType.OPEN_GALLERY, payload: "" })}
onClick={() => {
send({ type: AppBridgeMessageType.OPEN_GALLERY, payload: "" });

send({ type: AppBridgeMessageType.RECEIVE_SCAN_RESULT, payload: scanData });
}}
/>
<IconButton
text="카메라"
iconName="camera"
onClick={() => send({ type: AppBridgeMessageType.OPEN_CAMERA, payload: "" })}
onClick={() => {
send({ type: AppBridgeMessageType.OPEN_CAMERA, payload: "" });

send({ type: AppBridgeMessageType.RECEIVE_SCAN_RESULT, payload: scanData });
}}
/>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/components/ReceiptEdit/ReceiptEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useCreateReviewStore } from "@/store/useReviewStore";
import { useScanDataStore } from "@/store/useScanDataStore";

const ReceiptEdit = () => {
const { navigateToSelectTag } = useRoute();
const { navigateToHome, navigateToSelectTag } = useRoute();

const { scanData } = useScanDataStore();

Expand Down Expand Up @@ -114,7 +114,7 @@ const ReceiptEdit = () => {
/>
) : (
<>
<Button text="다시 스캔하기" variant="secondary" />
<Button text="다시 스캔하기" variant="secondary" onClick={navigateToHome} />
<Button
text="정보가 맞아요"
disabled={formData.some((item) => Object.values(item).some((value) => !value))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ interface AppBridgeProviderProps {

interface AppBridge {
send: (message: AppBridgeMessage) => void;
receive: (message: AppBridgeMessage) => void;
}

export const AppBridgeContext = createContext<null | AppBridge>(null);
Expand All @@ -37,15 +36,6 @@ export function AppBridgeProvider({ children }: AppBridgeProviderProps) {
}
};

const receive = (message: AppBridgeMessage) => {
try {
if (isIOS) return convertToIOSAppBridge(message);
return convertToAndroidAppBridge(message);
} catch {
alert("App Bridge API called: " + message.type);
}
};

useEffect(() => {
if (typeof window !== "undefined") {
window.response = {
Expand All @@ -69,9 +59,7 @@ export function AppBridgeProvider({ children }: AppBridgeProviderProps) {
}
}, []);

return (
<AppBridgeContext.Provider value={{ send, receive }}>{children}</AppBridgeContext.Provider>
);
return <AppBridgeContext.Provider value={{ send }}>{children}</AppBridgeContext.Provider>;
}

export function useAppBridge() {
Expand Down