[React]_Axios

hanseungjuneยท2023๋…„ 2์›” 14์ผ
0

๋น„์ „๊ณต์ž์˜ IT์ค€๋น„

๋ชฉ๋ก ๋ณด๊ธฐ
44/68
post-thumbnail

๐Ÿ“Œ ๋ชฉ์ 

ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๋ฉด์„œ, ์—ฌ๋Ÿฌ ๋ฐฉ์‹์˜ Axios ์š”์ฒญ์ด ์žˆ์—ˆ๋Š”๋ฐ ์ด์— ๋Œ€ํ•œ ์ฝ”๋“œ๋ฅผ ์ €์žฅํ•˜๋ฉด์„œ ์•ž์œผ๋กœ ๊ณ„์† ์ฐธ๊ณ ํ•˜๋Š” ์‹์œผ๋กœ ๊ณต๋ถ€ํ•˜๋ ค๊ณ  ๋ถ™์ž„!

๐Ÿ“Œ ์ฝ”๋“œ ๋ฐ ์ฝ”๋“œ ์„ค๋ช…

๐Ÿคž ๋ฉ”์ธ ์ฝ”๋“œ - ๋ฆฌ์ŠคํŠธ GET

const [items, setItems] = useState([]);

  const setBranchSet = () => {
    axios({
      method: 'GET',
      url: 'http://localhost:8080/api/kiosk/home/kiosk-list'
    })
      .then((res) => {
        setItems(res.data)
    })
      .catch((err) => console.log(err))
  }

  useEffect(() => {
    setBranchSet();
  }, [])

๐Ÿคž ๊ด€๋ จ ์ฝ”๋“œ(map)

{
  items.map(item => {
    return <KioskBranchLocation key={item.id} id={item.id} name={item.name} />
  })
}

๐Ÿคž ๋ฉ”์ธ ์ฝ”๋“œ - ์šฐ์‚ฐ ๊ฐฏ์ˆ˜ ๋ณ€๊ฒฝ

  const { id } = useParams()
  const [rentCnt, setRentCnt] = useState(0);
  const [returnCnt, setReturnCnt] = useState(0);

  const BrollyURL = `http://localhost:8080/api/kiosk/home/brolly/${id}`
  axios({
    method: 'GET',
    url: BrollyURL,
  })
  .then((res) => {
    setRentCnt(res.data.brollyCnt)
    setReturnCnt(res.data.emptyCnt)
  })
  .catch((err) => {
    console.log(err)
  })

๐Ÿคž ๊ด€๋ จ ์ฝ”๋“œ(props)

<div css={KioskButtons}>
  <KioskRentBtn rentCnt={rentCnt} />
    <KioskRemoveEventListener />
  <KioskReturnBtn returnCnt={returnCnt} />
</div>

๐Ÿคž ๋ฉ”์ธ ์ฝ”๋“œ - ์ด๋ฏธ์ง€ ์„œ๋ฒ„ ์ „์†ก

const [loading, setLoading] = useState(false);

const saveImage = () => {
  // ๋ฐ์ดํ„ฐ URL๋กœ ๊ทธ๋Œ€๋กœ ๋ณด๋‚ด๊ธฐ
  const canvas = document.getElementById("$canvas");
  const imgURL = canvas.toDataURL("image/png");

  setLoading(true);

  axios({
    method: 'POST',
    url: 'https://localhost:8080/api/brolly/return',
    data: {
      'brollyName': qrdata,
      'caseId': id,
      'imgData': imgURL
    }
  })
    .then((res) => {
    console.log(res.data.success)
    if (!res.data.success) {
      clearImage();
      setOpen(true);
      setLoading(false);
    }
  })
    .catch((err) => console.log(err));
};

๐Ÿคž ๊ด€๋ จ ์ฝ”๋“œ(loading, modal)

<div css={SpinnerDiv}>
  {loading ? <Stack sx={{ zIndex: '990', backgroundColor: 'rgba(255, 255, 255, 0.5)', width: '102vw', height: '102vh', display: 'flex', justifyContent: 'center', alignItems: 'center' }} spacing={5} direction="row">
  <CircularProgress sx={{ color: 'white' }} />
  </Stack> : null}
