Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
feat: reorder our repos (#9912)
Browse files Browse the repository at this point in the history
* [fix] overflow issue in repos section

* [ add ] reorder mechanic to UserRepos component

* feat: saves the new order
add: order repo by order

* fix: late return of ordered repos

* [ fix ] tag for React Sortable

* [ remove ] role list since it does not affect reactSortable

* [ clean file ]

* remove: comment line

* fix import

* Remove unnecessary await
Fix adding not shown repo until reload issue

* Update pages/account/manage/repos.js

Definitely! must have losed it while fixing things!

Co-authored-by: Eddie Jaoude <[email protected]>

* [ fix ] reorder repo button showing in public profile
[ fix ] ordered repos on public profile

---------

Co-authored-by: Eddie Jaoude <[email protected]>
  • Loading branch information
Giuliano1993 and eddiejaoude authored Mar 23, 2024
1 parent 739d3f6 commit 2609779
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 9 deletions.
72 changes: 69 additions & 3 deletions components/user/UserRepos.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,39 @@ import StarIcon from "@heroicons/react/20/solid/StarIcon";
import PencilIcon from "@heroicons/react/20/solid/PencilIcon";
import ExclamationCircleIcon from "@heroicons/react/20/solid/ExclamationCircleIcon";
import dateFormat from "@services/utils/dateFormat";
import { ReactSortable } from "react-sortablejs";
import { useState, useEffect } from "react";
import { clientEnv } from "@config/schemas/clientSchema";
import ArrowPathIcon from "@heroicons/react/24/outline/ArrowPathIcon";
import Button from "@components/Button";

export default function UserRepos({ manage = false, confirmDelete, repos }) {

const [reposList, setReposList] = useState(repos || []);
const [reorder, setReorder] = useState(false);
const [reposListPrevious, setReposListPrevious] = useState(repos || []);
const saveOrder = async () => {
const BASE_URL = clientEnv.NEXT_PUBLIC_BASE_URL;
const res = await fetch(`${BASE_URL}/api/account/manage/repos`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(reposList),
});
const updatedRepos = await res.json();
setReposList(updatedRepos);
setReposListPrevious(updatedRepos);
setReorder(false);
};
useEffect(()=>{
setReposList(repos)
},[repos]);


const item = (repo) => (
<div className="relative flex justify-between gap-x-6 px-4 py-5 hover:bg-primary-low dark:hover:bg-primary-medium transition-all duration-100 sm:px-6 lg:px-8">

<div className="flex gap-x-4">
<FallbackImage
className="h-12 w-12 flex-none rounded-full bg-primary-low"
Expand Down Expand Up @@ -81,10 +110,47 @@ export default function UserRepos({ manage = false, confirmDelete, repos }) {
);

return (
<ul role="list" className="divide-y divide-primary-low">
{repos.map((repo) => (
<>
<div className="flex gap-4">
{!reorder && manage && (
<Button
onClick={() => setReorder(true)}
disabled={reposList.length < 2}
>
<ArrowPathIcon className="h-5 w-5 mr-2" />
REORDER
</Button>
)}
{reorder && (
<Button
onClick={() => {
setReorder(false);
setReposList(reposListPrevious);
}}
>
CANCEL
</Button>
)}
{reorder && (
<Button primary={true} onClick={() => saveOrder()}>
SAVE
</Button>
)}
</div>
<ReactSortable
list={reposList}
setList={setReposList}
disabled={!reorder}
tag="ul"
ghostClass="border-2"
chosenClass="border-dashed"
dragClass="border-red-500"
className="divide-y divide-primary-low"
>
{reposList.map((repo) => (
<li key={repo._id}>{manage ? manageDelete(repo) : item(repo)}</li>
))}
</ul>
</ReactSortable>
</>
);
}
1 change: 1 addition & 0 deletions models/Profile/Repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const RepoSchema = new Schema({
min: 2,
max: 256,
},
order: Number,
name: {
type: String,
required: true,
Expand Down
2 changes: 0 additions & 2 deletions pages/account/manage/repos.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export default function ManageRepos({ BASE_URL, repos }) {
body: JSON.stringify({ url }),
});
const updatedRepos = await res.json();

if (updatedRepos.message) {
setIsDisabled(false);
return setShowNotification({
Expand All @@ -72,7 +71,6 @@ export default function ManageRepos({ BASE_URL, repos }) {

const resRepos = await fetch(`${BASE_URL}/api/account/manage/repos`);
const listRepos = await resRepos.json();

setRepoList(listRepos);
setUrl("");
return setShowNotification({
Expand Down
43 changes: 39 additions & 4 deletions pages/api/account/manage/repos.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,51 @@ import Profile from "@models/Profile";
export default async function handler(req, res) {
const session = await getServerSession(req, res, authOptions);

if (req.method !== "GET") {
if(!["GET","PATCH"].includes(req.method)){
return res.status(400).json({ error: "Invalid request: GET required" });
}

const data = await getReposApi(session.username);
const username = session.username;
let data = [];
if(req.method === "GET"){
data = await getReposApi(username);
}
if(req.method === "PATCH"){
data = await updateReposOrderApi(username, req.body);
}

return res.status(200).json(data);
}

export async function updateReposOrderApi(username, data) {
await connectMongo();
const log = logger.child({ username });

const repoList = data.map(async (repo, idx) => {
try {
return Profile.findOneAndUpdate(
{
username,
"repos._id": repo._id,
},
{
$set: {
"repos.$.order": idx,
},
},
);
} catch (e) {
log.error(e, `failed to update repo order for username: ${username}`);
}
});

const repos = await Promise.allSettled(repoList).then(() => {
return getReposApi(username);
})

return JSON.parse(JSON.stringify(repos));
}

export async function getReposApi(username) {
await connectMongo();
const log = logger.child({ username });
Expand All @@ -38,12 +74,11 @@ export async function getReposApi(username) {
},
},
{
$sort: { date: 1 },
$sort: { order: 1 },
},
]);
} catch (e) {
log.error(e, `failed to get repos for username: ${username}`);
}

return JSON.parse(JSON.stringify(getRepos));
}
6 changes: 6 additions & 0 deletions pages/api/profiles/[username]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export async function getUserApi(req, res, username, options = {}) {
sortBy: { date: -1 },
},
},
repos: {
$sortArray: {
input: "$repos",
sortBy: { order: 1 },
},
},
},
},
{
Expand Down

0 comments on commit 2609779

Please sign in to comment.