Skip to content

Commit

Permalink
Attempt to add user data to JSON File - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jonty-terrence committed Sep 9, 2020
1 parent bf43571 commit b856bcd
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 55 deletions.
13 changes: 7 additions & 6 deletions client/api/metaData.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import consume from './requestor'
import request from 'superagent'

export function addToFile (addedData) {
return consume('/', 'post', addedData)
.then(res => {
return res.body
})
const dataUrl = 'http://localhost:3000/api/v1'

export function addToFile (data) {
return request
.post(dataUrl)
.send(data)
}
2 changes: 1 addition & 1 deletion client/components/Preview.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { connect } from 'react-redux'

import { addToFile } from '../api/metaData'
import { addToFile } from '../api/metaData.js'

import TitleCharCount from './TitleCharCount'
import DescriptionCharCount from './DescriptionCharCount'
Expand Down
2 changes: 1 addition & 1 deletion metaData.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[object Object]
[object Object][object Object][object Object][object Object]
40 changes: 5 additions & 35 deletions server/public/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,42 +135,12 @@ var collectUrl = function collectUrl(url) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "addToFile", function() { return addToFile; });
/* harmony import */ var _requestor__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./requestor */ "./client/api/requestor.js");

function addToFile(addedData) {
return Object(_requestor__WEBPACK_IMPORTED_MODULE_0__["default"])('/', 'post', addedData).then(function (res) {
return res.body;
});
}

/***/ }),

/***/ "./client/api/requestor.js":
/*!*********************************!*\
!*** ./client/api/requestor.js ***!
\*********************************/
/*! exports provided: default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "default", function() { return consume; });
/* harmony import */ var superagent__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! superagent */ "./node_modules/superagent/lib/client.js");
/* harmony import */ var superagent__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(superagent__WEBPACK_IMPORTED_MODULE_0__);

var baseUrl = '/api/v1';
function consume(endpoint) {
var method = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'get';
var data = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var payLoadMethod = method.toLowerCase() === 'get' ? 'query' : 'send';
var headers = {
Accept: 'application/json'
};
return superagent__WEBPACK_IMPORTED_MODULE_0___default.a[method](baseUrl + endpoint).set(headers)[payLoadMethod](data).then(function (res) {
return res;
})["catch"](function (err) {
throw err;
});
var dataUrl = 'http://localhost:3000/api/v1';
function addToFile(data) {
return superagent__WEBPACK_IMPORTED_MODULE_0___default.a.post(dataUrl).send(data);
}

/***/ }),
Expand Down Expand Up @@ -429,7 +399,7 @@ __webpack_require__.r(__webpack_exports__);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! react */ "./node_modules/react/index.js");
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_5___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_5__);
/* harmony import */ var react_redux__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! react-redux */ "./node_modules/react-redux/es/index.js");
/* harmony import */ var _api_metaData__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ../api/metaData */ "./client/api/metaData.js");
/* harmony import */ var _api_metaData_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ../api/metaData.js */ "./client/api/metaData.js");
/* harmony import */ var _TitleCharCount__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./TitleCharCount */ "./client/components/TitleCharCount.jsx");
/* harmony import */ var _DescriptionCharCount__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./DescriptionCharCount */ "./client/components/DescriptionCharCount.jsx");

Expand All @@ -449,7 +419,7 @@ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Re


var carryInfo = function carryInfo(data) {
Object(_api_metaData__WEBPACK_IMPORTED_MODULE_7__["addToFile"])(data);
Object(_api_metaData_js__WEBPACK_IMPORTED_MODULE_7__["addToFile"])(data);
};

var Preview = /*#__PURE__*/function (_React$Component) {
Expand Down
2 changes: 1 addition & 1 deletion server/public/bundle.js.map

Large diffs are not rendered by default.

42 changes: 32 additions & 10 deletions server/routes.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
const fs = require('fs')
const express = require('express')

const path = require('path')

const router = express.Router()

const filePath = path.join(__dirname, './metaData.json')

function add (data, callback) {
// console.log(lemon)
// read the file
fs.readFile(filePath, 'utf8', (err, contents) => {
// handle file read error
if(err) return callback(err)
// JSON.parse the contents
const lemons = JSON.parse(contents)
// add the lemon (push)
lemons.push(lemon)
// stringify the array
const newContents = JSON.stringify(lemons, null, 2)
// save/write the file
fs.writeFile(filePath, newContents, callback)
})
}

router.post('/', (req, res) => {
const { url, title, description } = req.body
const newData = {
url: url,
title: title,
description: description
}
return fs.appendFile('./metaData.json', newData, (err) => {
if (err) throw err
console.log('Added to file successfully')
})
const { url, title, description } = req.body
const lemon = { url, title, description }
add(lemon, (err) => {
// sending err.message is a bad security practice - should be sanitised
if(err) return res.status(500).send(err.message)
res.redirect('/')
})
fs.readFile(filePath, 'utf8', (err, contents) => {
if (err) return res.status(500).send(err.message)
console.log(JSON.parse(contents))
})
})

module.exports = router
2 changes: 1 addition & 1 deletion server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ server.use(express.static(path.join(__dirname, './public')))

const metaInfo = require('./routes.js')

server.use('/api/v1/', metaInfo)
server.use('/api/v1', metaInfo)

server.get('*', (req, res) => {
res.sendFile(path.join(__dirname, './public/index.html'))
Expand Down

0 comments on commit b856bcd

Please sign in to comment.