</div>
<div>
  <Dialog
          sx={{ width: '100vw' }}
          open={open}
          TransitionComponent={Transition}
          keepMounted
          onClose={handleClose}
          aria-describedby="alert-dialog-slide-description"
          >
    <DialogTitle sx={{backgroundColor: '#EEF1FF', fontFamily: 'GangwonEduPowerExtraBoldA'}} fontSize={32} fontWeight={900}>{"์šฐ์‚ฐ์ด ์ธ์‹๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค."}</DialogTitle>
    <DialogContent sx={{backgroundColor: '#EEF1FF'}}>
      <DialogContentText sx={{fontFamily: 'GangwonEduPowerExtraBoldA'}} fontSize={20} fontWeight={700} id="alert-dialog-slide-description">
        ์šฐ์‚ฐ์„ ๊ผญ ํŽผ์ณ์„œ ์ดฌ์˜ํ•ด์ฃผ์„ธ์š”. ์šฐ์‚ฐ์ด ์ธ์‹๋˜์ง€ ์•Š์œผ๋ฉด ๋ฐ˜๋‚ฉ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์ง€์•Š์Šต๋‹ˆ๋‹ค.
      </DialogContentText>
    </DialogContent>
    <DialogActions sx={{backgroundColor: '#EEF1FF'}}>
      <Button sx={{fontFamily: 'GangwonEduPowerExtraBoldA'}} onClick={handleClose}>๋‹ซ๊ธฐ</Button>
    </DialogActions>
  </Dialog>
</div>

ํ•ด๋‹น ํƒœ๊ทธ๋Š” MUI ์‚ฌ์šฉ

๐Ÿคž ๋ฉ”์ธ ์ฝ”๋“œ - ์ฃผ์†Œ

const { id } = useParams()
  const [address, setAddress] = useState('');



  useEffect(() => {
    // ์ด๊ฑฐ ์ˆ˜์ •ํ•ด์•ผํ•จ
    const getAddress = () => {
      // ํ‚ค์˜ค์Šคํฌ geo ์—์„œ ์ง€์ ์— ํ•ด๋‹นํ•˜๋Š” ์œ„๋„ ๊ฒฝ๋„๊ฐ’ ๋ฐ›์•„์˜ค๊ธฐ
      // let geoURL = `http://192.168.100.80:8080/api/kiosk/home/kiosk-geo?id=1`
      let geoURL = `https://localhost:8080/api/kiosk/home/kiosk-geo?id=${id}`
      let addressURL = ``;
      axios.get(geoURL)
        .then((res) => {
          return res.data;
        })
        .then((data) => {
          // addressURL = `http://192.168.100.80:8080/api/address/reverse-geo?lat=${data.lat}&lng=${data.lng}`;
          addressURL = `https://localhost:8080/api/address/reverse-geo?lat=${data.lat}&lng=${data.lng}`;
          axios.get(addressURL)
            .then((res) => {
              setAddress(res.data.address_name)
            })
        })
        .catch((err) => console.log(err))
    };
    getAddress();
  }, [id])

๐Ÿคž ๋ฉ”์ธ ์ฝ”๋“œ - ์ฃผ์†Œ

const [celsius, setCelsius] = useState(0);
const [windspeed, setWindspeed] = useState(0);
const [imgsrc, setImgsrc] = useState("");
const { id } = useParams();

useEffect(() => {
  const getWeather = () => {
    // ํ‚ค์˜ค์Šคํฌ geo ์—์„œ ์ง€์ ์— ํ•ด๋‹นํ•˜๋Š” ์œ„๋„ ๊ฒฝ๋„๊ฐ’ ๋ฐ›์•„์˜ค๊ธฐ
    let geoURL = `https://localhost:8080/api/kiosk/home/kiosk-geo?id=${id}`;
    let weatherURL = ``;
    axios
      .get(geoURL)
      .then((res) => {
      return res.data;
    })
      .then((data) => {
      weatherURL = `https://localhost:8080/api/weather/current-weather?lat=${data.lat}&lng=${data.lng}`;
      axios.get(weatherURL).then((res) => {
        setImgsrc(res.data.icon);
        setCelsius(res.data.temp);
        setWindspeed(res.data.wind_speed);
      });
    })
      .catch((err) => console.log(err));
  };

  getWeather();
}, [id]);

