Skip to content

Commit

Permalink
elon bilan yangilik funksiyalari o'zgardi
Browse files Browse the repository at this point in the history
  • Loading branch information
masanov3167 committed Jan 28, 2023
1 parent 9d61b34 commit 41911cb
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 111 deletions.
58 changes: 56 additions & 2 deletions src/context/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React, { useState, useEffect } from "react";
import draftToHtml from "draftjs-to-html";
import {convertToRaw} from "draft-js"
import { useNavigate } from "react-router-dom";

const Context = React.createContext();

function Provider({ children }) {
const navigate = useNavigate()
const [lang, setLang] = useState(JSON.parse(localStorage.getItem("lang")) || "uz");
const [refresh, setRefresh] = useState(false);
const [searchedData, setSearchedData] = useState([]);
Expand All @@ -13,8 +15,8 @@ function Provider({ children }) {
const [textEditorBodyEn, setTextEditorBodyEn] = useState();
const [names, setNames] = useState();
const [selectValue, setSelectValue] = useState();
// const globalUrl = "http://localhost:5000";
const globalUrl = "http://backend.tkti.uz";
const globalUrl = "http://localhost:5000";
// const globalUrl = "http://backend.tkti.uz";


const time = (arg) => {
Expand Down Expand Up @@ -59,12 +61,64 @@ function Provider({ children }) {
.catch(() => setMyState({ error: true }));
}

function DataDeleter(url) {
fetch(`${globalUrl}/${url}`, {
method: "DELETE",
headers: {
"Content-type": "application/json",
token: localStorage.getItem("token"),
},
})
.then((res) => res.json())
.then((res) => {
if(res.status === 401 || res.status === 498){
localStorage.removeItem('token');
navigate('/login')
}
if (!res.success) {
alert(res.message + "❌");
} else {
alert(res.message + '👏');
window.location.reload(false);
}
})
.catch((err) => {
console.log(err);
});
}

function DataPoster(url, body){
fetch(`${globalUrl}/${url}`, {
method: "POST",
headers: {
token: localStorage.getItem("token"),
},
body: body,
})
.then((res) => res.json())
.then((res) => {
if(res.status === 401 || res.status === 498){
localStorage.removeItem('token');
navigate('/login')
}
if (!res.success) {
alert(res.message + "❌");
} else {
alert(res.message);
window.location.reload(false);
}
})
.catch((err) => console.log(err));
}

return (
<>
<Context.Provider
value={{
globalUrl,
DataGetter,
DataDeleter,
DataPoster,
time,
textSytles,
convertToHtml,
Expand Down
125 changes: 57 additions & 68 deletions src/pages/admin/elon/Elon.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import { Context } from "../../../context";

const Elon = () => {
const imgRef = useRef();
const { globalUrl, names, convertToHtml } = useContext(Context);
const {time, globalUrl, names, convertToHtml ,DataDeleter,DataPoster, DataGetter} = useContext(Context);
const [type, setType] = useState("table");
const [data, setData] = useState();
const [data, setData] = useState({isFetched: false,error: false,data: {}});
const [edit,setEdit] = useState({display:false, edit:false})

let content = null;

Expand All @@ -30,41 +31,54 @@ const Elon = () => {
EditorState.createEmpty()
);

const putElon = id =>{
const title_uz = document.querySelector(`.elon__title_uz${id}`).value
const title_ru = document.querySelector(`.elon__title_ru${id}`).value
const title_en = document.querySelector(`.elon__title_en${id}`).value

console.log(title_en, title_ru,title_uz);
// tablega chunmey form qo'sholmadim form bo'sa namedan value olardim
setEdit({display:false, id:false})
}

const analyseNameTableHead = ["Tartib raqam", "Elon nomi", "Amallar"];
const renderHead = (item, index) => <th key={index}>{item}</th>;
const bodyData = data;
const bodyData = data.data;
const renderBody = (item, index) => {
return (
<tr key={index} style={{ cursor: "pointer", userSelect: "none" }}>
<td>{index + 1}</td>
<td>{item.title_uz}</td>
<td>
<button className="event-btn edit" onClick={() => {}}>
<i className="fa fa-edit"></i>
</button>
<button
className="event-btn delete"
onClick={() => deleteYangilik(item._id)}
>
<i className="fa fa-trash"></i>
</button>
</td>
</tr>
edit.display && edit.id ===item._id ?(
<table>
<tr>
<td colSpan={3}>
<td><input className={`form-control elon__title_uz${item._id}`} type="text" defaultValue={item.title_uz} /></td>
<td><input className={`form-control elon__title_ru${item._id}`} type="text" defaultValue={item.title_ru} /></td>
<td><input className={`form-control elon__title_en${item._id}`} type="text" defaultValue={item.title_en} /></td>
</td>
</tr>
<td><button onClick={() => putElon(item._id)} >Submit</button></td>
<td><button onClick={() => setEdit({display:false, id:false})} >close</button></td>
</table>
):(
<tr key={index} style={{ cursor: "pointer", userSelect: "none" }}>
<td>{index + 1}</td>
<td>{item.title_uz}</td>
<td>
<button className="event-btn edit" onClick={() => setEdit({display:true, id: item._id})}>
<i className="fa fa-edit"></i>
</button>
<button
className="event-btn delete"
onClick={() => deleteYangilik(item._id)}
>
<i className="fa fa-trash"></i>
</button>
</td>
</tr>
)
);
};

function getData() {
fetch(`${globalUrl}/elon/all`, {
headers: {
"Content-type": "application/json",
},
})
.then((res) => res.json())
.then((res) => {
setData(res.data);
})
.catch((err) => console.log(err));
}


function postData(e) {
e.preventDefault();
Expand All @@ -75,61 +89,32 @@ const Elon = () => {
formData.append("title_uz", names?.nameUz);
formData.append("title_ru", names?.nameRu);
formData.append("title_en", names?.nameEn);
formData.append("date", time(e.target.fordate.value));
formData.append("photo", imgRef.current.files[0]);

fetch(`${globalUrl}/elon/add`, {
method: "POST",
headers: {
Token: localStorage.getItem("token"),
},
body: formData,
})
.then((res) => res.json())
.then((res) => {
if (!res.success) {
alert(res.message + "❌");
} else {
alert(res.message);
window.location.reload(false);
}
})
.catch((err) => console.log(err));
DataPoster('elon/add', formData)
}

function deleteYangilik(id) {
fetch(`${globalUrl}/elon/${id}`, {
method: "DELETE",
headers: {
"Content-type": "application/json",
Token: localStorage.getItem("token"),
},
})
.then((res) => res.json())
.then((res) => {
if (!res.success) {
alert(res.message + "❌");
} else {
alert("Malumotlar o'chirildi");
window.location.reload(false);
}
})
.catch((err) => {
console.log(err);
});
DataDeleter(`elon/${id}`)
}

useEffect(() => {
getData();
DataGetter(setData, 'elon/all')
}, []);

if (type === "table") {
content = (
<Table
data.isFetched && data.data ?(
<Table
headData={analyseNameTableHead}
renderHead={renderHead}
bodyData={bodyData}
renderBody={renderBody}
/>
):(
<></>
)
);
} else {
content = (
Expand Down Expand Up @@ -173,6 +158,10 @@ const Elon = () => {
/>
</div>

<div className="file w-25">
<input className="form-control" type="datetime-local" name="fordate"/>
</div>

<div className="file">
<label htmlFor="forImg">
<input
Expand Down
57 changes: 16 additions & 41 deletions src/pages/admin/yangilik/Yangilik.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import { Context } from "../../../context";

const Yangilik = () => {
const imgRef = useRef();
const { globalUrl, names, convertToHtml } = useContext(Context);
const {time, globalUrl, names, convertToHtml,DataDeleter,DataPoster, DataGetter } = useContext(Context);
const [type, setType] = useState("table");
const [data, setData] = useState();
const [data, setData] = useState({isFetched: false,error: false,data: {}});

let content = null;

Expand All @@ -32,7 +32,7 @@ const Yangilik = () => {

const analyseNameTableHead = ["Tartib raqam", "Yangilik nomi", "Amallar"];
const renderHead = (item, index) => <th key={index}>{item}</th>;
const bodyData = data;
const bodyData = data.data;
const renderBody = (item, index) => {
return (
<tr key={index} style={{ cursor: "pointer", userSelect: "none" }}>
Expand Down Expand Up @@ -75,61 +75,32 @@ const Yangilik = () => {
formData.append("title_uz", names?.nameUz);
formData.append("title_ru", names?.nameRu);
formData.append("title_en", names?.nameEn);
formData.append("date", time(e.target.fordate.value));
formData.append("photo", imgRef.current.files[0]);

fetch(`${globalUrl}/news/add`, {
method: "POST",
headers: {
Token: localStorage.getItem("token"),
},
body: formData,
})
.then((res) => res.json())
.then((res) => {
if (!res.success) {
alert(res.message + "❌");
} else {
alert(res.message);
window.location.reload(false);
}
})
.catch((err) => console.log(err));
DataPoster('news/add', formData)
}

function deleteYangilik(id) {
fetch(`${globalUrl}/news/${id}`, {
method: "DELETE",
headers: {
"Content-type": "application/json",
Token: localStorage.getItem("token"),
},
})
.then((res) => res.json())
.then((res) => {
if (!res.success) {
alert(res.message + "❌");
} else {
alert("Malumotlar o'chirildi");
window.location.reload(false);
}
})
.catch((err) => {
console.log(err);
});
DataDeleter(`news/${id}`)
}

useEffect(() => {
getData();
DataGetter(setData, 'news/all')
}, []);

if (type === "table") {
content = (
<Table
data.isFetched && data.data ?(
<Table
headData={analyseNameTableHead}
renderHead={renderHead}
bodyData={bodyData}
renderBody={renderBody}
/>
):(
<></>
)
);
} else {
content = (
Expand Down Expand Up @@ -173,6 +144,10 @@ const Yangilik = () => {
/>
</div>

<div className="file w-25">
<input className="form-control" type="datetime-local" name="fordate"/>
</div>

<div className="file">
<label htmlFor="forImg">
<input
Expand Down

0 comments on commit 41911cb

Please sign in to comment.