|
| 1 | +import React, { FunctionComponent, useEffect, useState } from "react"; |
| 2 | +import { Html5Table, useDebouncedState, createFilter, useFilter } from 'window-table'; |
| 3 | +import { Loading } from "./loading"; |
| 4 | + |
| 5 | +interface BranchData { |
| 6 | + readonly code: string; |
| 7 | + readonly name: string; |
| 8 | + readonly kana: string; |
| 9 | + readonly hira: string; |
| 10 | + readonly roma: string; |
| 11 | +} |
| 12 | + |
| 13 | +interface State { |
| 14 | + loading: boolean; |
| 15 | + branches: BranchData[]; |
| 16 | + invalid: boolean; |
| 17 | +} |
| 18 | + |
| 19 | +const filter = createFilter(['code', 'name', 'kana', 'hira', 'roma']) |
| 20 | + |
| 21 | +export const Branches: FunctionComponent<{ code: string }> = ({ code }) => { |
| 22 | + const [state, change] = useState<State>({ loading: false, invalid: true, branches: [] }); |
| 23 | + const [text, debouncedText, setText] = useDebouncedState(''); |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + if (!code) { |
| 27 | + change({ ...state, loading: false, branches: [], invalid: true }); |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + change({ ...state, loading: true, invalid: false }); |
| 32 | + |
| 33 | + fetch(`/api/branches/${code}.json`) |
| 34 | + .then((res) => res.json()) |
| 35 | + .then((branches: { [key: string]: BranchData}) => { |
| 36 | + return Object.values(branches).sort((a, b) => parseInt(a.code, 10) - parseInt(b.code, 10)) |
| 37 | + }) |
| 38 | + .then((branches) => { |
| 39 | + change({ ...state, branches, loading: false, invalid: false }); |
| 40 | + }) |
| 41 | + .catch(() => { |
| 42 | + change({ ...state, loading: false, invalid: true }); |
| 43 | + }); |
| 44 | + }, [code]) |
| 45 | + |
| 46 | + const branches = useFilter(filter, state.branches, debouncedText) as BranchData[]; |
| 47 | + |
| 48 | + if (state.loading) { |
| 49 | + return <Loading message="Loading branches..." />; |
| 50 | + } |
| 51 | + |
| 52 | + return ( |
| 53 | + <div className="section"> |
| 54 | + {state.invalid ? ( |
| 55 | + <div className="notification has-text-centered"> |
| 56 | + <p>Select a bank</p> |
| 57 | + </div> |
| 58 | + ) : ( |
| 59 | + <> |
| 60 | + <form className="form mb-3"> |
| 61 | + <div className="field"> |
| 62 | + <div className="control has-icons-left"> |
| 63 | + <input |
| 64 | + type="search" |
| 65 | + className="input" |
| 66 | + placeholder="ex: 0001 or ginza or ぎんざ or 銀座" |
| 67 | + value={text} |
| 68 | + onChange={(e) => setText(e.target.value)} |
| 69 | + /> |
| 70 | + <span className="icon is-left"> |
| 71 | + <i className="fas fa-search" /> |
| 72 | + </span> |
| 73 | + </div> |
| 74 | + </div> |
| 75 | + </form> |
| 76 | + <Html5Table |
| 77 | + columns={[ |
| 78 | + { key: 'code', title: 'Code', width: 1 }, |
| 79 | + { key: 'name', title: 'Name', width: 2 }, |
| 80 | + ]} |
| 81 | + data={branches} |
| 82 | + className="is-fullwidth" |
| 83 | + style={{ height: 'min(30rem,100vh)', overflow: 'hidden' }} |
| 84 | + /> |
| 85 | + <div className="notification is-info is-light mt-3"> |
| 86 | + <p>This data by <a href={`/api/branches/${code}.json`}>https://zengin-code.github.io/api/branches/{code}.json</a> .</p> |
| 87 | + <p>You can use this JSON like API.</p> |
| 88 | + </div> |
| 89 | + </> |
| 90 | + )} |
| 91 | + </div> |
| 92 | + ) |
| 93 | +} |
0 commit comments