๐Ÿคž ๊ด€๋ จ ์ฝ”๋“œ(๋‚ ์”จ)

<div css={KioskHomeWeatherTextBox}>
  <p className='celsius'>ํ˜„์žฌ ๊ธฐ์˜จ {Math.round(celsius).toFixed(1)}<span>โฐ</span></p>
  <p className='windspeed'>ํ’์† : {windspeed}(m/s)</p>
</div>

๐Ÿคž ๋ฉ”์ธ ์ฝ”๋“œ - ์˜์ˆ˜์ฆ

const { isBrolly } = useParams();
const { id } = useParams();
const navigate = useNavigate();
const [isReturn, setIsReturn] = useState(false);
const [kioskName, setKioskName] = useState('');

const location = useLocation();
const queryParams = new URLSearchParams(location.search)
const depositeMoney = Number(queryParams.get('depositeMoney')).toLocaleString('ko-KR');
const period = Number(queryParams.get('period')).toLocaleString('ko-KR');
const price = Number(queryParams.get('price')).toLocaleString('ko-KR');
const refundMoney = Number(queryParams.get('refundMoney')).toLocaleString('ko-KR');

const KioskNameURL = `https://localhost:8080/api/kiosk/home/kiosk-name?id=${id}`
  
useEffect(() => {
  if (isBrolly == 1) {
    setIsReturn(true)
  }
  axios({
    method:'GET',
    url: KioskNameURL,
  })
    .then((res) => setKioskName(res.data))
    .catch((err) => console.log(err))

  const timer = setTimeout(() => {
    navigate(`/kiosk/${id}`);
  }, 30000);

  return () => {
    clearTimeout(timer);
  };
}, [isReturn])

๐Ÿคž ๊ด€๋ จ ์ฝ”๋“œ(๊ฒฐ์ œ์‚ฌํ•ญ)

<div css={KioskReturnReceiptView}>
  <h1>์š”๊ธˆ ์‚ฌํ•ญ</h1>
  <div css={KioskReturnReceipt}>
    <div className='ReceiptTitle'>
      <p className='BranchName'>{kioskName}</p>
    </div>
    <div className='ReceiptTotal'>
      <p className='Payment'>{refundMoney}<span>์›</span></p>
    </div>
    <div className='ReceiptDetailView'>
      <div className='FirstHorizon'></div>
      <div className='ReceiptDetail'><span className='FontColorGray'>๋ณด์ฆ๊ธˆ</span><span>{depositeMoney}์›</span></div>
      <div className='ReceiptDetail'><span className='FontColorGray'>์ด์šฉ ๊ธฐ๊ฐ„</span><span>{period}์ผ</span></div>
      <div className='ReceiptDetail'><span className='FontColorGray'>์ด์šฉ ๊ธˆ์•ก</span><span>{price}์›</span></div>
      <div className='SecondHorizon'></div>
      <div className='ReceiptDetailRefunds'><span>ํ™˜๊ธ‰ ๊ธˆ์•ก</span><span>{refundMoney}์›</span></div>
    </div>
  </div>
</div>

๐Ÿ“Œ ํ›„๊ธฐ

axios ์š”์ฒญ์— ๋Œ€ํ•œ ํ˜•์‹์„ ์ตํž ์ˆ˜ ์žˆ์—ˆ๊ณ , ์•„์ง ๋กœ๊ทธ์ธ๊ณผ ํšŒ์›๊ฐ€์ž… ๋“ฑ์„ ํ• ๋•Œ, headers๋ž‘ Token ์ฒ˜๋ฆฌ ํ˜•์‹์„ ์•„์ง ๊ฒฝํ—˜ํ•ด๋ณด์ง€ ๋ชปํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด๋ฅผ ๊ฒฝํ—˜ํ•ด๋ณด๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ๊ฒฝํ—˜ํ•ด๋ณผ ๊ณ„ํš์ด๋‹ค.

profile
ํ•„์š”ํ•˜๋‹ค๋ฉด ๊ณต๋ถ€ํ•˜๋Š” ๊ฐœ๋ฐœ์ž, ํ•œ์Šน์ค€

0๊ฐœ์˜ ๋Œ“๊ธ€