[15 | 飲食店 表示] << [ホーム] >> [17 | react-dropzone]
「src/apis.js」ファイルに関数を追加します。
記述追加 【Desktop/QRMenu/qrmenu_react/src/apis.js】
import { toast } from 'react-toastify'; function request(path, {data = null, token = null, method = "GET" }) { return fetch(path, { method, headers: { Authorization: token ? `Token ${token}` : "", "Content-Type": "application/json", }, body: method !=="GET" && method !== "DELETE" ? JSON.stringify(data): null, }) .then((response) => { //もし成功したら if (response.ok) { if(method === "DELETE") { return true; } //toast.success("ログイン成功"); return response.json(); } //失敗 return response.json().then((json) => { //JSONエラー if (response.status === 400) { toast.error("氏名もしくはパスワードに間違いがあります。"); const errors = Object.keys(json).map( (k) => `${(json[k].join(" "))}` ); throw new Error(errors.join(" ")); } throw new Error(JSON.stringify(json)); }) .catch((e) => { if (e.name === "SyntaxError") { throw new Error(response.statusText); } throw new Error(e); }) }) .catch((e) => { //全エラー toast(e.message, { type: "error" }); }) } export function signIn(username, password) { return request("/auth/token/login/", { data: {username, password}, method: "POST", }) } export function register(username, password) { return request("/auth/users/", { data: {username, password}, method: "POST", }) } export function fetchPlaces(token) { return request("/api/places/", {token}); } export function addPlace(data, token) { return request("/api/places/", {data, token, method: "POST" }); }
「src」フォルダに「containers」フォルダを新規作成します。
作成した「containers」フォルダに「PlaceForm.js」ファイルを新規作成します。
新規作成 【Desktop/QRMenu/qrmenu_react/src/containers/PlaceForm.js】
import { Form, Button } from 'react-bootstrap'; import React, { useState, useContext } from 'react'; import { addPlace } from '../apis'; import AuthContext from '../contexts/AuthContext'; const PlaceForm =({ onDone }) => { const [name, setName] = useState(""); const [image, setImage] = useState(""); const auth = useContext(AuthContext); const onClick = async () => { const json = await addPlace({ name, image }, auth.token) if(json) { setName(""); setImage(""); onDone(); } } return ( <div> <h4 classname="text-center">飲食店</h4> <Form.Group> <Form.Label>飲食店名</Form.Label> <Form.Control type="text" placeholder="飲食店名を入力" value={name} onChange={(e) => setName(e.target.value)} /> </Form.Group> <Form.Group> <Form.Label>画像</Form.Label> <Form.Control type="text" placeholder="画像URLを入力" value={image} onChange={(e) => setImage(e.target.value)} /> </Form.Group> <Button variant="standard" block onClick={onClick}> 登録 </Button> </div> ) } export default PlaceForm;
「src/pages/Places.js」ファイルに記述を追加します。
記述追加 【QRMenu/qrmenu_react/src/pages/Places.js】
import { Row, Col, Modal } from 'react-bootstrap'; import { React, useEffect, useState, useContext } from 'react'; import styled from 'styled-components'; import { fetchPlaces } from '../apis'; import AuthContext from '../contexts/AuthContext'; import MainLayout from '../layouts/MainLayout'; import PlaceForm from '../containers/PlaceForm'; const Place = styled.div` margin-bottom: 20px; cursor: pointer; transition: all 0.2s; :hover { transform: scale(1.05); } > div { background-size: cover; height: 200px; border-radius: 5px; } > p { margin-top: 5px; font-size: 20px; font-weight: bold; } `; const AddPlaceButton = styled.div` border: 1px dashed gray; height: 200px; border-radius: 5px; display: flex; aligh-items: center; justify-content: center; font-size: 20px; cursor: pointer; background-color: white; :hover { background-color: #fbfbfb; } `; const Places = () => { const [places, setPlaces] = useState([]); const [show, setShow] = useState(false); const auth = useContext(AuthContext); const onHide = () => setShow(false); const onShow = () => setShow(true); const onFetchPlaces = async () => { const json = await fetchPlaces(auth.token); if (json) { setPlaces(json); } }; const onDone = () => { onFetchPlaces(); onHide(); } useEffect(() => { onFetchPlaces(); }, []); return ( <MainLayout> <h3>登録済み飲食店</h3> <br/> <Modal show={show} onHide={onHide} centerd> <Modal.Body> <PlaceForm onDone={onDone} /> </Modal.Body> </Modal> <Row> { places.map((place) => ( <Col key={place.id} lg={4}> <Place> <div style={{ backgroundImage: `url(${place.image})` }}></div> <p>{place.name}</p> </Place> </Col> ))} <Col lg={4}> <AddPlaceButton onClick={onShow}>飲食店登録</AddPlaceButton> </Col> </Row> </MainLayout> ) } export default Places;
ブラウザを確認します。
↓↓クリックして頂けると励みになります。
[15 | 飲食店 表示] << [ホーム] >> [17 | react-dropzone]