/* eslint import/default: off */
import _ from 'lodash';
import data from './data';
import serverErrors from '../../../server/api/util/server-errors';

// This is used to filter out bad keys from requests that are
// sent over
const categoryKeys = ['id', 'label', 'emoji'];

// We calculate the `lastId` so that new resources that are created
// have unique IDs
let lastId = data[data.length - 1].id;

export default function register(server) {
  server.respondWith(
    'GET',
    '/api/v1/categories',
    (req) => {
      req.respond(
        200,
        {'Content-Type': 'application/json'},
        JSON.stringify(data)
      );
    }
  );

  server.respondWith(
    'POST',
    '/api/v1/categories',
    (req) => {
      const json = JSON.parse(req.requestBody);
      const newCategory = {
        ...(_.pick(json, categoryKeys)),
        id: ++lastId
      };

      const jsonResponse = JSON.stringify(newCategory);
      req.respond(
        201,
        {'Content-Type': 'application/json'},
        jsonResponse
      );
    }
  );

  server.respondWith(
    'PATCH',
    /\/api\/v1\/categories\/(\d+)/,
    (req, id) => {
      const category = _.find(data, {id: parseInt(id)});
      if (!category) {
        const err = serverErrors.notFound.body();
        req.respond(
          404,
          {'Content-Type': 'application/json'},
          JSON.stringify(err)
        );
      } else {
        const json = JSON.parse(req.requestBody);
        const newCategory = {
          ...category,
          ...(_.pick(json, categoryKeys))
        };

        const jsonResponse = JSON.stringify(newCategory);
        req.respond(
          200,
          {'Content-Type': 'application/json'},
          jsonResponse
        );
      }
    }
  );

  server.respondWith(
    'DELETE',
    /\/api\/v1\/categories\/(\d+)/,
    (req, id) => {
      const category = _.find(data, {id: parseInt(id)});
      if (!category) {
        const err = serverErrors.notFound.body();
        req.respond(
          404,
          {'Content-Type': 'application/json'},
          JSON.stringify(err)
        );
      } else {
        req.respond(204);
      }
    }
  );
}

@@ -145,16 +145,51 @@
 rors';%0A%0A
+let mockData = _.cloneDeep(data);%0A%0A
 // This 
@@ -407,14 +407,22 @@
 d = 
-data%5Bd
+mockData%5BmockD
 ata.
@@ -656,17 +656,21 @@
 ringify(
-d
+mockD
 ata)%0A   
@@ -906,24 +906,59 @@
 d%0A      %7D;%0A%0A
+      mockData.push(newCategory);%0A%0A
       const 
@@ -1233,33 +1233,37 @@
 tegory = _.find(
-d
+mockD
 ata, %7Bid: parseI
@@ -1547,67 +1547,32 @@
     
-const newCategory = %7B%0A          ...category,%0A          ...(
+Object.assign(category, 
 _.pi
@@ -1598,18 +1598,8 @@
 ys))
-%0A        %7D
 ;%0A%0A 
@@ -1633,36 +1633,33 @@
  JSON.stringify(
-newC
+c
 ategory);%0A      
@@ -1873,32 +1873,57 @@
  (req, id) =%3E %7B%0A
+      id = parseInt(id);%0A
       const cate
@@ -1940,17 +1940,21 @@
 ind(
-d
+mockD
 ata, %7Bid
 : pa
@@ -1945,38 +1945,24 @@
 ockData, %7Bid
-: parseInt(id)
 %7D);%0A      if
@@ -2158,32 +2158,128 @@
 %0A      %7D else %7B%0A
+        // Delete this category from our mock data%0A        mockData = _.reject(mockData, %7Bid%7D);%0A
         req.resp

Update mock data as user interacts with app