fix(mobile): Nya förslag bläddrar bland recept + laddindikator (MIKRO 53)
CI / Typecheck, test & build (push) Failing after 44s
CI / Typecheck, test & build (push) Failing after 44s
This commit is contained in:
@@ -51,16 +51,22 @@ interface WhatToEatResponse {
|
|||||||
export default function WhatToEatScreen() {
|
export default function WhatToEatScreen() {
|
||||||
const [craving, setCraving] = useState("");
|
const [craving, setCraving] = useState("");
|
||||||
const [submittedCraving, setSubmittedCraving] = useState("");
|
const [submittedCraving, setSubmittedCraving] = useState("");
|
||||||
|
const [page, setPage] = useState(0);
|
||||||
const { track } = useAnalytics();
|
const { track } = useAnalytics();
|
||||||
|
|
||||||
const query = useQuery({
|
const query = useQuery({
|
||||||
queryKey: ["what-to-eat", submittedCraving],
|
queryKey: ["what-to-eat", submittedCraving],
|
||||||
queryFn: () =>
|
queryFn: () =>
|
||||||
api<WhatToEatResponse>(
|
api<WhatToEatResponse>(
|
||||||
`/v1/recommendations/what-to-eat?limit=5${submittedCraving ? `&craving=${encodeURIComponent(submittedCraving)}` : ""}`,
|
`/v1/recommendations/what-to-eat?limit=15${submittedCraving ? `&craving=${encodeURIComponent(submittedCraving)}` : ""}`,
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const allRecs = query.data?.recommendations ?? [];
|
||||||
|
const shownRecs: Recommendation[] = allRecs.length
|
||||||
|
? Array.from({ length: Math.min(5, allRecs.length) }, (_, i) => allRecs[(page * 5 + i) % allRecs.length]).filter((r): r is Recommendation => r != null)
|
||||||
|
: [];
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (query.data) {
|
if (query.data) {
|
||||||
track(
|
track(
|
||||||
@@ -87,7 +93,7 @@ export default function WhatToEatScreen() {
|
|||||||
placeholder={t("wte.cravingPlaceholder")}
|
placeholder={t("wte.cravingPlaceholder")}
|
||||||
value={craving}
|
value={craving}
|
||||||
onChangeText={setCraving}
|
onChangeText={setCraving}
|
||||||
onSubmitEditing={() => setSubmittedCraving(craving)}
|
onSubmitEditing={() => { setSubmittedCraving(craving); setPage(0); }}
|
||||||
returnKeyType="search"
|
returnKeyType="search"
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
@@ -126,9 +132,9 @@ export default function WhatToEatScreen() {
|
|||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{query.data.recommendations.length === 0 && <EmptyState text={t("wte.empty")} />}
|
{allRecs.length === 0 && <EmptyState text={t("wte.empty")} />}
|
||||||
|
|
||||||
{query.data.recommendations.map((rec, index) => (
|
{shownRecs.map((rec, index) => (
|
||||||
<Card key={rec.recipeId} onPress={() => router.push(`/recipe/${rec.recipeId}`)}>
|
<Card key={rec.recipeId} onPress={() => router.push(`/recipe/${rec.recipeId}`)}>
|
||||||
<Row style={{ justifyContent: "space-between" }}>
|
<Row style={{ justifyContent: "space-between" }}>
|
||||||
<Heading>
|
<Heading>
|
||||||
@@ -167,7 +173,8 @@ export default function WhatToEatScreen() {
|
|||||||
<Button
|
<Button
|
||||||
label={t("wte.refresh")}
|
label={t("wte.refresh")}
|
||||||
variant="secondary"
|
variant="secondary"
|
||||||
onPress={() => void query.refetch()}
|
loading={query.isFetching}
|
||||||
|
onPress={() => setPage((p) => p + 1)}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user