add files, server side, only css and js files

This commit is contained in:
catarak 2016-07-13 18:53:56 -04:00
parent 70588cd487
commit 4d6e4857ba
7 changed files with 71 additions and 11 deletions

View file

@ -1,4 +1,7 @@
import * as ActionTypes from '../../../constants'; import * as ActionTypes from '../../../constants';
import axios from 'axios';
const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api';
export function updateFileContent(name, content) { export function updateFileContent(name, content) {
return { return {
@ -9,9 +12,39 @@ export function updateFileContent(name, content) {
} }
// TODO make req to server // TODO make req to server
export function createFile(name) { export function createFile(formProps) {
return { return (dispatch, getState) => {
type: ActionTypes.CREATE_FILE, const state = getState();
name if (state.project.id) {
const postParams = {
name: formProps.name
};
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
.then(response => {
dispatch({
type: ActionTypes.CREATE_FILE,
...response.data
});
})
.catch(response => dispatch({
type: ActionTypes.ERROR,
error: response.data
}));
} else {
let maxFileId = 0;
state.files.forEach(file => {
if (parseInt(file.id, 10) > maxFileId) {
maxFileId = parseInt(file.id, 10);
}
});
dispatch({
type: ActionTypes.CREATE_FILE,
name: formProps.name,
id: `${maxFileId + 1}`
});
dispatch({
type: ActionTypes.HIDE_MODAL
});
}
}; };
} }

View file

@ -51,12 +51,10 @@ function validate(formProps) {
if (!formProps.name) { if (!formProps.name) {
errors.name = 'Please enter a name'; errors.name = 'Please enter a name';
} else if (!formProps.name.match(/(.+\.js$|.+\.css$)/)) {
errors.name = 'File must be of type JavaScript or CSS.';
} }
// if (formProps.name.match(/(.+\.js$|.+\.css$)/)) {
// errors.name = 'File must be of type JavaScript or CSS.';
// }
return errors; return errors;
} }

View file

@ -64,8 +64,8 @@ const files = (state = initialState, action) => {
return [...action.files]; return [...action.files];
case ActionTypes.SET_PROJECT: case ActionTypes.SET_PROJECT:
return [...action.files]; return [...action.files];
case ActionTypes.NEW_FILE: case ActionTypes.CREATE_FILE:
return [{ name: action.name, content: '' }, ...state.files]; return [...state, { name: action.name, id: action.id, content: '' }];
default: default:
return state; return state;
} }

View file

@ -1,7 +1,7 @@
.modal { .modal {
position: absolute; position: absolute;
top: #{66 / $base-font-size}rem; top: #{66 / $base-font-size}rem;
right: #{400 / $base-font-size}rem;; right: #{400 / $base-font-size}rem;
z-index: 100; z-index: 100;
} }

View file

@ -0,0 +1,19 @@
import Project from '../models/Project'
// Bug -> timestamps don't get created, but it seems like this will
// be fixed in mongoose soon
// https://github.com/Automattic/mongoose/issues/4049
export function createFile(req, res) {
Project.findByIdAndUpdate(req.params.project_id,
{
$push: {
'files': req.body
}
},
{
new: true
}, (err, updatedProject) => {
if (err) { return res.json({ success: false }); }
return res.json(updatedProject.files[updatedProject.files.length - 1]);
});
}

View file

@ -0,0 +1,8 @@
import { Router } from 'express';
import * as FileController from '../controllers/file.controller';
const router = new Router();
router.route('/projects/:project_id/files').post(FileController.createFile);
export default router;

View file

@ -27,6 +27,7 @@ import serverConfig from './config';
import users from './routes/user.routes'; import users from './routes/user.routes';
import sessions from './routes/session.routes'; import sessions from './routes/session.routes';
import projects from './routes/project.routes'; import projects from './routes/project.routes';
import files from './routes/file.routes';
import serverRoutes from './routes/server.routes'; import serverRoutes from './routes/server.routes';
// Body parser, cookie parser, sessions, serve public assets // Body parser, cookie parser, sessions, serve public assets
@ -55,6 +56,7 @@ app.use(passport.session());
app.use('/api', users); app.use('/api', users);
app.use('/api', sessions); app.use('/api', sessions);
app.use('/api', projects); app.use('/api', projects);
app.use('/api', files);
// this is supposed to be TEMPORARY -- until i figure out // this is supposed to be TEMPORARY -- until i figure out
// isomorphic rendering // isomorphic rendering
app.use('/', serverRoutes); app.use('/', serverRoutes);