This commit is contained in:
2020-06-24 21:25:01 +03:00
parent 5d4972145b
commit 4e465ce39f
5 changed files with 46 additions and 16 deletions
+2 -3
View File
@@ -1,7 +1,6 @@
const baseUrl = process.env.NODE_ENV === "development" ? 'http://localhost:8080/v1' : 'https://api.unmei.nix13.pw/v1'; const baseUrl = process.env.NODE_ENV === "development" ? 'http://localhost:8080/v1' : 'https://api.unmei.nix13.pw/v1';
export const response = async (url: string, method: string, body?: string | FormData) => { const response = async (url: string, method: string, body?: string | FormData) => {
let bUrl = baseUrl; let bUrl = baseUrl;
if(baseUrl.endsWith('/')) bUrl = bUrl.slice(0, baseUrl.length-1); if(baseUrl.endsWith('/')) bUrl = bUrl.slice(0, baseUrl.length-1);
if(url.startsWith('/')) url = url.slice(1, baseUrl.length); if(url.startsWith('/')) url = url.slice(1, baseUrl.length);
@@ -40,6 +39,6 @@ const TranslateStatus: { [id: string]: string } = {
}; };
export const getVersion = () => get('version'); export const getVersion = () => get('version');
export const version = '0.9'; export const version = '0.10';
export { get, post, put, del, TranslateStatus }; export { get, post, put, del, TranslateStatus };
+5 -6
View File
@@ -1,8 +1,7 @@
import {get, put} from './api'; import {del, get, put} from './api';
const fetchNews = () => get('news'); export const fetchNews = () => get('news');
const fetchPost = (id: number) => get(`news/${id}`); export const fetchPost = (id: number) => get(`news/${id}`);
const updatePost = (post: PostType) => put(`news/${post.id}`, post); export const updatePost = (post: PostType) => put(`news/${post.id}`, post);
export const deletePost = (id: number) => del(`news/${id}`);
export { fetchNews, fetchPost, updatePost }
+36 -6
View File
@@ -1,7 +1,7 @@
import React, {ChangeEvent, FormEvent} from "react"; import React, {ChangeEvent, FormEvent} from "react";
import Group from "../../../ui/Group/Group"; import Group from "../../../ui/Group/Group";
import NotFoundError from "../../NotFoundError"; import NotFoundError from "../../NotFoundError";
import {fetchPost, updatePost} from "../../../api/news"; import {deletePost, fetchPost, updatePost} from "../../../api/news";
import './APNews.sass'; import './APNews.sass';
import '../../../ui/TextField/TextField.sass' import '../../../ui/TextField/TextField.sass'
import Input from "../../../ui/Input/Input"; import Input from "../../../ui/Input/Input";
@@ -10,10 +10,15 @@ import BBEditor from "../../../ui/BBEditor/BBEditor";
// @ts-ignore // @ts-ignore
import parser from 'bbcode-to-react'; import parser from 'bbcode-to-react';
import Title from "../../../ui/Title/Title"; import Title from "../../../ui/Title/Title";
import {connect} from "react-redux";
import {addNotification, setModal} from "../../../store/actions";
import ConfirmPopout from "../../../ui/ConfirmPopout/ConfirmPopout";
import NotificationMessage from "../../../ui/Notifications/NotificationMessage";
type Props = { type Props = {
postId: number postId: number
//addNotification: (notification: React.ReactNode) => void setPopout: (modal: React.ReactNode | null) => void
addNotification: (notification: React.ReactNode) => void
}; };
type State = { type State = {
@@ -49,8 +54,16 @@ class APModifyPost extends React.Component<Props, State> {
post.short_post = this.shortPost.current.value; post.short_post = this.shortPost.current.value;
post.full_post = this.fullPost.current.value; post.full_post = this.fullPost.current.value;
const { addNotification } = this.props;
await updatePost(post); updatePost(post).then(() => {
const notification = (
<NotificationMessage level={"success"}>
Успешно!
</NotificationMessage>
)
addNotification(notification)
});
} }
componentDidUpdate(prevProps: Readonly<Props>, prevState: Readonly<State>) { componentDidUpdate(prevProps: Readonly<Props>, prevState: Readonly<State>) {
@@ -78,6 +91,18 @@ class APModifyPost extends React.Component<Props, State> {
this.setState({ post }); this.setState({ post });
} }
deletePost = () => {
const { postId, setPopout } = this.props;
const popout = (
<ConfirmPopout onConfirm={() => deletePost(postId)}>
Вы уверены?
</ConfirmPopout>
);
setPopout(popout);
}
render() { render() {
const { post, title, previewShort, previewFull } = this.state; const { post, title, previewShort, previewFull } = this.state;
@@ -108,8 +133,8 @@ class APModifyPost extends React.Component<Props, State> {
</div> </div>
<div> <div>
<Button>Сохранить</Button> <Button style={{ marginRight: '1rem' }}>Сохранить</Button>
<Button type={"button"} style={{ color: 'var(--error) '}}>Удалить</Button> <Button type={"button"} style={{ color: 'var(--error-bg-color)'}} onClick={this.deletePost}>Удалить</Button>
</div> </div>
</form> </form>
</Group> </Group>
@@ -117,4 +142,9 @@ class APModifyPost extends React.Component<Props, State> {
} }
} }
export default APModifyPost; export default connect(null,
dispatch => ({
setPopout: (modal: React.ReactNode | null) => dispatch(setModal(modal)),
addNotification: (notification: React.ReactNode) => dispatch(addNotification(notification))
})
)(APModifyPost);
+2
View File
@@ -2,6 +2,7 @@ import React from "react";
import {fetchNews} from "../../../api/news"; import {fetchNews} from "../../../api/news";
import Group from "../../../ui/Group/Group"; import Group from "../../../ui/Group/Group";
import {Link} from "react-router-dom"; import {Link} from "react-router-dom";
import Button from "../../../ui/Button/Button";
type Props = { type Props = {
path: string path: string
@@ -26,6 +27,7 @@ class APNews extends React.Component<Props, State> {
return ( return (
<Group title={'Новости'}> <Group title={'Новости'}>
<Button>Добавить новость</Button>
{news.map(post => ( {news.map(post => (
<Link to={`${path}/${post.id}`} key={post.id}> <Link to={`${path}/${post.id}`} key={post.id}>
<div style={{ display: "flex" }}> <div style={{ display: "flex" }}>
+1 -1
View File
@@ -55,7 +55,7 @@ class App extends React.Component<Props, State> {
getVersion().then(res => { getVersion().then(res => {
if(version !== res.version) { if(version !== res.version) {
console.debug(`Current back-end version: ${res.version}. Clearing cache`); console.log(`Current back-end version: ${res.version}. Clearing cache`);
caches.keys().then(names => { caches.keys().then(names => {
names.forEach(name => caches.delete(name).catch(console.error)) names.forEach(name => caches.delete(name).catch(console.error))