Mercurial > gemma
view client/src/store/imports.js @ 2787:2ce5ee6d65b5
client:translations: merge translations from weblate
author | Fadi Abbud <fadi.abbud@intevation.de> |
---|---|
date | Mon, 25 Mar 2019 10:34:33 +0100 |
parents | 7badd99c9315 |
children | c1e832f77726 |
line wrap: on
line source
/* This is Free Software under GNU Affero General Public License v >= 3.0 * without warranty, see README.md and license for details. * * SPDX-License-Identifier: AGPL-3.0-or-later * License-Filename: LICENSES/AGPL-3.0.txt * * Copyright (C) 2018 by via donau * – Österreichische Wasserstraßen-Gesellschaft mbH * Software engineering by Intevation GmbH * * Author(s): * Thomas Junk <thomas.junk@intevation.de> */ import { HTTP } from "@/lib/http"; import { WFS } from "ol/format.js"; import { equalTo as equalToFilter } from "ol/format/filter.js"; import { startOfHour } from "date-fns"; /* eslint-disable no-unused-vars */ /* eslint-disable no-unreachable */ const STATES = { NEEDSAPPROVAL: "pending", APPROVED: "accepted", REJECTED: "declined" }; const NODETAILS = -1; // initial state const init = () => { return { failed: false, pending: false, accepted: false, declined: false, warning: false, stretches: [], imports: [], reviewed: [], show: NODETAILS, showAdditional: NODETAILS, showLogs: NODETAILS, details: [], startDate: startOfHour(new Date()), endDate: new Date(), prev: null, next: null }; }; const getStretchFromWFS = filter => { return new Promise((resolve, reject) => { var stretchesFeatureCollectionRequest = new WFS().writeGetFeature({ srsName: "EPSG:4326", featureNS: "gemma", featurePrefix: "gemma", featureTypes: ["stretches_geoserver"], outputFormat: "application/json", filter: filter }); HTTP.post( "/internal/wfs", new XMLSerializer().serializeToString(stretchesFeatureCollectionRequest), { headers: { "X-Gemma-Auth": localStorage.getItem("token"), "Content-type": "text/xml; charset=UTF-8" } } ) .then(response => { resolve(response); }) .catch(error => { reject(error); }); }); }; const clearFilterCriteria = state => { state.warning = false; state.successful = false; state.failed = false; state.pending = false; state.accepted = false; state.declined = false; }; const imports = { init, namespaced: true, state: init(), getters: { filters: state => { return ["failed", "pending", "accepted", "declined", "warning"].filter( x => state[x] ); } }, mutations: { setPrev: (state, prev) => { state.prev = prev; }, setNext: (state, next) => { state.next = next; }, setStartDate: (state, start) => { state.startDate = start; }, setEndDate: (state, end) => { state.endDate = end; }, setCurrentDetails: (state, details) => { state.details = details; }, toggleFilter: (state, name) => { state[name] = !state[name]; const allSet = state.failed && state.pending && state.accepted && state.declined && state.warning; if (allSet) { clearFilterCriteria(state); } }, clearFilters: state => { clearFilterCriteria(state); }, setStretches: (state, stretches) => { state.stretches = stretches; }, setReviewed: (state, reviewed) => { state.reviewed = reviewed; }, setImports: (state, imports) => { const lookUp = state.reviewed.reduce((o, n) => { const { id, status } = n; o[id] = status; return o; }, {}); imports = imports.map(x => { if (x.state === "pending") { const reviewState = lookUp[x.id]; if (reviewState) { x.status = reviewState; } else { x.status = STATES.NEEDSAPPROVAL; } } return x; }); state.imports = imports; }, showDetailsFor: (state, id) => { state.show = id; }, hideDetails: state => { state.show = NODETAILS; }, showAdditionalInfoFor: (state, id) => { state.showAdditional = id; }, hideAdditionalInfo: state => { state.showAdditional = NODETAILS; }, showAdditionalLogsFor: (state, id) => { state.showLogs = id; }, hideAdditionalLogs: state => { state.showLogs = NODETAILS; }, toggleApprove: (state, change) => { const { id, newStatus } = change; const stagedResult = state.imports.find(e => { return e.id === id; }); if (stagedResult.status === newStatus) { stagedResult.status = STATES.NEEDSAPPROVAL; state.reviewed = state.reviewed.filter(x => x.id !== stagedResult.id); } else { stagedResult.status = newStatus; let index = state.reviewed.findIndex(r => r.id === id); if (index !== -1) { state.reviewed[index].status = newStatus; } else { state.reviewed.push({ id: stagedResult.id, status: newStatus }); } } } }, actions: { loadStretch({ commit }, name) { return new Promise((resolve, reject) => { getStretchFromWFS(equalToFilter("name", name)) .then(response => { resolve(response); }) .catch(error => { reject(error); }); }); }, loadStretches({ commit }) { return new Promise((resolve, reject) => { getStretchFromWFS(equalToFilter("staging_done", true)) .then(response => { if (response.data.features) { commit("setStretches", response.data.features); } else { commit("setStretches", []); } resolve(response); }) .catch(error => { reject(error); }); }); }, saveStretch({ commit }, stretch) { return new Promise((resolve, reject) => { HTTP.post("/imports/st", stretch, { headers: { "X-Gemma-Auth": localStorage.getItem("token") } }) .then(response => { resolve(response); }) .catch(error => { reject(error); }); }); }, getImports({ commit }, options) { let { filter, from, to, query } = options; let queryParams = ""; const hasWarning = filter.includes("warning"); filter = filter.filter(x => x != "warning"); if (filter && filter.length > 0) { queryParams = "?states=" + filter.join(","); if (hasWarning) queryParams += "&warnings=true"; queryParams += "&from=" + from; queryParams += "&to=" + to; queryParams += "&query=" + query; } else { if (hasWarning) { queryParams += "?warnings=true"; queryParams += "&from=" + from; queryParams += "&to=" + to; queryParams += "&query=" + query; } else { queryParams += "?from=" + from; queryParams += "&to=" + to; queryParams += "&query=" + query; } } return new Promise((resolve, reject) => { HTTP.get("/imports" + queryParams, { headers: { "X-Gemma-Auth": localStorage.getItem("token") } }) .then(response => { const { imports, prev, next } = response.data; commit("setPrev", prev); commit("setNext", next); commit("setImports", imports); resolve(response); }) .catch(error => { reject(error); }); }); }, confirmReview({ state }, reviewResults) { return new Promise((resolve, reject) => { HTTP.patch("/imports", reviewResults, { headers: { "X-Gemma-Auth": localStorage.getItem("token"), "Content-type": "application/json" } }) .then(response => { resolve(response); }) .catch(error => { reject(error); }); }); } } }; export { imports, STATES };