Commit a1e3e9ba authored by psl4's avatar psl4
Browse files

Added ajax

parent ab97dd98
......@@ -13,3 +13,4 @@ yarn-error.log
/_ide_helper.php
/.phpstorm.meta.php
/_ide_helper_models.php
/storage/debugbar
This diff is collapsed.
......@@ -5,7 +5,7 @@ namespace App\Http\Controllers;
use App\Article;
use App\Http\Requests\ArticleStoreRequest;
use App\Http\Requests\ArticleUpdateRequest;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class ArticleController extends Controller
{
......@@ -26,6 +26,18 @@ class ArticleController extends Controller
return redirect(route('page::index'));
}
public function createAjax(Request $request)
{
$article = new Article();
$articles = Article::paginate(1);
if ($request->ajax()) {
return view('page.includes._list', compact('article'), compact('articles'));
}
return view('page.article.create_ajax', compact('articles'), compact('article'));
}
public function store(ArticleStoreRequest $request)
{
$article = new Article($request->all());
......@@ -37,6 +49,19 @@ class ArticleController extends Controller
return redirect(route('page::index'));
}
public function storeAjax(ArticleStoreRequest $request) {
$article = new Article($request->all());
$article->idUser = \Auth::user()->id;
$article->save();
$article = new Article();
$articles = Article::paginate(1);
$articles->withPath('/page/article/create/ajax');
return view('page.includes._list', compact('article'), compact('articles'));
}
public function edit($article)
{
if (auth()->check())
......@@ -67,6 +92,27 @@ class ArticleController extends Controller
return redirect(route('page::index'));
}
public function updateAjax(ArticleUpdateRequest $request, $article)
{
if (auth()->check()) {
if (auth()->user()->id == $article->idUser) {
$article_new = new Article($request->all());
$article->Header = $article_new->Header;
$article->Text = $article_new->Text;
$article->Description = $article_new->Description;
$article->save();
}
}
$article = new Article();
$articles = Article::paginate(1);
$articles->withPath('/page/article/create/ajax');
return view('page.includes._list', compact('article'), compact('articles'));
}
public function delete($article)
{
if (auth()->check()) {
......@@ -77,4 +123,20 @@ class ArticleController extends Controller
return redirect(route('page::index'));
}
public function deleteAjax($article)
{
if (auth()->check()) {
if (auth()->user()->id == $article->idUser) {
Article::where('idArticle', $article->idArticle)->delete();
}
}
$article = new Article();
$articles = Article::paginate(1);
$articles->withPath('/page/article/create/ajax');
return view('page.includes._list', compact('article'), compact('articles'));
}
}
......@@ -19,8 +19,11 @@ gulp.task('scss', function() {
gulp.task('js', function () {
return gulp.src('resources/js/**/*.js')
.pipe(webpack())
.pipe(gulp.dest('public/js'))
.pipe(webpack({
output: {
filename: 'public/js/app.js'
}
}));
});
gulp.task('browser-sync', function() {
......
This diff is collapsed.
......@@ -13,9 +13,11 @@
"axios": "^0.19",
"bootstrap": "^4.0.0",
"cross-env": "^7.0",
"gulp-sass": "^4.1.0",
"jquery": "^3.2",
"laravel-mix": "^5.0.1",
"lodash": "^4.17.13",
"node-sass": "^4.14.1",
"popper.js": "^1.12",
"resolve-url-loader": "^2.3.1",
"sass": "^1.20.1",
......@@ -26,7 +28,6 @@
"dependencies": {
"browser-sync": "^2.26.7",
"gulp": "^4.0.2",
"gulp-sass": "^4.1.0",
"gulp-webpack": "^1.5.0",
"laravel-elixir": "^6.0.0-18",
"webpack": "^4.43.0",
......
This diff is collapsed.
This diff is collapsed.
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
(function webpackMissingModule() { throw new Error("Cannot find module \"C:\\Users\\User\\OpenServer\\domains\\unwto\\resources\\scss\\style.scss\""); }());
(function webpackMissingModule() { throw new Error("Cannot find module \"C:\\Users\\User\\OpenServer\\domains\\unwto\\resources\\scss\\_footer.scss\""); }());
(function webpackMissingModule() { throw new Error("Cannot find module \"C:\\Users\\User\\OpenServer\\domains\\unwto\\resources\\scss\\_header.scss\""); }());
/***/ })
/******/ ]);
\ No newline at end of file
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
__webpack_require__(1);
__webpack_require__(2);
(function webpackMissingModule() { throw new Error("Cannot find module \"C:\\Users\\User\\OpenServer\\domains\\unwto\\public\\css\\style.css\""); }());
/***/ }),
/* 1 */
/***/ (function(module, exports) {
/***/ }),
/* 2 */
/***/ (function(module, exports) {
/***/ })
/******/ ]);
\ No newline at end of file
/**
* First we will load all of this project's JavaScript dependencies which
* includes Vue and other libraries. It is a great starting point when
* building robust, powerful web applications using Vue and Laravel.
*/
require('./bootstrap');
window.Vue = require('vue');
/**
* The following block of code may be used to automatically register your
* Vue components. It will recursively scan this directory for the Vue
* components and automatically register them with their "basename".
*
* Eg. ./components/ExampleComponent.vue -> <example-component></example-component>
*/
// const files = require.context('./', true, /\.vue$/i)
// files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default))
Vue.component('example-component', require('./components/ExampleComponent.vue').default);
/**
* Next, we will create a fresh Vue application instance and attach it to
* the page. Then, you may begin adding components to this application
* or customize the JavaScript scaffolding to fit your unique needs.
*/
const app = new Vue({
el: '#app',
});
$(function () {
$('body').on('click', '#btn-save-message', function (event) {
event.preventDefault();
let form = $('#form-create-message');
$.ajax({
url: '/page/article/store/ajax',
data: form.serialize(),
dataType: 'html',
method: 'post'
}).fail(function (data) {
}).done(function (data) {
$('#messages-list').html(data);
});
});
$('body').on('click', '#btn-update-article', function (event) {
event.preventDefault();
let link = $('#form-delete-article').attr('action');
let id = link.match(/[0-9]+/)[0];
let form = $('#form-create-message');
$.ajax({
url: `/page/article/${id}/update/ajax`,
data: form.serialize(),
dataType: 'html',
method: 'put'
}).fail(function (data) {
}).done(function (data) {
$('#messages-list').html(data);
});
});
$('body').on('click', '#btn-delete-article', function (event) {
event.preventDefault();
let link = $('#form-update-article').attr('action');
let id = link.match(/[0-9]+/)[0];
let form = $('#form-create-message');
$.ajax({
url: `/page/article/${id}/delete/ajax`,
data: form.serialize(),
dataType: 'html',
method: 'delete'
}).fail(function (data) {
}).done(function (data) {
$('#messages-list').html(data);
});
});
$('body').on('click', '.page-link', function (event) {
event.preventDefault();
let link = $(this).attr('href');
let id = link.match(/[0-9]+/)[0];
let form = $('#form-create-message');
$.ajax({
url: `/page/article/create/ajax?page=${id}`,
data: form.serialize(),
dataType: 'html',
method: 'get'
}).fail(function (data) {
}).done(function (data) {
$('#messages-list').html(data);
});
});
});
This diff is collapsed.
......@@ -4,6 +4,7 @@
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="{{asset('css/style.css')}}">
<script src="{{asset('js/jquery-3.5.1.min.js')}}"></script>
</head>
<body>
<header>
......
......@@ -9,39 +9,39 @@
</head>
<body>
<div class="container">
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if (Session::has('result'))
<div class="alert alert-success">
{{ Session::get('result') }}
</div>
@endif
@if (Session::has('result'))
<div class="alert alert-success">
{{ Session::get('result') }}
</div>
@endif
{{Form::model($article, ['route' => ['page::article::store']])}}
<h3>Title</h3>
<div class="form-group">
{{Form::text('Header', null, ['class' => 'form-control'])}}
</div>
<h3>Description</h3>
<div class="form-group">
{{Form::text('Description', null, ['class' => 'form-control'])}}
</div>
<h3>Text</h3>
<div class="form-group">
{{Form::textarea('Text', null, ['class' => 'form-control'])}}
</div>
<div class="form-group">
{{Form::submit('Сохранить', ['class' => 'btn btn-primary'])}}
</div>
{{Form::close()}}
{{Form::model($article, ['route' => ['page::article::store']])}}
<h3>Title</h3>
<div class="form-group">
{{Form::text('Header', null, ['class' => 'form-control'])}}
</div>
<h3>Description</h3>
<div class="form-group">
{{Form::text('Description', null, ['class' => 'form-control'])}}
</div>
<h3>Text</h3>
<div class="form-group">
{{Form::textarea('Text', null, ['class' => 'form-control'])}}
</div>
<div class="form-group">
{{Form::submit('Save', ['class' => 'btn btn-primary'])}}
</div>
{{Form::close()}}
</div>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>Document</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>
<body>
<div class="container">
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if (Session::has('result'))
<div class="alert alert-success">
{{ Session::get('result') }}
</div>
@endif
{{Form::model($article, ['id' => 'form-create-message', 'route' => ['page::article::store.ajax']])}}
<h3>Title</h3>
<div class="form-group">
{{Form::text('Header', null, ['class' => 'form-control'])}}
</div>
<h3>Description</h3>
<div class="form-group">
{{Form::text('Description', null, ['class' => 'form-control'])}}
</div>
<h3>Text</h3>
<div class="form-group">
{{Form::textarea('Text', null, ['class' => 'form-control'])}}
</div>
<div class="form-group">
{{Form::submit('Create', ['id' => 'btn-save-message', 'class' => 'btn btn-primary'])}}
</div>
{{Form::close()}}
<div id="messages-list" class="col-lg-12">
@include('page.includes._list')
</div>
<script src="{{ asset('js/app.js') }}"></script>
</div>
</body>
</html>
......@@ -6,6 +6,7 @@
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>Document</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<script src="{{asset('js/app.js')}}"></script>
</head>
<body>
<div class="container">
......@@ -26,7 +27,6 @@
@endif
{{Form::model($article, ['route' => ['page::article::update', $article], 'method' => 'PUT'])}}
<p>{{ $article->idArticle }}</p>
<h3>Title</h3>
<div class="form-group">
{{Form::text('Header', null, ['class' => 'form-control'])}}
......@@ -40,9 +40,10 @@
{{Form::textarea('Text', null, ['class' => 'form-control'])}}
</div>
<div class="form-group">
{{Form::submit('Сохранить', ['class' => 'btn btn-primary'])}}
{{Form::submit('Save', ['id' => 'btn-save-message', 'class' => 'btn btn-primary'])}}
</div>
{{Form::close()}}
</div>
</body>
</html>
{{ $articles->links() }}
@foreach ($articles as $article)
<div class="col-lg-12">
<div class="panel panel-default">
<h2>{{ $article->Header }}</h2>
<div class="panel-heading text-right">
<div class="panel-title">
<small>Author: <b>{{ DB::table('users')->where('id', $article->idUser)->first()->name }}</b></small>
<br>
<p>Created at: <i>{{$article->created_at}}</i></p>
</div>
</div>
<hr>
<div class="panel-body">
<p><i>{{ $article->Description }}</i></p>
<p>{{ $article->Text }}</p>
{{ Form::open(['id' => 'form-update-article', 'url' => '/page/article/' . $article->idArticle . '/update/', 'method' => 'put']) }}
{{Form::submit('Update', ['id' => 'btn-update-article', 'class' => 'btn btn-primary'])}}
{{ Form::close() }}
<br>
{{ Form::open(['id' => 'form-delete-article', 'url' => '/page/article/' . $article->idArticle . '/delete/', 'method' => 'delete']) }}
{{Form::submit('Delete', ['id' => 'btn-delete-article', 'class' => 'btn btn-primary'])}}
{{ Form::close() }}
<br>
</div>
</div>
</div>
@endforeach
......@@ -42,6 +42,7 @@
</div>
@endforeach
{{ $articles->links() }}
</div>
</body>
</html>
......@@ -81,12 +81,24 @@ Route::group(['prefix' => 'page', 'as' => 'page::'], function () {
'middleware' => 'auth'
]);
Route::get('create/ajax', [
'as' => 'create.ajax',
'uses' => 'ArticleController@createAjax',
'middleware' => 'auth'
]);
Route::post('store', [
'as' => 'store',
'uses' => 'ArticleController@store',
'middleware' => 'auth'
]);
Route::post('store/ajax', [
'as' => 'store.ajax',
'uses' => 'ArticleController@storeAjax',
'middleware' => 'auth'
]);
Route::get('{article}/edit', [
'as' => 'edit',
'uses' => 'ArticleController@edit',
......@@ -99,10 +111,22 @@ Route::group(['prefix' => 'page', 'as' => 'page::'], function () {
'middleware' => 'article.editor'
]);
Route::put('{article}/update/ajax', [
'as' => 'update',