feat(saju): 사주풀이 5 컴포넌트 + useSajuReading hook
This commit is contained in:
33
src/pages/saju/hooks/useSajuReading.js
Normal file
33
src/pages/saju/hooks/useSajuReading.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { sajuGetReading } from '../../../api';
|
||||
|
||||
export default function useSajuReading(readingId) {
|
||||
const [data, setData] = useState(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!readingId) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
let cancelled = false;
|
||||
setLoading(true);
|
||||
sajuGetReading(readingId)
|
||||
.then((d) => {
|
||||
if (!cancelled) {
|
||||
setData(d);
|
||||
setLoading(false);
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
if (!cancelled) {
|
||||
setError(e.message || '사주 결과를 불러올 수 없습니다.');
|
||||
setLoading(false);
|
||||
}
|
||||
});
|
||||
return () => { cancelled = true; };
|
||||
}, [readingId]);
|
||||
|
||||
return { data, loading, error };
|
||||
}
|
||||
Reference in New Issue
Block a user