Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions app/Http/Controllers/ComputerScienceResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@

use App\Http\Requests\ComputerScienceResource\StoreResourceRequest;
use App\Models\ComputerScienceResource;
use App\Models\ResourceEdits;
use App\Models\ResourceReview;
use App\Services\CommentService;
use App\Services\UpvoteService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Inertia\Inertia;

class ComputerScienceResourceController extends Controller
{
protected $commentService;
protected $upvoteService;

function __construct(CommentService $commentService, UpvoteService $upvoteService)
{
$this->commentService = $commentService;
$this->upvoteService = $upvoteService;
}

/**
* Display a listing of the resource.
*/
Expand Down Expand Up @@ -54,7 +66,7 @@ public function store(StoreResourceRequest $request)

// Add topics as tags
$resource->topic_tags = $validatedData['topic_tags'];

// Add programming languages as tags (if provided)
if (isset($validatedData['programming_language_tags'])) {
$resource->programming_language_tags = $validatedData['programming_language_tags'];
Expand All @@ -67,51 +79,59 @@ public function store(StoreResourceRequest $request)

Log::debug("Created resource " . json_encode($resource));

return redirect(route('resources.show', ['computerScienceResource'=>$resource->id]))
return redirect(route('resources.show', ['computerScienceResource' => $resource->id]))
->with('success', 'Created Resource Succesfully!');
}

/**
* Display the specified resource.
*/
public function show(Request $request, CommentService $commentService, ComputerScienceResource $computerScienceResource, string $tab = 'reviews')
public function show(Request $request, ComputerScienceResource $computerScienceResource, string $tab = 'reviews')
{
$validTabs = ['reviews', 'discussion', 'edits'];

if (!in_array($tab, $validTabs)) {
// Redirect to default if invalid
return redirect()->route('resources.show', [
'computerScienceResource' => $computerScienceResource->id,
'tab' => 'reviews',
]);
}

// return the resource and tab
$data = [
'tab' => $tab,
'resource' => $computerScienceResource,
];


$sortBy = $request->query('sort_by', 'top');
// Load only the necessary tab data
if ($tab === 'reviews') {
$data['reviews'] = Inertia::defer(fn () =>
$computerScienceResource->reviews()->orderByDesc('created_at')->get()
$data['reviews'] = Inertia::defer(
function () use ($computerScienceResource, $sortBy) {
$query = ResourceReview::where('computer_science_resource_id', $computerScienceResource->id);
$query = $this->upvoteService->applySort($query, $sortBy, ResourceReview::class);
return $query->get();
}
);
} elseif ($tab === 'edits') {
$data['resourceEdits'] = Inertia::defer(fn () =>
$computerScienceResource->edits
$data['resourceEdits'] = Inertia::defer(
function () use ($computerScienceResource, $sortBy) {
$query = ResourceEdits::where('computer_science_resource_id', $computerScienceResource->id);
$query = $this->upvoteService->applySort($query, $sortBy, ResourceEdits::class);
return $query->get();
}
);
} elseif ($tab === 'discussion') {
$sortBy = $request->query('sort_by', 'top');
$data['discussion'] = Inertia::defer(fn () =>
$commentService->getPaginatedComments('resource', $computerScienceResource->id, 0, 150, $sortBy)
$data['discussion'] = Inertia::defer(
fn() =>
$this->commentService->getPaginatedComments('resource', $computerScienceResource->id, 0, 150, $sortBy)
);
$data['discussionSortByValue'] = $sortBy;
}

return Inertia::render('Resources/Show', $data);
}


/**
* Remove the specified resource from storage.
Expand Down
10 changes: 4 additions & 6 deletions app/Services/CommentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function __construct(ModelResolverService $resolver)
* @return array
*/
// TODO: Refactor commentable types, and commentable types short for all other objects
public function getPaginatedComments(string $commentableTypeShort, int $commentableId, int $index, int $paginationLimit = -1, string $sortBy = 'top'): array
public function getPaginatedComments(string $commentableTypeShorthand, int $commentableId, int $index, int $paginationLimit = -1, string $sortBy = 'top'): array
{
if ($paginationLimit == -1)
{
Expand All @@ -37,17 +37,15 @@ public function getPaginatedComments(string $commentableTypeShort, int $commenta

Validator::make([
'index' => $index,
'commentable_type_short' => $commentableTypeShort,
'commentable_type_short' => $commentableTypeShorthand,
'pagination_limit' => $paginationLimit,
'sort_by' => $sortBy,
], [
'index' => ['required', 'integer', 'min:0'],
'commentable_type_short' => ['required', Rule::in(config('comment.commentable_types_shorthand'))],
'pagination_limit' => ['required', 'integer', 'max:' . config('comment.pagination_limit')],
'sort_by' => ['required', 'string', Rule::in(config('comment.sortable_options'))],
])->validate();

$commentableType = $this->modelResolver->getModelClass($commentableTypeShort);
$commentableType = $this->modelResolver->getModelClass($commentableTypeShorthand);
Log::debug("Request is, commentable_type: {$commentableType}. id: {$commentableId}. index: {$index}");

// Get the root comments:
Expand All @@ -58,7 +56,7 @@ public function getPaginatedComments(string $commentableTypeShort, int $commenta
]);

// Apply sorting on the comments
app(UpvoteService::class)->applySort($query, $sortBy, Comment::class);
$query = app(UpvoteService::class)->applySort($query, $sortBy, Comment::class);

$rootComments = $query->get();
Log::debug("Root comments: " . json_encode($rootComments));
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Components/Comments/Commentable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ref, provide, readonly, nextTick, onMounted, reactive } from "vue";
import axios from "axios";
import CommentActionsForm from "@/Components/Comments/CommentActionsForm.vue";
import SortByDropdown from "@/Components/Comments/SortByDropdown.vue";
import SortByDropdown from "@/Components/Comments/SortUpvotesByDropdown.vue";
import CommentList from "./CommentList.vue";

const props = defineProps({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup>
import { router } from '@inertiajs/vue3';
import { defineProps } from 'vue';
import SortByDropdown from "@/Components/Comments/SortByDropdown.vue";
import SortByDropdown from "@/Components/Comments/SortUpvotesByDropdown.vue";

const props = defineProps({
resourceId: {
Expand All @@ -10,16 +10,19 @@ const props = defineProps({
},
initialValue: {
type: String,
required: true
default: 'top',
},
tab: {
type: String,
default: 'discussion',
}
})

function handleSortChange(newSortType) {
if (props.initialValue == newSortType) return;
// Change the sort_by parameter
const baseUrl = route('resources.show', {
computerScienceResource: props.resourceId,
tab: 'discussion',
tab: props.tab,
});

// Create a new URL object based on the current location
Expand All @@ -39,5 +42,6 @@ function handleSortChange(newSortType) {
<template>
<SortByDropdown
@change="handleSortChange"
:initial-value="initialValue"
></SortByDropdown>
</template>
20 changes: 10 additions & 10 deletions resources/js/Pages/Resources/Show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import ResourceReviews from "@/Components/Resources/Reviews/ResourceReviews.vue";
import Commentable from "@/Components/Comments/Commentable.vue";
import ResourceEdits from "@/Components/Resources/ResourceEdit/ResourceEdits.vue";
import DiscussionSorting from "@/Components/Resources/Discussion/DiscussionSorting.vue";
import ResourceUpvoteSorting from "@/Components/Resources/ResourceUpvoteSorting.vue";
import { getConfigData } from "@/Helpers/config";

const props = defineProps({
Expand All @@ -27,10 +27,6 @@ const props = defineProps({
type: Object,
required: false,
},
discussionSortByValue: {
type: String,
required: false,
},
reviews: {
type: Array,
required: false,
Expand All @@ -48,6 +44,9 @@ const tabs = [
{ label: "Discussion", value: "discussion" },
{ label: "Proposed Edits", value: "edits" },
];

const urlParams = new URLSearchParams(window.location.search);
const sortingType = urlParams.get('sort_by') || 'top';
</script>

<template>
Expand Down Expand Up @@ -220,6 +219,12 @@ const tabs = [

<!-- Tab Panels -->
<div class="px-6 pb-6">
<ResourceUpvoteSorting
:resource-id="props.resource.id"
:initial-value="sortingType"
:tab="props.tab"
></ResourceUpvoteSorting>

<div v-if="props.tab === 'reviews'">
<Deferred data="reviews">
<template #fallback>
Expand All @@ -233,11 +238,6 @@ const tabs = [
</div>

<div v-else-if="props.tab === 'discussion'">
<DiscussionSorting
:resource-id="props.resource.id"
:initial-value="props.discussionSortByValue"
></DiscussionSorting>

<Deferred data="discussion">
<template #fallback>
<div>Loading...</div>
Expand Down
Loading