Skip to content

Commit

Permalink
Merge pull request #280 from cabcookie:add-manager-employee-relation
Browse files Browse the repository at this point in the history
Chef/Mitarbeiter Beziehung
  • Loading branch information
cabcookie authored Dec 20, 2024
2 parents 65af7c3 + 7a44707 commit 4b26af3
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 119 deletions.
2 changes: 2 additions & 0 deletions amplify/data/person-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const personSchmema = {
"partner",
"friend",
"smallgroup",
"manager",
"employer",
]),
MeetingParticipant: a
.model({
Expand Down
8 changes: 4 additions & 4 deletions api/useAccountPeople.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { type Schema } from "@/amplify/data/resource";
import { generateClient, SelectionSet } from "aws-amplify/data";
import { filter, flow, identity, map, sortBy, uniq } from "lodash/fp";
import useSWR from "swr";
import { handleApiErrors } from "./globals";
import {
getLatestUpdate,
isCurrentRole,
personName,
} from "@/helpers/account-people";
import { generateClient, SelectionSet } from "aws-amplify/data";
import { filter, flow, identity, map, sortBy, uniq } from "lodash/fp";
import useSWR from "swr";
import { handleApiErrors } from "./globals";
const client = generateClient<Schema>();

const selectionSet = [
Expand Down
25 changes: 9 additions & 16 deletions api/usePerson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,8 @@ const usePerson = (personId?: string) => {
if (id === "NEW")
return createRelationship({ relatedPerson, nameOfRelationship });

const updRelation = (() => {
const relationship = person.relationships.find((r) => r.id === id);
return !relationship
? undefined
: ({ ...relationship } as PersonRelationship);
})();
const updRelation = person.relationships.find((r) => r.id === id);

if (!updRelation) return;
const relationType = getRelationType(
updRelation.direction,
Expand All @@ -253,10 +249,7 @@ const usePerson = (personId?: string) => {
...(endDate && { endDate }),
...(nameOfAnniversary && { nameOfAnniversary }),
...(relationType && {
nameOfRelationship:
updRelation.direction === "from"
? relationType.name
: relationType.nameOfOtherDirection,
nameOfRelationship: relationType.name,
}),
...(relatedPerson && { relatedPerson }),
});
Expand All @@ -269,7 +262,7 @@ const usePerson = (personId?: string) => {
} as Person;
mutate(updPerson, false);

const { data, errors } = await client.models.PersonRelationship.update({
const updObj = {
id,
personId:
updRelation.direction === "from" ? person.id : relatedPerson?.id,
Expand All @@ -278,14 +271,14 @@ const usePerson = (personId?: string) => {
...(!relationType
? {}
: {
typeName:
updRelation.direction === "from"
? relationType.name
: relationType.nameOfOtherDirection,
typeName: relationType.name,
}),
...(!anniversary ? {} : { date: toISODateString(anniversary) }),
...(!endDate ? {} : { endDate: toISODateString(endDate) }),
});
};

const { data, errors } =
await client.models.PersonRelationship.update(updObj);
if (errors) handleApiErrors(errors, "Updating person relation failed");
mutate(updPerson);
return data?.id;
Expand Down
13 changes: 12 additions & 1 deletion api/usePersonActivities.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { type Schema } from "@/amplify/data/resource";
import { generateClient, SelectionSet } from "aws-amplify/data";
import { flatMap, flow, map, sortBy, union, uniqBy } from "lodash/fp";
import {
filter,
flatMap,
flow,
identity,
map,
sortBy,
union,
uniqBy,
} from "lodash/fp";
import useSWR from "swr";
import { handleApiErrors } from "./globals";
const client = generateClient<Schema>();
Expand Down Expand Up @@ -78,6 +87,8 @@ const fetchMentionedPersonActivities = async (personId: string) => {
if (!data) throw new Error(`Reading person ${personId} mentions failed`);
try {
return flow(
identity<MentionActivityData[] | undefined>,
filter((a) => !!a.noteBlock?.activity),
map(mapMentionActivity),
sortBy((a) => -a.finishedOn.getTime())
)(data);
Expand Down
73 changes: 73 additions & 0 deletions components/people/PersonRelationshipShowEdit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { PersonRelationship } from "@/helpers/person/relationships";
import { PlusCircle } from "lucide-react";
import { FC, useEffect, useState } from "react";
import { Button } from "../ui/button";
import RelationEdit from "./relations/relation-edit";
import RelationText from "./relations/relation-text";

interface PersonRelationshipShowEditProps {
relationship?: PersonRelationship;
updateRelationship: (
relationship: Partial<PersonRelationship> & { id: string }
) => void;
deleteRelationship?: (id: string) => Promise<string | undefined>;
}

export const emptyRelation = {
id: "NEW",
direction: "from",
} as PersonRelationship;

const PersonRelationshipShowEdit: FC<PersonRelationshipShowEditProps> = ({
updateRelationship,
deleteRelationship,
relationship = emptyRelation,
}) => {
const [isEditing, setIsEditing] = useState(false);
const [relation, setRelation] = useState(relationship);

useEffect(() => {
setRelation(relationship);
}, [relationship]);

const saveChanges = async () => {
await updateRelationship(relation);
if (relationship.id === "NEW") setRelation(emptyRelation);
setIsEditing(false);
};

const abortChanges = () => {
if (relationship.id === "NEW") setRelation(emptyRelation);
setIsEditing(false);
};

return (
<>
{relationship.id === "NEW" && !isEditing && (
<Button size="sm" className="gap-1" onClick={() => setIsEditing(true)}>
<PlusCircle className="w-4 h-4" />
Relation
</Button>
)}

{isEditing ? (
<RelationEdit
relationship={relation}
updateRelationship={setRelation}
saveChanges={saveChanges}
abortChanges={abortChanges}
/>
) : (
relationship.id !== "NEW" && (
<RelationText
relationship={relationship}
deleteRelation={() => deleteRelationship?.(relationship.id)}
startEditing={() => setIsEditing(true)}
/>
)
)}
</>
);
};

export default PersonRelationshipShowEdit;
61 changes: 14 additions & 47 deletions components/people/PersonRelationships.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,12 @@
import { PersonRelationship } from "@/helpers/person/relationships";
import { differenceInYears } from "date-fns";
import { capitalize } from "lodash";
import { PlusCircle } from "lucide-react";
import { FC, useState } from "react";
import { FC } from "react";
import DefaultAccordionItem from "../ui-elements/accordion/DefaultAccordionItem";
import LoadingAccordionItem from "../ui-elements/accordion/LoadingAccordionItem";
import { Button } from "../ui/button";
import RelationEdit from "./relations/relation-edit";
import RelationText from "./relations/relation-text";
import PersonRelationshipShowEdit from "./PersonRelationshipShowEdit";
import SubRelationships from "./relations/sub-relation-text";

type PersonRelationshipHelperProps = {
relationship: PersonRelationship;
updateRelationship: (
relationship: Partial<PersonRelationship> & { id: string }
) => Promise<string | undefined>;
deleteRelationship: (id: string) => Promise<string | undefined>;
};

const PersonRelationshipHelper: FC<PersonRelationshipHelperProps> = ({
updateRelationship,
deleteRelationship,
relationship,
}) => {
const [isEditing, setIsEditing] = useState(!relationship.nameOfRelationship);

return !isEditing ? (
<RelationText
relationship={relationship}
deleteRelation={() => deleteRelationship(relationship.id)}
startEditing={() => setIsEditing(true)}
/>
) : (
<RelationEdit
relationship={relationship}
updateRelationship={updateRelationship}
confirmChanges={() => setIsEditing(false)}
/>
);
};

type PersonRelationshipsProps = {
ownPersonId?: string;
relationships?: PersonRelationship[];
Expand Down Expand Up @@ -70,8 +37,14 @@ const PersonRelationships: FC<PersonRelationshipsProps> = ({
relationships,
updateRelationship,
deleteRelationship,
}) =>
!ownPersonId || !relationships ? (
}) => {
const saveChanges = async (
relation: Partial<PersonRelationship> & { id: string }
) => {
await updateRelationship(relation);
};

return !ownPersonId || !relationships ? (
<LoadingAccordionItem
value="loading-relations"
sizeTitle="lg"
Expand All @@ -84,20 +57,13 @@ const PersonRelationships: FC<PersonRelationshipsProps> = ({
triggerSubTitle={relationships.filter(validRelations).map(relationText)}
>
<div className="space-y-4">
<Button
size="sm"
className="gap-1"
onClick={() => updateRelationship({ id: "NEW" })}
>
<PlusCircle className="w-4 h-4" />
Relation
</Button>
<PersonRelationshipShowEdit updateRelationship={saveChanges} />

{relationships.map((r) => (
<PersonRelationshipHelper
<PersonRelationshipShowEdit
key={r.id}
relationship={r}
updateRelationship={updateRelationship}
updateRelationship={saveChanges}
deleteRelationship={deleteRelationship}
/>
))}
Expand All @@ -110,5 +76,6 @@ const PersonRelationships: FC<PersonRelationshipsProps> = ({
</div>
</DefaultAccordionItem>
);
};

export default PersonRelationships;
Loading

0 comments on commit 4b26af3

Please sign in to comment.