diff --git a/src/containers/ProjectContentsContainer.jsx b/src/containers/ProjectContentsContainer.jsx index 82a3691f..93e22f78 100644 --- a/src/containers/ProjectContentsContainer.jsx +++ b/src/containers/ProjectContentsContainer.jsx @@ -2,7 +2,7 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; -import * as contentsActions from '../ducks/contents'; +import * as contentsActions from '../ducks/resource'; import { BaseModal, ModalBody, ModalFooter } from 'pui-react-modals'; import { DefaultButton } from 'pui-react-buttons'; @@ -29,7 +29,7 @@ class ProjectContentsContainer extends Component { const { actions } = this.props; const type = this.props.routes[2].path; const id = type ? this.props.params.resource_id : this.props.params.project_id; - return actions.fetchProjectContents(id, type); + return actions.fetchResource(id, type); } handleClick(event) { @@ -40,7 +40,7 @@ class ProjectContentsContainer extends Component { } render() { - const { contents } = this.props; + const { resource } = this.props; return (
- {React.cloneElement(this.props.children, { contents })} + {React.cloneElement(this.props.children, { contents: resource })}
); @@ -68,7 +68,7 @@ class ProjectContentsContainer extends Component { } const mapStateToProps = (state) => ({ - contents: state.contents, + resource: state.resource, }); const mapDispatchToProps = (dispatch) => ({ actions: bindActionCreators(contentsActions, dispatch), diff --git a/src/ducks/contents.js b/src/ducks/contents.js index f36c032e..a6696e07 100644 --- a/src/ducks/contents.js +++ b/src/ducks/contents.js @@ -26,16 +26,13 @@ const projectContentsReducer = (state = initialState, action) => { }; // Action Creators -const fetchProjectContents = (id, type) => { - type = type ? type.split('/')[0] : 'project'; +const fetchProjectContents = (project_id) => { return (dispatch) => { dispatch({ type: FETCH_PROJECT_CONTENTS, }); - const key = `${type}_id`; - const query = {}; - query[key] = id; - apiClient.type(`${type}_contents`).get(query) + const query = { project_id }; + apiClient.type('project_contents').get(query) .then((projectContents) => { dispatch({ type: FETCH_PROJECT_CONTENTS_SUCCESS, diff --git a/src/ducks/reducer.js b/src/ducks/reducer.js index 47f6349e..1ba5aebb 100644 --- a/src/ducks/reducer.js +++ b/src/ducks/reducer.js @@ -2,9 +2,11 @@ import { combineReducers } from 'redux'; import contents from './contents'; import login from './login'; import projects from './projects'; +import resource from './resource'; export default combineReducers({ contents, login, - projects + projects, + resource }); diff --git a/src/ducks/resource.js b/src/ducks/resource.js new file mode 100644 index 00000000..542989bd --- /dev/null +++ b/src/ducks/resource.js @@ -0,0 +1,69 @@ +import apiClient from 'panoptes-client/lib/api-client'; + +// Action Types +export const FETCH_RESOURCE = 'FETCH_RESOURCE'; +export const FETCH_RESOURCE_SUCCESS = 'FETCH_RESOURCE_SUCCESS'; +export const FETCH_RESOURCE_ERROR = 'FETCH_RESOURCE_ERROR'; + +// Reducer +const initialState = { + data: [], + error: false, + loading: false, +}; + +const resourceReducer = (state = initialState, action) => { + switch (action.type) { + case FETCH_RESOURCE: + return Object.assign({}, initialState, { loading: true }); + case FETCH_RESOURCE_SUCCESS: + return Object.assign({}, state, { data: action.payload, loading: false }); + case FETCH_RESOURCE_ERROR: + return Object.assign({}, state, { error: action.payload, loading: false }); + default: + return state; + } +}; + +// Action Creators +const fetchResource = (id, type) => { + type = type ? type.split('/')[0] : 'project'; + return (dispatch) => { + dispatch({ + type: FETCH_RESOURCE, + }); + const key = `${type}_id`; + const query = {}; + query[key] = id; + apiClient.type(`${type}_contents`).get(query) + .then((resource) => { + dispatch({ + type: FETCH_RESOURCE_SUCCESS, + payload: resource, + }); + }); + }; +}; + +const createNewTranslation = (type) => + (dispatch, getState) => { + const { contents } = getState(); + const translation = apiClient.type(type).create({ + title: contents.title, + description: contents.description, + introduction: contents.introduction, + language: 'nz', + 'links.project': contents.links.project, + }); + translation.save() + .then(res => console.info('Saved! ', res)) + .catch(error => console.error(error)); + }; + +// Exports +export default resourceReducer; + +export { + createNewTranslation, + fetchResource, +};