Skip to content

Commit

Permalink
added friend icon to dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
mialana committed Oct 11, 2023
1 parent 2d5f56b commit 55fc7f1
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 18 deletions.
7 changes: 1 addition & 6 deletions frontend/plan/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,7 @@ export const setPrimaryScheduleIdOnFrontend = (scheduleId) => ({
export const checkForDefaultSchedules = (schedulesFromBackend) => (
dispatch
) => {
if (
!schedulesFromBackend.reduce(
(acc, { name }) => acc || name === "cart",
false
)
) {
if (!schedulesFromBackend.find((acc, { name }) => acc || name === "cart")) {
dispatch(createScheduleOnBackend("cart"));
}
// if the user doesn't have an initial schedule, create it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const AddScheduleFriendsModalInterior = ({
const [errorObj, setErrorObj] = useState({ message: "", error: false });

useEffect(() => {
console.log(existingData);
validateInput(
user,
userInput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { Icon } from "../bulma_derived_components";
const InitialIcon = styled.div<{ color: Color }>`
color: white;
background-color: ${(props) => props.color};
opacity: 65%;
justify-content: center;
align-items: center;
text-align: center;
justify-content: center;
border-radius: 50%;
font-weight: 600;
font-size: 1vw;
margin-right: 1rem;
padding: 0.25rem;
Expand All @@ -23,7 +25,7 @@ const InitialIcon = styled.div<{ color: Color }>`

const FriendRequestContainer = styled.div`
display: grid;
grid-template-columns: 10% 57% 23% 10%;
grid-template-columns: 15% 52% 23% 10%;
height: 100%
width: 100%;
height: 2.5vw;
Expand Down
85 changes: 80 additions & 5 deletions frontend/plan/components/schedule/ScheduleSelectorDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import React, { useState, useEffect, useRef } from "react";
import styled from "styled-components";
import { Icon } from "../bulma_derived_components";
import { User, Schedule as ScheduleType, FriendshipState } from "../../types";
import { User, Schedule, Color as FriendshipState, ScheduleType, Color } from "../../types";
import { nextAvailable } from "../../reducers/schedule";

const ButtonContainer = styled.div<{ isActive: boolean; isPrimary?: boolean }>`
line-height: 1.5;
position: relative;
border-radius: 0 !important;
cursor: pointer;
padding: 0.5rem 0.5rem 0.5rem 1rem;
padding: 0.5rem 0.5rem 0.5rem 0.75rem;
transition: background 0.1s ease;
display: flex;
flex-direction: row;
justify-content: space-between;
background: ${(props) => (props.isActive ? "#F5F6F8" : "#FFF")};
align-items: center;
&:hover {
background: #ebedf1;
Expand All @@ -40,35 +41,64 @@ const ButtonContainer = styled.div<{ isActive: boolean; isPrimary?: boolean }>`
.option-icon i.primary:hover {
color: ${(props) =>
props.isPrimary ? "#295FCE" : "#7E7E7E"}; !important
props.isPrimary ? "#295FCE" : "#7E7E7E"}; !important;
}
.initial-icon {
color: white; !important;
font-size: 1vw;
}
`;

const ButtonLabelContainer = styled.div<{ width: number }>`
display: flex;
flex-grow: 1;
flex-grow: 0.5;
font-weight: 400;
justify-content: start;
max-width: ${(props) => props.width}%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
`;

const InitialIcon = styled.div<{ color: Color }>`
color: white;
background-color: ${(props) => props.color};
opacity: 65%;
justify-content: center;
align-items: center;
text-align: center;
justify-content: center;
border-radius: 50%;
font-weight: 600;
font-size: 1vw;
display: inline-flex;
height: 1.75vw;
max-width: 1.75vw;
min-width: 1.75vw;
`;

interface FriendButtonProps {
friendName: string;
isActive: boolean;
display: () => void;
removeFriend: () => void;
color: Color
}

const FriendButton = ({
friendName,
isActive,
display,
removeFriend,
color
}: FriendButtonProps) => (
<ButtonContainer isActive={isActive} onClick={display}>
<ButtonLabelContainer width={75}>{friendName}</ButtonLabelContainer>
<InitialIcon color={color}>
<div className="initial-icon">{friendName.split(" ")[0][0]}</div>
</InitialIcon>
<ButtonLabelContainer width={65}>{friendName}</ButtonLabelContainer>
<Icon
onClick={(e) => {
removeFriend();
Expand Down Expand Up @@ -382,6 +412,50 @@ const ScheduleSelectorDropdown = ({
let hasFriends = friendshipState.acceptedFriends.length != 0;
let numRequests = friendshipState.requestsReceived.length;

// Used for box coloring, from StackOverflow:
// https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript
const hashString = (s: string) => {
let hash = 0;
if (!s || s.length === 0) return hash;
for (let i = 0; i < s.length; i += 1) {
const chr = s.charCodeAt(i);
hash = (hash << 5) - hash + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};

// step 2 in the CIS121 review: hashing with linear probing.
// hash every section to a color, but if that color is taken, try the next color in the
// colors array. Only start reusing colors when all the colors are used.
const getColor = (() => {
const colors = [
Color.BLUE,
Color.RED,
Color.AQUA,
Color.ORANGE,
Color.GREEN,
Color.PINK,
Color.SEA,
Color.INDIGO,
];
// some CIS120: `used` is a *closure* storing the colors currently in the schedule
let used: Color[] = [];
return (c: string) => {
if (used.length === colors.length) {
// if we've used all the colors, it's acceptable to start reusing colors.
used = [];
}
let i = Math.abs(hashString(c));
while (used.indexOf(colors[i % colors.length]) !== -1) {
i += 1;
}
const color = colors[i % colors.length];
used.push(color);
return color;
};
})();

useEffect(() => {
const listener = (event: Event) => {
if (
Expand Down Expand Up @@ -496,6 +570,7 @@ const ScheduleSelectorDropdown = ({
friendshipState.activeFriend.username ===
friend.username
}
color={getColor(friend.username)}
/>
))}
<AddNew onClick={addFriend} role="button" href="#">
Expand Down
10 changes: 5 additions & 5 deletions frontend/plan/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ const StyledToast = styled(ToastContainer)`
`;

let middlewares = [thunkMiddleware, analyticsMiddleware];
// if (process.env.NODE_ENV === "development") {
// // eslint-disable-next-line
// const { logger: loggerMiddleware } = require("redux-logger");
// middlewares = [thunkMiddleware, loggerMiddleware, analyticsMiddleware];
// }
if (process.env.NODE_ENV === "development") {
// eslint-disable-next-line
const { logger: loggerMiddleware } = require("redux-logger");
middlewares = [thunkMiddleware, loggerMiddleware, analyticsMiddleware];
}

export function showToast(text: string, error: boolean) {
if (error) {
Expand Down

0 comments on commit 55fc7f1

Please sign in to comment.