-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️(frontend) make document and video components generic
The Document and Video components are almost identical in structure. This refactor moves them into the core frontend package as generic components, allowing for better reusability and maintainability.
- Loading branch information
1 parent
da32cf5
commit 8cda9bc
Showing
45 changed files
with
351 additions
and
496 deletions.
There are no files selected for viewing
File renamed without changes.
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
71 changes: 71 additions & 0 deletions
71
src/frontend/packages/core/src/components/ResourceFilters/index.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,71 @@ | ||
import React from "react"; | ||
import { Select } from "@openfun/cunningham-react"; | ||
import { DateFilters } from "../DateFilters"; | ||
import { useResourceFilters } from "../../hooks"; | ||
import { Resource } from "../../types"; | ||
|
||
export type ResourceOption = { | ||
value: string; | ||
label: string; | ||
}; | ||
|
||
// FIXME - refactor this part with the prototype of Experience Index. | ||
export const RESOURCES: Array<Resource> = [ | ||
{ id: "uuid://0aecfa93-cef3-45ae-b7f5-a603e9e45f50", title: "Resource 1" }, | ||
{ id: "uuid://1c0c127a-f121-4bd1-8db6-918605c2645d", title: "Resource 2" }, | ||
{ id: "uuid://541dab6b-50ae-4444-b230-494f0621f132", title: "Resource 3" }, | ||
{ id: "uuid://69d32ad5-3af5-4160-a995-87e09da6865c", title: "Resource 4" }, | ||
{ id: "uuid://7d4f3c70-1e79-4243-9b7d-166076ce8bfb", title: "Resource 5" }, | ||
{ id: "uuid://8d386f48-3baa-4acf-8a46-0f2be4ae243e", title: "Resource 6" }, | ||
{ id: "uuid://b172ec09-97ec-4651-bc57-6eabebf47ed0", title: "Resource 7" }, | ||
{ id: "uuid://d613b564-5d18-4238-a69c-0fc8cee5d0e7", title: "Resource 8" }, | ||
{ id: "uuid://dd38149d-956a-483d-8975-c1506de1e1a9", title: "Resource 9" }, | ||
{ id: "uuid://e151ee65-7a72-478c-ac57-8a02f19e748b", title: "Resource 10" }, | ||
]; | ||
|
||
export type ResourceFiltersProps = { | ||
label: string; | ||
}; | ||
/** | ||
* A React functional component for filtering documents and specifying a date range. | ||
* | ||
* This component provides user interface elements to select documents from a list, | ||
* specify a date range, and trigger a data refresh. | ||
* @param {label} string - The label name for the resources. | ||
* | ||
*/ | ||
export const ResourceFilters: React.FC<ResourceFiltersProps> = ({ label }) => { | ||
const { setResources } = useResourceFilters(); | ||
|
||
const getResourceOptions = (): ResourceOption[] => { | ||
return RESOURCES.map((item) => ({ | ||
value: item.id, | ||
label: item.title, | ||
})); | ||
}; | ||
|
||
const handleResourceIdsChange = ( | ||
value: string | string[] | number | undefined, | ||
): void => { | ||
if (typeof value === "number") { | ||
return; | ||
} | ||
|
||
setResources(RESOURCES.filter((resource) => value?.includes(resource.id))); | ||
}; | ||
|
||
return ( | ||
<DateFilters> | ||
<Select | ||
label={label} | ||
defaultValue={RESOURCES[0].id} | ||
options={getResourceOptions()} | ||
multi={true} | ||
monoline={true} | ||
onChange={(selectedValues) => | ||
handleResourceIdsChange(selectedValues.target.value) | ||
} | ||
/> | ||
</DateFilters> | ||
); | ||
}; |
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
File renamed without changes.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./ltiContext"; | ||
export * from "./filtersContext"; | ||
export * from "./jwtContext"; | ||
export * from "./dateFiltersContext"; | ||
export * from "./resourceFiltersContext"; |
29 changes: 29 additions & 0 deletions
29
src/frontend/packages/core/src/contexts/resourceFiltersContext.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,29 @@ | ||
import React, { | ||
createContext, | ||
Dispatch, | ||
SetStateAction, | ||
useMemo, | ||
useState, | ||
} from "react"; | ||
import { Resource } from "../types"; | ||
|
||
export interface ResourceFiltersContextType { | ||
resources: Array<Resource>; | ||
setResources: Dispatch<SetStateAction<Array<Resource>>>; | ||
} | ||
|
||
export const ResourceFiltersContext = | ||
createContext<ResourceFiltersContextType | null>(null); | ||
|
||
export const ResourceFiltersProvider: React.FC<{ children: any }> = ({ | ||
children, | ||
}) => { | ||
const [resources, setResources] = useState<Array<Resource>>([]); | ||
const value = useMemo(() => ({ resources, setResources }), [resources]); | ||
|
||
return ( | ||
<ResourceFiltersContext.Provider value={value}> | ||
{children} | ||
</ResourceFiltersContext.Provider> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from "./useFilters"; | ||
export * from "./useDateFilters"; | ||
export * from "./useJwtContext"; | ||
export * from "./useLTIContext"; | ||
export * from "./useRefreshToken"; | ||
export * from "./useResourceFilters"; | ||
export * from "./useTokenInterceptor"; | ||
export * from "./useJwtContext"; |
2 changes: 1 addition & 1 deletion
2
...end/packages/core/src/hooks/useFilters.ts → ...packages/core/src/hooks/useDateFilters.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
15 changes: 15 additions & 0 deletions
15
src/frontend/packages/core/src/hooks/useResourceFilters.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,15 @@ | ||
import { useContext } from "react"; | ||
import { | ||
ResourceFiltersContext, | ||
ResourceFiltersContextType, | ||
} from "../contexts"; | ||
|
||
export const useResourceFilters = (): ResourceFiltersContextType => { | ||
const value = useContext(ResourceFiltersContext); | ||
if (!value) { | ||
throw new Error( | ||
`Missing wrapping Provider for Store ResourceFiltersContextType`, | ||
); | ||
} | ||
return value; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import dayjs from "dayjs"; | ||
import { formatDates } from "./formatDates"; | ||
|
||
/** | ||
* Calculate the previous time period based on the provided since and until dates. | ||
* | ||
* This function calculates the start and end dates of the previous time period relative to the given 'since' and 'until' dates. | ||
* It computes the duration of the selected period and calculates the previous period's start and end dates accordingly. | ||
* | ||
* @param {string} since - The start date of the current period. | ||
* @param {string} until - The end date of the current period. | ||
* @returns {string[]} An array of two ISO date strings representing the start and end dates of the previous time period. | ||
*/ | ||
export const getPreviousPeriod = (since: string, until: string) => { | ||
const periodDuration = dayjs(until).diff(since, "day") + 1; | ||
return formatDates([ | ||
dayjs(since).subtract(periodDuration, "day"), | ||
dayjs(since).subtract(1, "day"), | ||
]); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ResourceMetricsResponse } from "../types"; | ||
|
||
/** | ||
* Calculate the total sum of downloads from an array of ResourceMetricsResponse objects. | ||
* @param {ResourceMetricsResponse[]} metrics - An array of ResourceMetricsResponse objects. | ||
* @returns {number} The total sum of downloads. | ||
*/ | ||
export const sumMetrics = (metrics: ResourceMetricsResponse[]): number => | ||
metrics?.length | ||
? metrics | ||
.map((v) => v.total) | ||
.reduce((previous: number, current: number) => previous + current) | ||
: 0; |
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.