Skip to content

Commit

Permalink
Remove backend limit on sample batches
Browse files Browse the repository at this point in the history
  • Loading branch information
XanderVertegaal committed Sep 16, 2024
1 parent 2cfa23e commit 85a040f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 30 deletions.
23 changes: 2 additions & 21 deletions backend/aethel_db/views/sample_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class AethelSampleDataPhrase:


class AethelSampleError(Enum):
INVALID_SKIP = "INVALID_SKIP"
INVALID_WORD = "INVALID_WORD"
NO_INPUT = "NO_INPUT"

Expand All @@ -39,14 +38,10 @@ def serialize(self):
return asdict(self)


SAMPLE_LIMIT = 10


@dataclass
class AethelSampleDataResponse:
results: list[AethelSampleDataResult] = field(default_factory=list)
error: AethelSampleError | None = None
skip: int = 0

def get_or_create_result(self, sample: Sample) -> AethelSampleDataResult:
"""
Expand All @@ -66,15 +61,10 @@ def get_or_create_result(self, sample: Sample) -> AethelSampleDataResult:
return new_result

def json_response(self) -> JsonResponse:
total_count = len(self.results)
limit = min(SAMPLE_LIMIT, total_count)
skip = max(0, self.skip)
paginated = list(self.results)[skip : skip + limit]
serialized = [result.serialize() for result in paginated]
serialized = [result.serialize() for result in self.results]
return JsonResponse(
{
"results": serialized,
"totalCount": total_count,
"error": self.error,
},
status=status.HTTP_200_OK,
Expand All @@ -85,18 +75,15 @@ class AethelSampleDataView(APIView):
def get(self, request: HttpRequest) -> JsonResponse:
type_input = self.request.query_params.get("type", None)
word_input = self.request.query_params.get("word", None)
skip = self.request.query_params.get("skip", 0)

response_object = AethelSampleDataResponse()

error = self.validate_input(type_input, word_input, skip)
error = self.validate_input(type_input, word_input)

if error:
response_object.error = error
return response_object.json_response()

response_object.skip = int(skip)

word_input = json.loads(word_input)

try:
Expand Down Expand Up @@ -134,13 +121,7 @@ def validate_input(
self,
type_input: str | None,
word_input: str | None,
skip: str | int,
) -> AethelSampleError | None:
try:
skip = int(skip)
except ValueError:
return AethelSampleError.INVALID_SKIP

try:
word_input = json.loads(word_input)
except json.JSONDecodeError:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<td colspan="5">
<ul class="sample-list mb-2">
@for (sample of samples(); track $index) {
@for (sample of visibleSamples(); track $index) {
<li class="sample-item">
<p>{{ $index + 1 }}</p>
<p class="phrase-list mx-2">
Expand All @@ -27,7 +27,7 @@
<button
class="button button-primary"
type="button"
(click)="loadSamples()"
(click)="loadMoreSamples()"
>
Load more
</button>
Expand Down
19 changes: 12 additions & 7 deletions frontend/src/app/aethel/sample-details/sample-data.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@ import { environment } from "src/environments/environment";
export class SampleDataComponent implements OnInit {
@Input({ required: true }) aethelResult: AethelListResult | null = null;

public samples = signal<AethelSampleDataResult[]>([]);
public limit = signal<number>(10);
public loading = false;

// Hides the "Load More" button when all samples have been loaded.
public allSamplesLoaded = computed(() => {
if (!this.aethelResult) {
return false;
}
return this.samples().length >= this.aethelResult.sampleCount;
return this.limit() >= this.samples().length;
});

public visibleSamples = computed(() => {
return this.samples().slice(0, this.limit());
})

private samples = signal<AethelSampleDataResult[]>([]);

constructor(
private destroyRef: DestroyRef,
private http: HttpClient,
) {}

ngOnInit(): void {
this.loadSamples();
}

public loadSamples(): void {
if (!this.aethelResult) {
return;
}
Expand All @@ -60,6 +62,10 @@ export class SampleDataComponent implements OnInit {
});
}

public loadMoreSamples(): void {
this.limit.update(limit => limit + 10);
}

public getSampleURL(sampleName: string): string[] {
return ["sample", sampleName.replace(".xml", "")];
}
Expand All @@ -70,7 +76,6 @@ export class SampleDataComponent implements OnInit {
aethelResult.phrase.items.map((item) => item.word),
),
type: aethelResult.type,
skip: this.samples().length.toString(),
};
return queryParams;
}
Expand Down

0 comments on commit 85a040f

Please sign in to comment.