Skip to content

Commit

Permalink
Merge branch 'main' into feature/frontend/cardstab-actionbar
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian702 committed Feb 27, 2024
2 parents 9ba1ad3 + 8c5cb42 commit 1cf4458
Show file tree
Hide file tree
Showing 34 changed files with 775 additions and 834 deletions.
15 changes: 15 additions & 0 deletions backend/pkg/comparators/modelComparators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package comparators

import "github.com/kioku-project/kioku/pkg/model"

func CardModelDateComparator(a, b model.Card) int {
return a.CreatedAt.Compare(b.CreatedAt)
}

func DeckModelDateComparator(a, b model.Deck) int {
return a.CreatedAt.Compare(b.CreatedAt)
}

func GroupModelDateComparator(a, b model.Group) int {
return a.CreatedAt.Compare(b.CreatedAt)
}
17 changes: 17 additions & 0 deletions backend/pkg/comparators/protoComparators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package comparators

import (
pbCommon "github.com/kioku-project/kioku/pkg/proto"
"strings"
)

func GroupUserProtoRoleComparator(a, b *pbCommon.User) int {
if val := int(b.GroupRole.Number()) - int(a.GroupRole.Number()); val != 0 {
return val
}
return strings.Compare(strings.ToLower(a.UserName), strings.ToLower(b.UserName))
}

func UserProtoNameComparator(a, b *pbCommon.User) int {
return strings.Compare(strings.ToLower(a.UserName), strings.ToLower(b.UserName))
}
10 changes: 6 additions & 4 deletions backend/pkg/model/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
import (
"github.com/kioku-project/kioku/pkg/helper"
"gorm.io/gorm"
"time"
)

type GroupType string
Expand All @@ -14,10 +15,11 @@ const (
)

