Skip to content

Commit

Permalink
Add actions for generic Panoptes resources
Browse files Browse the repository at this point in the history
  • Loading branch information
eatyourgreens committed Jun 6, 2017
1 parent a3d9ed7 commit cc3440f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/containers/ProjectContentsContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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) {
Expand All @@ -40,7 +40,7 @@ class ProjectContentsContainer extends Component {
}

render() {
const { contents } = this.props;
const { resource } = this.props;
return (
<div>
<BaseModal
Expand All @@ -60,15 +60,15 @@ class ProjectContentsContainer extends Component {
</ModalFooter>
</BaseModal>
<div onClick={this.handleClick}>
{React.cloneElement(this.props.children, { contents })}
{React.cloneElement(this.props.children, { contents: resource })}
</div>
</div>
);
}
}

const mapStateToProps = (state) => ({
contents: state.contents,
resource: state.resource,
});
const mapDispatchToProps = (dispatch) => ({
actions: bindActionCreators(contentsActions, dispatch),
Expand Down
9 changes: 3 additions & 6 deletions src/ducks/contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/ducks/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
69 changes: 69 additions & 0 deletions src/ducks/resource.js
Original file line number Diff line number Diff line change
@@ -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,
};

0 comments on commit cc3440f

Please sign in to comment.