34 lines
838 B
JavaScript
34 lines
838 B
JavaScript
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 };
|
|
}
|