-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
creating categories from the client side
- Loading branch information
1 parent
a85387f
commit 30b5b3d
Showing
4 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import fetch from "isomorphic-fetch"; | ||
import {API} from "../config.js"; | ||
import cookie from "js-cookie"; | ||
|
||
export const create = (category,token) => { | ||
return fetch(`${API}/api/category`, { | ||
method: 'POST', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
Authorization:`Bearer ${token}` | ||
|
||
}, | ||
body: JSON.stringify(category) | ||
}) | ||
.then(response => { | ||
return response.json(); | ||
}) | ||
.catch(error => console.log(error)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React,{useState,useEffect} from "react"; | ||
import Link from "next/link"; | ||
import Router from "next/router"; | ||
import {isAuthenticated,getCookie} from "../../actions/authentication"; | ||
import {create} from "../../actions/category"; | ||
|
||
|
||
const Category =()=>{ | ||
const [infos,setInfos] = useState({name:"",error:false,success:false,categories:[],removed:false}); | ||
|
||
const {name,error,success,categories,removed} = infos | ||
const token = getCookie("token") | ||
|
||
|
||
const handleSubmit =(event)=>{ | ||
event.preventDefault() | ||
// console.log("create",name) | ||
create({name},token).then(data=>{ | ||
if(data.error){ | ||
setInfos({...infos,error:data.error,success:false}) | ||
} else{ | ||
setInfos({...infos,error:false,success:true,name:""}) | ||
} | ||
}); | ||
}; | ||
|
||
|
||
const handleChange =(event)=>{ | ||
setInfos({...infos,name:event.target.value,error:false,success:false,remove:""}) | ||
|
||
} | ||
|
||
const newCategoryForm =()=>( | ||
<form onSubmit={handleSubmit}> | ||
<div className="form-group"> | ||
<label className="text-muted"> | ||
Name | ||
</label> | ||
<input type="text" className="form-control" onChange={handleChange} value={name} required/> | ||
</div> | ||
<div> | ||
<button type="submit" className="btn btn-info">Create</button> | ||
</div> | ||
</form> | ||
); | ||
|
||
return ( | ||
<> | ||
{newCategoryForm()} | ||
</> | ||
) | ||
|
||
|
||
|
||
|
||
|
||
} | ||
|
||
export default Category; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters