UNPKG

@readr-media/react-election-widgets

Version:
218 lines (196 loc) 6.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _axios = _interopRequireDefault(require("axios")); var _errors = _interopRequireDefault(require("@twreporter/errors")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /** * @typedef {Object} SeatData * @property {{label: string, seats: number}[]} parties */ let Loader = /*#__PURE__*/function () { /** * @constructor * @param {Object} props * @param {string} [props.apiUrl='https://whoareyou-gcs.readr.tw'] */ function Loader({ apiUrl = 'https://whoareyou-gcs.readr.tw/elections' }) { _classCallCheck(this, Loader); _defineProperty(this, "apiUrl", 'https://whoareyou-gcs.readr.tw/elections'); this.apiUrl = apiUrl; } /** * Load data from web service. * @param {Object} props * @param {string} props.year * @param {string} props.type * @param {string} props.filename * @throws Error * @returns {Promise<SeatData>} */ _createClass(Loader, [{ key: "loadData", value: async function loadData({ year, type, filename }) { try { const axiosRes = await _axios.default.get(`${this.apiUrl}/${year}/${type}/${filename}`); return axiosRes === null || axiosRes === void 0 ? void 0 : axiosRes.data; } catch (err) { const annotatedErr = _errors.default.helpers.annotateAxiosError(err); throw annotatedErr; } } /** * Load data from web service. * @param {Object} props * @param {string} props.year * @param {string} props.countyCode - county code, see `Loader.countyCodes` for more info * @throws Error * @returns {Promise<SeatData>} */ }, { key: "loadCouncilMemberData", value: function loadCouncilMemberData({ year, countyCode }) { return this.loadData({ type: 'councilMember', year, filename: `seat/county/${countyCode}.json` }); } /** * Load data from web service. * @param {Object} props * @param {string} props.year * @param {string} props.countyCode - county code, see `Loader.countyCodes` for more info * @throws Error * @returns {Promise<SeatData>} */ }, { key: "loadAreaLegislatorData", value: function loadAreaLegislatorData({ year, countyCode }) { return this.loadData({ type: 'legislator', year, filename: `seat/county/normal/${countyCode}.json` }); } /** * Load data from web service. * @param {Object} props * @param {string} props.year * @param {string} props.countyCode - county code, see `Loader.countyCodes` for more info * @throws Error * @returns {Promise<SeatData>} */ }, { key: "loadAllLegislatorData", value: function loadAllLegislatorData({ year }) { return this.loadData({ type: 'legislator', year, filename: `seat/country/all/country.json` }); } /** * Load data from web service. * @param {Object} props * @param {string} props.year * @throws Error * @returns {Promise<SeatData>} */ }, { key: "loadMountainIndigenousLegislatorData", value: function loadMountainIndigenousLegislatorData({ year }) { return this.loadData({ type: 'legislator', year, filename: `seat/country/mountain-indigenous/country.json` }); } /** * Load data from web service. * @param {Object} props * @param {string} props.year * @throws Error * @returns {Promise<SeatData>} */ }, { key: "loadPlainIndigenousLegislatorData", value: function loadPlainIndigenousLegislatorData({ year }) { return this.loadData({ type: 'legislator', year, filename: `seat/country/plain-indigenous/country.json` }); } /** * Load data from web service. * @param {Object} props * @param {string} props.year * @throws Error * @returns {Promise<SeatData>} */ }, { key: "loadPartyLegislatorData", value: function loadPartyLegislatorData({ year }) { return this.loadData({ type: 'legislator', year, filename: `seat/country/party/country.json` }); } }]); return Loader; }(); exports.default = Loader; Loader.electionTypes = ['councilMember' // 縣市議員 ]; Loader.electionYears = ['2014', '2018', '2022']; Loader.countyCodes = ['09007', // 連江縣 '09020', // 金門縣 '10002', // 宜蘭縣 '10004', // 新竹縣 '10005', // 苗栗縣 '10007', // 彰化縣 '10008', // 南投縣 '10009', // 雲林縣 '10010', // 嘉義縣 '10013', // 屏東縣 '10014', // 台東縣 '10015', // 花蓮縣 '10016', // 澎湖縣 '10017', // 基隆市 '10018', // 新竹市 '10020', // 嘉義市 '63000', // 台北市 '64000', // 高雄市 '65000', // 新北市 '66000', // 台中市 '67000', // 台南市 '68000' // 桃園市 ];