Skip to content

Commit caee24e

Browse files
committed
feat: allow admins to edit any job posting
- Added role-based permission check in JobListingsTable - Admins (role_id 1 or 4) can now edit and delete any job posting - Non-admins can only edit/delete their own job postings - Shows 'No actions available' for non-editable jobs - Imported useUserStore to check current user role This ensures proper access control while giving admins full management capabilities over all job postings.
1 parent 33ad07d commit caee24e

1 file changed

Lines changed: 36 additions & 22 deletions

File tree

‎apps/codebility/app/home/hire/_components/JobListingsTable.tsx‎

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { JobListing } from "@/app/(marketing)/careers/_types/job-listings";
3434
import EditJobModal from "./EditJobModal";
3535
import { createClientClientComponent } from "@/utils/supabase/client";
3636
import { Skeleton } from "@/components/ui/skeleton/skeleton";
37+
import { useUserStore } from "@/store/codev-store";
3738

3839
interface JobWithApplicationCount extends JobListing {
3940
application_count?: number;
@@ -48,11 +49,15 @@ interface JobWithApplicationCount extends JobListing {
4849
export default function JobListingsTable() {
4950
const router = useRouter();
5051
const { toast } = useToast();
52+
const { user } = useUserStore();
5153
const [jobs, setJobs] = useState<JobWithApplicationCount[]>([]);
5254
const [loading, setLoading] = useState(true);
5355
const [editingJob, setEditingJob] = useState<JobListing | null>(null);
5456
const [isEditModalOpen, setIsEditModalOpen] = useState(false);
5557

58+
// Check if user is admin (role_id 1 or 4)
59+
const isAdmin = user?.role_id === 1 || user?.role_id === 4;
60+
5661
useEffect(() => {
5762
fetchJobListings();
5863
}, []);
@@ -319,28 +324,37 @@ export default function JobListingsTable() {
319324
</Button>
320325
</DropdownMenuTrigger>
321326
<DropdownMenuContent align="end" className="bg-gray-900 border-gray-800">
322-
<DropdownMenuItem
323-
onClick={(e) => {
324-
e.preventDefault();
325-
e.stopPropagation();
326-
handleEdit(job);
327-
}}
328-
className="text-gray-300 hover:text-white hover:bg-gray-800 cursor-pointer"
329-
>
330-
<Edit className="mr-2 h-4 w-4" />
331-
Edit Listing
332-
</DropdownMenuItem>
333-
<DropdownMenuItem
334-
onClick={(e) => {
335-
e.preventDefault();
336-
e.stopPropagation();
337-
handleDelete(job.id);
338-
}}
339-
className="text-red-400 hover:text-red-300 hover:bg-gray-800 cursor-pointer"
340-
>
341-
<Trash2 className="mr-2 h-4 w-4" />
342-
Delete
343-
</DropdownMenuItem>
327+
{(isAdmin || job.created_by === user?.id) && (
328+
<>
329+
<DropdownMenuItem
330+
onClick={(e) => {
331+
e.preventDefault();
332+
e.stopPropagation();
333+
handleEdit(job);
334+
}}
335+
className="text-gray-300 hover:text-white hover:bg-gray-800 cursor-pointer"
336+
>
337+
<Edit className="mr-2 h-4 w-4" />
338+
Edit Listing
339+
</DropdownMenuItem>
340+
<DropdownMenuItem
341+
onClick={(e) => {
342+
e.preventDefault();
343+
e.stopPropagation();
344+
handleDelete(job.id);
345+
}}
346+
className="text-red-400 hover:text-red-300 hover:bg-gray-800 cursor-pointer"
347+
>
348+
<Trash2 className="mr-2 h-4 w-4" />
349+
Delete
350+
</DropdownMenuItem>
351+
</>
352+
)}
353+
{!isAdmin && job.created_by !== user?.id && (
354+
<DropdownMenuItem disabled className="text-gray-500">
355+
No actions available
356+
</DropdownMenuItem>
357+
)}
344358
</DropdownMenuContent>
345359
</DropdownMenu>
346360
</div>

0 commit comments

Comments
 (0)