diff --git a/src/actions/frequency.js b/src/actions/frequency.js new file mode 100644 index 0000000..5731a6a --- /dev/null +++ b/src/actions/frequency.js @@ -0,0 +1,67 @@ +import { RSAA } from 'redux-api-middleware' + +import * as types from '../constants/ActionTypes' +import { RSAAHeaders } from '../utils/ApiClient' +import { API_URL } from '../constants/Api' + + +export const getFrequency = (query) => ({ + [RSAA]: { + endpoint: `${API_URL}/frequency/?${query || ''}`, + method: 'GET', + headers: RSAAHeaders, + bailout: (state) => state.frequency.fetching || state.frequency.query === query, + types: [ + { + type: types.FREQUENCY_REQUEST, + meta: { query: query }, + }, + types.FREQUENCY_SUCCESS, + types.FREQUENCY_FAILURE, + ] + } +}) + +export const updateFrequency = (id, body) => ({ + [RSAA]: { + endpoint: `${API_URL}/frequency/${id}/`, + body: JSON.stringify(body), + method: 'PATCH', + headers: RSAAHeaders, + types: [ + types.FREQUENCY_REQUEST, + types.FREQUENCY_UPDATE, + types.FREQUENCY_FAILURE, + ] + } +}) + +export const createFrequency = (body) => ({ + [RSAA]: { + endpoint: `${API_URL}/frequency/`, + body: JSON.stringify(body), + method: 'POST', + headers: RSAAHeaders, + types: [ + types.FREQUENCY_REQUEST, + types.FREQUENCY_CREATE, + types.FREQUENCY_FAILURE, + ] + } +}) + +export const deleteFrequency = (id) => ({ + [RSAA]: { + endpoint: `${API_URL}/frequency/${id}/`, + method: 'DELETE', + headers: RSAAHeaders, + types: [ + types.FREQUENCY_REQUEST, + { + type: types.FREQUENCY_DELETE, + meta: { id } + }, + types.FREQUENCY_FAILURE, + ] + } +}) diff --git a/src/actions/index.js b/src/actions/index.js index 889a7b8..7f2e8f6 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -97,11 +97,16 @@ export const polygonReset = () => { } } -export const lastCenterUpdate = (lat, lon) => { +export const lastCenterUpdate = (lat, lon, zoom) => { // this only update current map center from map movement alone + let payload = { + lastCenter: [lat, lon] + } + if (zoom !== undefined) + payload['zoom'] = zoom return { type: types.GEO_LASTCENTER_UPDATE, - payload: [lat, lon], + payload, } } diff --git a/src/actions/stop.js b/src/actions/stop.js index ad77118..5236450 100644 --- a/src/actions/stop.js +++ b/src/actions/stop.js @@ -33,6 +33,23 @@ export const updateStop = (id, body) => ({ } }) +export const mergeStop = (id, body) => ({ + [RSAA]: { + endpoint: `${API_URL}/stop/${id}/merge/`, + body: JSON.stringify(body), + method: 'POST', + headers: RSAAHeaders, + types: [ + types.STOP_REQUEST, + { + type: types.STOP_UPDATE, + meta: { id, opt: 'merge', mergeWith: body } + }, + types.STOP_FAILURE, + ] + } +}) + export const createStop = (body) => ({ [RSAA]: { endpoint: `${API_URL}/stop/`, diff --git a/src/components/FrequencyForm.js b/src/components/FrequencyForm.js new file mode 100644 index 0000000..aabe446 --- /dev/null +++ b/src/components/FrequencyForm.js @@ -0,0 +1,126 @@ +import React, { Component } from 'react' +import styled from 'styled-components' + +import Input from './parts/Input' +import Select from './parts/Select' +import { + updateFrequency, createFrequency, deleteFrequency +} from '../actions/frequency' +import store from '../store' +import { + ExactTimeChoices +} from '../constants/choices' +import { getItemFromList } from '../utils' + +const StyleBox = styled.div` +padding: 5px; +background: white; +margin-bottom: 1rem; +` + +class FrequencyForm extends Component { + + cancel = null + state = { + editMode: false, + trip: null, + id: null, + } + + constructor(props) { + super(props) + this.handleChange = this.handleChange.bind(this) + this.handleSubmit = this.handleSubmit.bind(this) + this.handleDelete = this.handleDelete.bind(this) + } + + handleChange(evt) { + let updated = {} + updated[evt.target.name] = evt.target.value + this.setState(updated) + } + + handleSubmit() { + const { id } = this.state + let body = {...this.state } + delete body.id + if (id !== null) { + store.dispatch(updateFrequency(id, body)) + } else { + store.dispatch(createFrequency(body)) + } + this.props.toggleEditMode() + } + + handleDelete() { + const { id } = this.state + store.dispatch(deleteFrequency(id)) + this.props.toggleEditMode() + } + + static getDerivedStateFromProps(props, state) { + if (props.item && props.item.id !== null && state.id === null) { + return props.item + } else if (props.trip !== undefined) { + return { trip: props.trip } + } + return null + } + + render() { + const item = this.state + return ( + + + + + + +