From 4ae4e874689a71e33cdd7a5799fc0c85c4861367 Mon Sep 17 00:00:00 2001
From: Joseph Ditton <jditton.atomic@gmail.com>
Date: Mon, 22 Nov 2021 14:21:53 -0700
Subject: adds start for console

---
 client/components/sign_up/_sign_up.jsx | 35 +++++++++++++++++++---------------
 1 file changed, 20 insertions(+), 15 deletions(-)

(limited to 'client/components/sign_up/_sign_up.jsx')

diff --git a/client/components/sign_up/_sign_up.jsx b/client/components/sign_up/_sign_up.jsx
index bbbd51b..13ac6c7 100644
--- a/client/components/sign_up/_sign_up.jsx
+++ b/client/components/sign_up/_sign_up.jsx
@@ -1,6 +1,10 @@
-import { useState } from 'react';
+import { useContext, useState } from 'react';
+import { useNavigate } from 'react-router';
+import { SettingsContext } from '../../utils/settings_context';
 
 export const SignUp = () => {
+  const [, dispatch] = useContext(SettingsContext);
+  const navigate = useNavigate();
   const [name, setName] = useState('');
   const [email, setEmail] = useState('');
   const [emailConfirmation, setEmailConfirmation] = useState('');
@@ -29,20 +33,21 @@ export const SignUp = () => {
       setErrorMessage('Name cannot be blank.');
       return;
     }
-    try {
-      await fetch('/users', {
-        method: 'POST',
-        headers: {
-          'Content-Type': 'application/json',
-        },
-        body: JSON.stringify({
-          name,
-          email,
-          password,
-        }),
-      });
-    } catch (e) {
-      console.log(e.message);
+    const res = await fetch('/users', {
+      method: 'POST',
+      headers: {
+        'Content-Type': 'application/json',
+      },
+      body: JSON.stringify({
+        name,
+        email,
+        password,
+      }),
+    });
+    if (res.status === 201) {
+      const result = await res.json();
+      dispatch({ type: 'update', payload: { jwt: result.token } });
+      navigate('/');
     }
   };
 
-- 
cgit v1.2.3-70-g09d2

