diff --git a/src/hooks/useExpressionData.ts b/src/hooks/useExpressionData.ts
index d24038b..4bae751 100644
--- a/src/hooks/useExpressionData.ts
+++ b/src/hooks/useExpressionData.ts
@@ -1,11 +1,26 @@
import { useState } from "react";
import { MockData } from "../mock";
+import {
+ Category,
+ Expression,
+ ExpressionSet,
+ ExpressionToCategory,
+ ExpressionToExpressionSet,
+} from "../model";
-export function useExpressionData() {
+export interface ExpressionData {
+ categories: Category[];
+ expressions: Expression[];
+ expression_sets: ExpressionSet[];
+ expression_to_category: ExpressionToCategory[];
+ expression_to_expression_set: ExpressionToExpressionSet[];
+}
+
+export function useExpressionData(): ExpressionData {
const [state] = useState(MockData);
return {
- catefories: state.categories,
+ categories: state.categories,
expressions: state.expressions,
expression_sets: state.expression_sets,
expression_to_category: state.expression_to_category,
diff --git a/src/pages/expression-sets/details.tsx b/src/pages/expression-sets/details.tsx
index 1e7a5a7..c669e61 100644
--- a/src/pages/expression-sets/details.tsx
+++ b/src/pages/expression-sets/details.tsx
@@ -10,8 +10,9 @@ const ExpressionSetDetailsPage: NextPage = () => {
const { expression_sets, expression_to_expression_set } = useExpressionData();
// Fallback for expression set not found
+ const expression_set_id = Number.parseInt(query["set-id"] as string);
const expression_set = expression_sets.find(
- (item) => item.id === Number.parseInt(query["set-id"] as string)
+ (item) => item.id === expression_set_id
);
if (!expression_set)
return (
@@ -51,7 +52,7 @@ const ExpressionSetDetailsPage: NextPage = () => {
{/* TODO other details */}
{
+ const { query } = useRouter();
+ const {
+ expressions,
+ categories,
+ expression_sets,
+ expression_to_expression_set,
+ expression_to_category,
+ } = useExpressionData();
+
+ // Fallback for expression set not found
+ const expression_set = expression_sets.find(
+ (item) => item.id === Number.parseInt(query["set-id"] as string)
+ );
+ if (!expression_set)
+ return (
+
+ Expression set not found
+
+ );
+
+ // Fallback for expression not found
+ const expression_candidates = getExpressionsInSet({
+ expression_set_id: expression_set.id,
+ expression_to_expression_set,
+ expressions,
+ });
+
+ if (expression_candidates.length === 0) {
+ return (
+
+ No expressions left in this set
+
+ );
+ }
+
+ // Choose word and find categories
+ // Find categories
+ const expression = sample(expression_candidates);
+ const expression_categories = getCategoriesInExpression({
+ categories,
+ expression_id: expression.id,
+ expression_to_category,
+ });
+
+ console.log("Expressions", expressions);
+ console.log("Categories", expression_categories);
+ console.log("Expression", expression);
+
+ return (
+
+ category.name)}
+ description={expression.description}
+ show_description
+ />
+
+ );
+};
+
+export default ExpressionSetListPage;
diff --git a/src/util/array-utils.ts b/src/util/array-utils.ts
new file mode 100644
index 0000000..ef0f762
--- /dev/null
+++ b/src/util/array-utils.ts
@@ -0,0 +1,3 @@
+export function sample(array: T[]) {
+ return array[Math.floor(Math.random() * array.length)];
+}
diff --git a/src/util/data-utils.ts b/src/util/data-utils.ts
new file mode 100644
index 0000000..774fdda
--- /dev/null
+++ b/src/util/data-utils.ts
@@ -0,0 +1,40 @@
+import {
+ Category,
+ Expression,
+ ExpressionToCategory,
+ ExpressionToExpressionSet,
+} from "../model";
+
+interface GetExpressionsInSetParams {
+ expression_set_id: number;
+ expressions: Expression[];
+ expression_to_expression_set: ExpressionToExpressionSet[];
+}
+
+export function getExpressionsInSet({
+ expression_set_id,
+ expression_to_expression_set,
+ expressions,
+}: GetExpressionsInSetParams): Expression[] {
+ const expression_ids = expression_to_expression_set
+ .filter((item) => item.expression_set_id === expression_set_id)
+ .map((item) => item.expression_id);
+ return expressions.filter((item) => expression_ids.includes(item.id));
+}
+
+interface GetCategoriesInExpressionParams {
+ expression_id: number;
+ categories: Category[];
+ expression_to_category: ExpressionToCategory[];
+}
+
+export function getCategoriesInExpression({
+ categories,
+ expression_id,
+ expression_to_category,
+}: GetCategoriesInExpressionParams) {
+ const category_ids = expression_to_category
+ .filter((item) => item.expression_id === expression_id)
+ .map((item) => item.category_id);
+ return categories.filter((item) => category_ids.includes(item.id));
+}