Skip to content

Commit

Permalink
Use parenthesised notation in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
XanderVertegaal committed Aug 26, 2024
1 parent 83a417d commit ade6bd5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
21 changes: 17 additions & 4 deletions backend/aethel_db/views/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from aethel_db.models import dataset

from aethel.frontend import LexicalPhrase
from aethel.mill.types import type_prefix
from aethel.mill.types import type_prefix, Type, type_repr


@dataclass
Expand All @@ -25,6 +25,7 @@ class AethelListPhrase:
@dataclass
class AethelListResult:
phrase: AethelListPhrase
display_type: str
type: str
sampleCount: int = 0
# Counter for the number of samples that contain this item.
Expand All @@ -34,6 +35,7 @@ class AethelListResult:
def serialize(self):
return {
"phrase": asdict(self.phrase),
"displayType": self.display_type,
"type": self.type,
"sampleCount": len(self._sample_names),
}
Expand All @@ -49,7 +51,7 @@ class AethelListResponse:
error: str | None = None

def get_or_create_result(
self, phrase: LexicalPhrase, type: str
self, phrase: LexicalPhrase, type: Type
) -> AethelListResult:
"""
Return an existing result with the same lemma, word, and type, or create a new one if it doesn't exist.
Expand All @@ -60,12 +62,22 @@ def get_or_create_result(
]
new_phrase = AethelListPhrase(items=items)

# type_repr: parenthesised notation, used for frontend.
display_type = type_repr(type)
# type_prefix: space-separated notation, used to find sample data.
type = type_prefix(type)

# Construct a unique 'key' for every combination of word, lemma and type, so we can group samples.
key_word = tuple(item.word for item in phrase.items)
key_lemma = tuple(item.lemma for item in phrase.items)
key = (key_word, key_lemma, type)

new_result = AethelListResult(phrase=new_phrase, type=type, sampleCount=0)
new_result = AethelListResult(
phrase=new_phrase,
display_type=display_type,
type=type,
sampleCount=0,
)
return self.results.setdefault(key, new_result)

def json_response(self) -> JsonResponse:
Expand Down Expand Up @@ -100,7 +112,8 @@ def get(self, request: HttpRequest) -> JsonResponse:

result = response_object.get_or_create_result(
# type_prefix returns a string representation of the type, with spaces between the elements.
phrase=phrase, type=type_prefix(phrase.type)
phrase=phrase,
type=phrase.type,
)

result._sample_names.add(sample.name)
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/aethel/aethel.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ <h2 class="title is-2" i18n>Æthel</h2>
</td>
<td>{{ combineWord(row) }}</td>
<td>{{ combineLemma(row) }}</td>
<td [innerHTML]="row.type | proof"></td>
<td [innerHTML]="row.displayType | proof"></td>
<td>{{ row.sampleCount }}</td>
</tr>
</ng-template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe("SampleDataComponent", () => {
},
sampleCount: 0,
type: "test",
displayType: "Test",
};
const mockResponse: AethelSampleDataReturn = {
results: [],
Expand Down
1 change: 1 addition & 0 deletions frontend/src/app/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface AethelListPhrase {
export interface AethelListResult {
phrase: AethelListPhrase
type: string;
displayType: string;
sampleCount: number;
}

Expand Down

0 comments on commit ade6bd5

Please sign in to comment.