forked from formbricks/formbricks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Data table for persons (formbricks#3154)
- Loading branch information
Showing
38 changed files
with
758 additions
and
344 deletions.
There are no files selected for viewing
6 changes: 5 additions & 1 deletion
6
...or)/environments/[environmentId]/surveys/[surveyId]/edit/components/AddQuestionButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ditor)/environments/[environmentId]/surveys/[surveyId]/edit/components/EditorCardMenu.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...environments/[environmentId]/(people)/people/[personId]/components/DeletePersonButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
apps/web/app/(app)/environments/[environmentId]/(people)/people/actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
"use server"; | ||
|
||
import { z } from "zod"; | ||
import { authenticatedActionClient } from "@formbricks/lib/actionClient"; | ||
import { checkAuthorization } from "@formbricks/lib/actionClient/utils"; | ||
import { getAttributes } from "@formbricks/lib/attribute/service"; | ||
import { getOrganizationIdFromEnvironmentId } from "@formbricks/lib/organization/utils"; | ||
import { getPeople } from "@formbricks/lib/person/service"; | ||
import { ZId } from "@formbricks/types/common"; | ||
|
||
const ZGetPersonsAction = z.object({ | ||
environmentId: ZId, | ||
page: z.number(), | ||
}); | ||
|
||
export const getPersonsAction = authenticatedActionClient | ||
.schema(ZGetPersonsAction) | ||
.action(async ({ ctx, parsedInput }) => { | ||
await checkAuthorization({ | ||
userId: ctx.user.id, | ||
organizationId: await getOrganizationIdFromEnvironmentId(parsedInput.environmentId), | ||
rules: ["environment", "read"], | ||
}); | ||
|
||
return getPeople(parsedInput.environmentId, parsedInput.page); | ||
}); | ||
|
||
const ZGetPersonAttributesAction = z.object({ | ||
environmentId: ZId, | ||
personId: ZId, | ||
}); | ||
|
||
export const getPersonAttributesAction = authenticatedActionClient | ||
.schema(ZGetPersonAttributesAction) | ||
.action(async ({ ctx, parsedInput }) => { | ||
await checkAuthorization({ | ||
userId: ctx.user.id, | ||
organizationId: await getOrganizationIdFromEnvironmentId(parsedInput.environmentId), | ||
rules: ["environment", "read"], | ||
}); | ||
|
||
return getAttributes(parsedInput.personId); | ||
}); |
38 changes: 0 additions & 38 deletions
38
apps/web/app/(app)/environments/[environmentId]/(people)/people/components/PersonCard.tsx
This file was deleted.
Oops, something went wrong.
107 changes: 107 additions & 0 deletions
107
.../web/app/(app)/environments/[environmentId]/(people)/people/components/PersonDataView.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
"use client"; | ||
|
||
import { | ||
getPersonAttributesAction, | ||
getPersonsAction, | ||
} from "@/app/(app)/environments/[environmentId]/(people)/people/actions"; | ||
import { PersonTable } from "@/app/(app)/environments/[environmentId]/(people)/people/components/PersonTable"; | ||
import { useEffect, useState } from "react"; | ||
import React from "react"; | ||
import { TEnvironment } from "@formbricks/types/environment"; | ||
import { TPerson, TPersonTableData } from "@formbricks/types/people"; | ||
|
||
interface PersonDataViewProps { | ||
environment: TEnvironment; | ||
personCount: number; | ||
itemsPerPage: number; | ||
} | ||
|
||
export const PersonDataView = ({ environment, personCount, itemsPerPage }: PersonDataViewProps) => { | ||
const [persons, setPersons] = useState<TPerson[]>([]); | ||
const [personTableData, setPersonTableData] = useState<TPersonTableData[]>([]); | ||
const [pageNumber, setPageNumber] = useState<number>(1); | ||
const [totalPersons, setTotalPersons] = useState<number>(0); | ||
const [isDataLoaded, setIsDataLoaded] = useState<boolean>(false); | ||
const [hasMore, setHasMore] = useState<boolean>(false); | ||
const [loadingNextPage, setLoadingNextPage] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
setTotalPersons(personCount); | ||
setHasMore(pageNumber < Math.ceil(personCount / itemsPerPage)); | ||
|
||
const fetchData = async () => { | ||
try { | ||
const getPersonActionData = await getPersonsAction({ | ||
environmentId: environment.id, | ||
page: pageNumber, | ||
}); | ||
if (getPersonActionData?.data) { | ||
setPersons(getPersonActionData.data); | ||
} | ||
} catch (error) { | ||
console.error("Error fetching people data:", error); | ||
} | ||
}; | ||
|
||
fetchData(); | ||
}, [pageNumber]); | ||
|
||
// Fetch additional person attributes and update table data | ||
useEffect(() => { | ||
const fetchAttributes = async () => { | ||
const updatedPersonTableData = await Promise.all( | ||
persons.map(async (person) => { | ||
const attributes = await getPersonAttributesAction({ | ||
environmentId: environment.id, | ||
personId: person.id, | ||
}); | ||
return { | ||
createdAt: person.createdAt, | ||
personId: person.id, | ||
userId: person.userId, | ||
email: attributes?.data?.email ?? "", | ||
attributes: attributes?.data ?? {}, | ||
}; | ||
}) | ||
); | ||
setPersonTableData(updatedPersonTableData); | ||
setIsDataLoaded(true); | ||
}; | ||
|
||
if (persons.length > 0) { | ||
fetchAttributes(); | ||
} | ||
}, [persons]); | ||
|
||
const fetchNextPage = async () => { | ||
if (hasMore && !loadingNextPage) { | ||
setLoadingNextPage(true); | ||
const getPersonsActionData = await getPersonsAction({ | ||
environmentId: environment.id, | ||
page: pageNumber, | ||
}); | ||
if (getPersonsActionData?.data) { | ||
const newData = getPersonsActionData.data; | ||
setPersons((prevPersonsData) => [...prevPersonsData, ...newData]); | ||
} | ||
setPageNumber((prevPage) => prevPage + 1); | ||
setHasMore(pageNumber + 1 < Math.ceil(totalPersons / itemsPerPage)); | ||
setLoadingNextPage(false); | ||
} | ||
}; | ||
|
||
const deletePersons = (personIds: string[]) => { | ||
setPersons(persons.filter((p) => !personIds.includes(p.id))); | ||
}; | ||
|
||
return ( | ||
<PersonTable | ||
data={personTableData} | ||
fetchNextPage={fetchNextPage} | ||
hasMore={hasMore} | ||
isDataLoaded={isDataLoaded} | ||
deletePersons={deletePersons} | ||
environmentId={environment.id} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.