Skip to content

Commit 30b5b3d

Browse files
creating categories from the client side
1 parent a85387f commit 30b5b3d

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

client/actions/category.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import fetch from "isomorphic-fetch";
2+
import {API} from "../config.js";
3+
import cookie from "js-cookie";
4+
5+
export const create = (category,token) => {
6+
return fetch(`${API}/api/category`, {
7+
method: 'POST',
8+
headers: {
9+
Accept: 'application/json',
10+
'Content-Type': 'application/json',
11+
Authorization:`Bearer ${token}`
12+
13+
},
14+
body: JSON.stringify(category)
15+
})
16+
.then(response => {
17+
return response.json();
18+
})
19+
.catch(error => console.log(error));
20+
};

client/components/Header.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react';
1+
import React, { useState,useEffect } from 'react';
22
import Link from "next/link";
33
import {APP_NAME} from "../config.js";
44
import NProgress from "nprogress";
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React,{useState,useEffect} from "react";
2+
import Link from "next/link";
3+
import Router from "next/router";
4+
import {isAuthenticated,getCookie} from "../../actions/authentication";
5+
import {create} from "../../actions/category";
6+
7+
8+
const Category =()=>{
9+
const [infos,setInfos] = useState({name:"",error:false,success:false,categories:[],removed:false});
10+
11+
const {name,error,success,categories,removed} = infos
12+
const token = getCookie("token")
13+
14+
15+
const handleSubmit =(event)=>{
16+
event.preventDefault()
17+
// console.log("create",name)
18+
create({name},token).then(data=>{
19+
if(data.error){
20+
setInfos({...infos,error:data.error,success:false})
21+
} else{
22+
setInfos({...infos,error:false,success:true,name:""})
23+
}
24+
});
25+
};
26+
27+
28+
const handleChange =(event)=>{
29+
setInfos({...infos,name:event.target.value,error:false,success:false,remove:""})
30+
31+
}
32+
33+
const newCategoryForm =()=>(
34+
<form onSubmit={handleSubmit}>
35+
<div className="form-group">
36+
<label className="text-muted">
37+
Name
38+
</label>
39+
<input type="text" className="form-control" onChange={handleChange} value={name} required/>
40+
</div>
41+
<div>
42+
<button type="submit" className="btn btn-info">Create</button>
43+
</div>
44+
</form>
45+
);
46+
47+
return (
48+
<>
49+
{newCategoryForm()}
50+
</>
51+
)
52+
53+
54+
55+
56+
57+
}
58+
59+
export default Category;

client/pages/adminDashboard/update/category-tag.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Layout from "../../../components/Layout";
22
import Admin from "../../../components/authentication/Admin";
3+
import Category from "../../../components/update/Category";
34
import Link from "next/link";
45

56

@@ -13,11 +14,12 @@ const CategoryTag =() =>{
1314
<h2>Create Catagories and Tags</h2>
1415
</div>
1516
<div className="col-md-6 pt-5 pb-5">
16-
<a>Categories</a>
17+
<Category/>
1718
</div>
1819
<div className="col-md-6 pt-5 pb-5">
1920
<a>Tags</a>
2021
</div>
22+
2123
</div>
2224
</div>
2325
</Admin>

0 commit comments

Comments
 (0)