type Group struct {
ID string `gorm:"primaryKey"`
Name string `gorm:"not null"`
Description string `gorm:"not null"`
IsDefault bool `gorm:"not null"`
ID string `gorm:"primaryKey"`
Name string `gorm:"not null"`
Description string `gorm:"not null"`
IsDefault bool `gorm:"not null"`
CreatedAt time.Time
GroupType GroupType `gorm:"not null"`
Decks []Deck `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
GroupUserRoles []GroupUserRole `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Expand Down
10 changes: 3 additions & 7 deletions backend/services/carddeck/handler/carddeck.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package handler
import (
"context"
"errors"

"github.com/kioku-project/kioku/pkg/comparators"
pbCommon "github.com/kioku-project/kioku/pkg/proto"
pbCardDeck "github.com/kioku-project/kioku/services/carddeck/proto"
"gorm.io/gorm"
Expand Down Expand Up @@ -173,10 +173,6 @@ func (e *CardDeck) updateCardReferences(ctx context.Context, cardSideToDelete *m
return isLastCardSide, nil
}

func cardModelDateComparator(a, b model.Card) int {
return a.CreatedAt.Compare(b.CreatedAt)
}

func (e *CardDeck) copyCards(ctx context.Context, cards []*model.Card, deckID string) error {
for _, card := range cards {
newCard := &model.Card{
Expand Down Expand Up @@ -219,7 +215,7 @@ func (e *CardDeck) GetGroupDecks(ctx context.Context, req *pbCommon.GroupRequest
return err
}
}

slices.SortFunc(decks, comparators.DeckModelDateComparator)
rsp.Decks = converter.ConvertToTypeArray(decks, converter.StoreDeckToProtoDeckConverter)
for _, deck := range rsp.Decks {
deckRole, err := e.collaborationService.GetGroupUserRole(ctx, &pbCommon.GroupRequest{
Expand Down Expand Up @@ -393,7 +389,7 @@ func (e *CardDeck) GetDeckCards(ctx context.Context, req *pbCommon.DeckRequest,
if err := e.checkUserDeckAccess(ctx, req.UserID, deck.ID); err != nil {
return err
}
slices.SortFunc(deck.Cards, cardModelDateComparator)
slices.SortFunc(deck.Cards, comparators.CardModelDateComparator)
rsp.Cards = make([]*pbCommon.Card, len(deck.Cards))
for i, card := range deck.Cards {
cardSides, err := e.store.FindCardSidesByCardID(ctx, card.ID)
Expand Down
10 changes: 8 additions & 2 deletions backend/services/collaboration/handler/collaboration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package handler
import (
"context"
"errors"
"github.com/kioku-project/kioku/pkg/comparators"
"golang.org/x/exp/slices"

"github.com/kioku-project/kioku/pkg/converter"
"github.com/kioku-project/kioku/pkg/helper"
Expand Down Expand Up @@ -89,6 +91,7 @@ func (e *Collaboration) generateGroupMemberAdmissionResponse(
UserEmail: user.UserEmail,
}
}
slices.SortFunc(memberAdmissions, comparators.UserProtoNameComparator)
logger.Infof("Successfully received user information from %d users and added it to request information",
len(users.Users))
return
Expand Down Expand Up @@ -153,6 +156,7 @@ func (e *Collaboration) GetUserGroups(ctx context.Context, req *pbCommon.User, r
if err != nil {
return err
}
slices.SortFunc(groups, comparators.GroupModelDateComparator)
protoGroups := converter.ConvertToTypeArray(groups, converter.StoreGroupToProtoGroupConverter)
protoRoles := make([]pbCommon.GroupRole, len(protoGroups))
for index, group := range protoGroups {
Expand Down Expand Up @@ -307,14 +311,16 @@ func (e *Collaboration) GetGroupMembers(ctx context.Context, req *pbCommon.Group
if err != nil {
return err
}
rsp.Users = make([]*pbCommon.User, len(users.Users))
groupUsers := make([]*pbCommon.User, len(users.Users))
for i, user := range users.Users {
rsp.Users[i] = &pbCommon.User{
groupUsers[i] = &pbCommon.User{
UserID: user.UserID,
UserName: user.UserName,
GroupRole: converter.MigrateModelRoleToProtoRole(groupMembers[i].RoleType),
}
}
slices.SortFunc(groupUsers, comparators.GroupUserProtoRoleComparator)
rsp.Users = groupUsers
logger.Infof("Found %d users in group with id %s", len(rsp.Users), req.Group.GroupID)
return nil
}
Expand Down
1 change: 1 addition & 0 deletions backend/store/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ func (s *CollaborationStoreImpl) ModifyGroup(ctx context.Context, group *model.G
Name: group.Name,
Description: group.Description,
IsDefault: group.IsDefault,
CreatedAt: group.CreatedAt,
GroupType: group.GroupType,
}).Error
return
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
env_file:
- ./backend/.env
healthcheck:
test: ["CMD-SHELL", "pg_isready -U kioku"]
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
interval: 5s
timeout: 5s
retries: 5
Expand Down
40 changes: 11 additions & 29 deletions frontend/components/deck/Deck.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import Link from "next/link";
import { useRouter } from "next/router";
import { useEffect, useMemo, useState } from "react";
import { Globe, Heart, Lock, MoreVertical } from "react-feather";
import "react-toastify/dist/ReactToastify.css";
import { preload, useSWRConfig } from "swr";
import { preload } from "swr";

import { Text } from "@/components/Text";
import { IconLabel, IconLabelType } from "@/components/graphics/IconLabel";
import { Button } from "@/components/input/Button";
import { Deck as DeckType } from "@/types/Deck";
import { deleteRequest, postRequest } from "@/util/api";
import { fetcher, useDueCards } from "@/util/swr";
import { toggleFavorite } from "@/util/api";
import { deckRoute } from "@/util/endpoints";
import { fetcher, useDeckDueCards } from "@/util/swr";

interface DeckProps {
/**
Expand All @@ -35,12 +35,12 @@ interface DeckProps {
export const FetchDeck = ({ deck, ...props }: DeckProps) => {
const router = useRouter();

const { dueCards } = useDueCards(deck.deckID);
const { dueCards } = useDeckDueCards(deck.deckID);

useEffect(() => {
if (deck) {
router.prefetch(`/deck/${deck.deckID}`);
preload(`/api/decks/${deck.deckID}`, fetcher);
preload(deckRoute(deck.deckID), fetcher);
}
}, [router, deck]);

Expand All @@ -60,8 +60,6 @@ export const Deck = ({
deckNotification,
className = "",
}: DeckProps) => {
const { mutate } = useSWRConfig();

const [isFavorite, setFavorite] = useState(deck.isFavorite);

useEffect(() => {
Expand Down Expand Up @@ -123,7 +121,11 @@ export const Deck = ({
}
className="relative hover:scale-105"
onClick={(event) => {
modifyFavorite(deck);
toggleFavorite(
deck.deckID,
deck.groupID,
isFavorite
);
event.preventDefault();
}}
/>
Expand Down Expand Up @@ -196,24 +198,4 @@ export const Deck = ({
)}
</Link>
);

async function modifyFavorite(deck: DeckType) {
const response = isFavorite
? await deleteRequest(
"/api/decks/favorites",
JSON.stringify({
deckID: deck.deckID,
})
)
: await postRequest(
"/api/decks/favorites",
JSON.stringify({
deckID: deck.deckID,
})
);
setFavorite((prev) => !prev);
mutate(`/api/groups/${deck.groupID}/decks`);
mutate("/api/decks/favorites");
mutate("/api/decks/active");
}
};
35 changes: 8 additions & 27 deletions frontend/components/flashcard/Card.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { useLingui } from "@lingui/react";
import clsx from "clsx";
import React, { useState } from "react";
import React, { useRef, useState } from "react";
import { Check, Trash, X } from "react-feather";
import { toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import { useSWRConfig } from "swr";

import { Text } from "@/components/Text";
import { Button } from "@/components/input/Button";
import { Card as CardType } from "@/types/Card";
import { deleteRequest } from "@/util/api";
import { deleteCard } from "@/util/api";

interface CardProps {
/**
Expand Down Expand Up @@ -38,8 +37,10 @@ export const Card = ({
className = "",
setCard,
}: CardProps) => {
const { mutate } = useSWRConfig();
const [isDelete, setDelete] = useState(false);
const cardNameInput = useRef<HTMLInputElement>(null);

const { _ } = useLingui();

return (
<button
Expand All @@ -63,7 +64,8 @@ export const Card = ({
buttonSize=""
buttonIcon={<Check />}
onClick={(event) => {
deleteCard();
card.deckID &&
deleteCard(card.deckID, card.cardID);
event.stopPropagation();
}}
/>
Expand Down Expand Up @@ -91,28 +93,7 @@ export const Card = ({
}}
/>
)}
{/* <Edit2
className="cursor-pointer"
size={20}
onClick={() => {
if (setCard) {
setCard(card);
}
}}
/> */}
</div>
</button>
);

async function deleteCard() {
const response = await deleteRequest(`/api/cards/${card.cardID}`);
if (response?.ok) {
toast.info("Card deleted!", { toastId: "deletedCardToast" });
mutate(`/api/decks/${card.deckID}/cards`);
mutate(`/api/decks/${card.deckID}/pull`);
mutate(`/api/decks/${card.deckID}/dueCards`);
} else {
toast.error("Error!", { toastId: "deletedCardToast" });
}
}
};
Loading

0 comments on commit 1cf4458

Please sign in to comment.