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

Add the possibility to reorder the repositories #9912

Merged
merged 20 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
cbfb016
[fix] overflow issue in repos section
Giuliano1993 Aug 30, 2023
226f661
Merge branch 'main' of https://github.com/Giuliano1993/BioDrop
Giuliano1993 Sep 2, 2023
fcf0578
Merge branch 'main' of https://github.com/Giuliano1993/BioDrop
Giuliano1993 Nov 29, 2023
f7fe801
Merge branch 'main' of https://github.com/Giuliano1993/BioDrop
Giuliano1993 Dec 4, 2023
a95715e
[ add ] reorder mechanic to UserRepos component
Giuliano1993 Dec 14, 2023
dde3800
feat: saves the new order
Giuliano1993 Dec 14, 2023
865aada
fix: late return of ordered repos
Giuliano1993 Dec 15, 2023
ba0c411
[ fix ] tag for React Sortable
Giuliano1993 Dec 16, 2023
4749426
[ remove ] role list since it does not affect reactSortable
Giuliano1993 Dec 16, 2023
ce9869b
[ clean file ]
Giuliano1993 Dec 16, 2023
a5acb94
Merge branch 'main' into reordering-repos
Giuliano1993 Dec 23, 2023
6082f04
Merge branch 'main' into reordering-repos
eddiejaoude Jan 13, 2024
502463b
remove: comment line
Giuliano1993 Jan 19, 2024
bc28657
Merge branch 'reordering-repos' of https://github.com/Giuliano1993/Bi…
Giuliano1993 Jan 19, 2024
4c33ef6
fix import
Giuliano1993 Jan 19, 2024
a5abaa7
Remove unnecessary await
Giuliano1993 Jan 27, 2024
5eed2e6
Merge branch 'main' into reordering-repos
eddiejaoude Jan 29, 2024
183fbd2
Update pages/account/manage/repos.js
Giuliano1993 Jan 31, 2024
e15d602
[ fix ] reorder repo button showing in public profile
Giuliano1993 Feb 13, 2024
3014897
Merge branch 'main' into reordering-repos
Giuliano1993 Feb 13, 2024
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
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 && (
<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
3 changes: 1 addition & 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,7 @@ export default function ManageRepos({ BASE_URL, repos }) {

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

console.log(listRepos);
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));
}
Loading