Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week challenges #4

Merged
merged 4 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/components/AchievementBadge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ type AchievementProps = Record<
>;

const achievementProps: AchievementProps = {
challengeAccepted: {
[AchievementLevel.Bronze]: {
textProps: [5],
emoji: '🛴',
},
[AchievementLevel.Silver]: {
textProps: [10],
emoji: '🛵',
},
[AchievementLevel.Gold]: {
textProps: [20],
emoji: '🚀',
},
},
committed: {
[AchievementLevel.Bronze]: {
textProps: [3],
Expand All @@ -43,7 +57,7 @@ const achievementProps: AchievementProps = {
},
[AchievementLevel.Silver]: {
textProps: [80],
emoji: '🐥',
emoji: '🐧',
},
[AchievementLevel.Gold]: {
textProps: [150],
Expand Down
20 changes: 20 additions & 0 deletions src/components/AchievementList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ const {achievements} = storeToRefs(useAppStateStore());
/>
</div>
</div>
<div>
<p class="achievement-list__label">{{ $t('achievements.challengeAccepted.title') }}</p>
<div class="achievement-list__badge-container">
<AchievementBadge
:active="achievements.challengeAccepted >= AchievementLevel.Bronze"
:level="AchievementLevel.Bronze"
achievement="challengeAccepted"
/>
<AchievementBadge
:active="achievements.challengeAccepted >= AchievementLevel.Silver"
:level="AchievementLevel.Silver"
achievement="challengeAccepted"
/>
<AchievementBadge
:active="achievements.challengeAccepted >= AchievementLevel.Gold"
:level="AchievementLevel.Gold"
achievement="challengeAccepted"
/>
</div>
</div>
<div>
<p class="achievement-list__label">{{ $t('achievements.committed.title') }}</p>
<div class="achievement-list__badge-container">
Expand Down
4 changes: 3 additions & 1 deletion src/components/CategoryStatus.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ withDefaults(
>
<span>{{ $t(totals ? 'categoryStatus.topLabelTotal' : 'categoryStatus.topLabel') }}</span>
<span :class="totals ? 'text-5xl' : 'text-6xl'">{{ veggies.length }}</span>
<span>{{ $t('categoryStatus.bottomLabel') }}</span>
<span>{{
$t(totals ? 'categoryStatus.bottomLabelTotal' : 'categoryStatus.bottomLabel')
}}</span>
</i18n-t>
</div>
</template>
Expand Down
8 changes: 5 additions & 3 deletions src/components/VeggieSearch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import {
import {useElementBounding, useMemoize} from '@vueuse/core';
import {ALL_VEGGIES} from '@/utils/constants';
import {Category, type TranslatedListing} from '@/utils/types';
import VeggieSearchGroup from '@/components/VeggieSearchGroup.vue';
import {getCategoryForVeggie} from '@/utils/helpers';
import {useScreen} from '@/hooks/screen';
import VeggieSearchGroup from '@/components/VeggieSearchGroup.vue';
import VeggieSearchChallenge from '@/components/VeggieSearchChallenge.vue';

const model = defineModel<string[]>({
required: true,
Expand Down Expand Up @@ -53,7 +54,7 @@ const filteredVeggies = useMemoize(
},
);

const getAvailableHeightForOptions = computed(
const availableHeightForOptions = computed(
() => `max-height: calc(${visualHeight.value}px - ${top.value}px - 1rem)`,
);

Expand Down Expand Up @@ -91,12 +92,13 @@ watch(query, scrollToStart);
>
<ComboboxOptions
ref="optionsElement"
:style="getAvailableHeightForOptions"
:style="availableHeightForOptions"
class="veggie-search__options"
>
<li v-if="filteredVeggies().length === 0 && query !== ''" class="veggie-search__no-results">
{{ $t('veggieSearch.noResults') }}
</li>
<VeggieSearchChallenge />
<VeggieSearchGroup
v-for="category in Category"
:key="category"
Expand Down
27 changes: 27 additions & 0 deletions src/components/VeggieSearchChallenge.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script setup lang="ts">
import {inject} from 'vue';
import VeggieSearchOption from '@/components/VeggieSearchOption.vue';
import {KEYS} from '@/utils/constants';

const challenge = inject(KEYS.challenge);
</script>

<template>
<li v-if="challenge">
<div id="veggie-search-heading-challenge" class="veggie-search__heading">
<span aria-hidden="true">🎖️</span>
<span>{{ $t('veggieSearch.challenge') }}</span>
</div>
<ul role="group" :aria-labelledby="'veggie-search-heading-challenge'">
<VeggieSearchOption :veggie="challenge" />
</ul>
</li>
</template>

<style scoped lang="scss">
.veggie-search__heading {
@apply flex-container justify-start;
@apply select-none p-2;
@apply bg-slate-300 text-slate-900;
}
</style>
35 changes: 5 additions & 30 deletions src/components/VeggieSearchGroup.vue
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
<script setup lang="ts">
import {useMemoize} from '@vueuse/core';
import {ComboboxOption} from '@headlessui/vue';
import type {Category, TranslatedListing} from '@/utils/types';
import {CATEGORY_EMOJI} from '@/utils/constants';
import VeggieSearchOption from './VeggieSearchOption.vue';

defineProps<{
items: TranslatedListing[];
category: Category;
}>();

const getOptionClasses = useMemoize((active: boolean, selected: boolean) => {
const textClass = active ? 'text-slate-50' : 'text-slate-900 fill-slate-900';
let bgClass = `bg-slate-50`;
if (active) {
bgClass = 'bg-sky-500';
} else if (selected) {
bgClass = 'bg-sky-200';
}

return `${textClass} ${bgClass}`;
});
</script>

<template>
Expand All @@ -29,20 +16,12 @@ const getOptionClasses = useMemoize((active: boolean, selected: boolean) => {
<span>{{ $t(`categories.${category}`) }} ({{ items.length }})</span>
</div>
<ul role="group" :aria-labelledby="`veggie-search-heading-${category}`">
<ComboboxOption
<VeggieSearchOption
v-for="{veggie, translation} in items"
v-slot="{active, selected}"
as="template"
:key="veggie"
:value="veggie"
>
<li :class="[getOptionClasses(active, selected), 'veggie-search__option']" role="menuitem">
<span>
{{ translation }}
</span>
<IconComponent v-if="selected" icon="check" />
</li>
</ComboboxOption>
:veggie="veggie"
:translation="translation"
/>
</ul>
</li>
</template>
Expand All @@ -53,8 +32,4 @@ const getOptionClasses = useMemoize((active: boolean, selected: boolean) => {
@apply select-none p-2;
@apply bg-slate-300 text-slate-900;
}

.veggie-search__option {
@apply dropdown-list-option;
}
</style>
35 changes: 35 additions & 0 deletions src/components/VeggieSearchOption.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<script setup lang="ts">
import {useMemoize} from '@vueuse/core';
import {ComboboxOption} from '@headlessui/vue';
defineProps<{
veggie: string;
translation?: string;
}>();

const getOptionClasses = useMemoize((active: boolean, selected: boolean) => {
const textClass = active ? 'text-slate-50' : 'text-slate-900 fill-slate-900';
let bgClass = `bg-slate-50`;
if (active) {
bgClass = 'bg-sky-500';
} else if (selected) {
bgClass = 'bg-sky-200';
}

return `${textClass} ${bgClass}`;
});
</script>
<template>
<ComboboxOption v-slot="{active, selected}" as="template" :key="veggie" :value="veggie">
<li :class="[getOptionClasses(active, selected), 'veggie-search__option']" role="menuitem">
<span>
{{ translation || $t(`veggies.${veggie}`) }}
</span>
<IconComponent v-if="selected" icon="check" />
</li>
</ComboboxOption>
</template>
<style lang="scss" scoped>
.veggie-search__option {
@apply dropdown-list-option;
}
</style>
4 changes: 2 additions & 2 deletions src/components/__tests__/AchievementList.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('AchievementList', () => {
it('renders', () => {
const wrapper = mount(AchievementList);
expect(wrapper.html()).toMatchSnapshot();
expect(wrapper.findAll('.badge[aria-disabled="true"]').length).toBe(15);
expect(wrapper.findAll('.badge[aria-disabled="true"]').length).toBe(18);
});

it('renders with badges enabled', () => {
Expand All @@ -36,6 +36,6 @@ describe('AchievementList', () => {

const wrapper = mount(AchievementList);
expect(wrapper.html()).toMatchSnapshot();
expect(wrapper.findAll('.badge[aria-disabled="true"]').length).toBe(7);
expect(wrapper.findAll('.badge[aria-disabled="true"]').length).toBe(10);
});
});
2 changes: 1 addition & 1 deletion src/components/__tests__/CategoryStatus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ describe('CategoryStatus', () => {
},
},
});
expect(wrapper.find('h1').text()).toBe('In Total 2 Veggies');
expect(wrapper.find('h1').text()).toBe('In Total 2 Actions');
});
});
56 changes: 26 additions & 30 deletions src/components/__tests__/VeggieSearch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@ import {describe, it, expect} from 'vitest';
import {mount} from '@vue/test-utils';
import VeggieSearch from '@/components/VeggieSearch.vue';
import IconComponent from '@/components/IconComponent.vue';
import {KEYS} from '@/utils/constants';

const mounter = (modelValue: string[] = [], inject?: string) =>
mount(VeggieSearch, {
props: {
modelValue,
},
global: {
provide: {
[KEYS.challenge]: inject,
},
},
});

describe('VeggieSearch', () => {
it('renders', () => {
const wrapper = mount(VeggieSearch, {
props: {
modelValue: [],
},
});
const wrapper = mounter();
expect(wrapper).toBeTruthy();
});

it('filters veggies', async () => {
const wrapper = mount(VeggieSearch, {
props: {
modelValue: [],
},
});
const wrapper = mounter();
const input = wrapper.find('.veggie-search__input');
await input.setValue('tomato');
expect(wrapper.find('.veggie-search__options').isVisible()).toBe(true);
Expand All @@ -27,35 +32,23 @@ describe('VeggieSearch', () => {
});

it('shows all categories with matches', async () => {
const wrapper = mount(VeggieSearch, {
props: {
modelValue: [],
},
});
const wrapper = mounter();
const input = wrapper.find('.veggie-search__input');
await input.setValue('bar');
expect(wrapper.findAll('.veggie-search__option').length).toBe(2);
expect(wrapper.findAll('.veggie-search__heading').length).toBe(2);
});

it('displays no results', async () => {
const wrapper = mount(VeggieSearch, {
props: {
modelValue: [],
},
});
const wrapper = mounter();
const input = wrapper.find('.veggie-search__input');
await input.setValue('test');
expect(wrapper.find('.veggie-search__option').exists()).toBe(false);
expect(wrapper.find('.veggie-search__no-results').isVisible()).toBe(true);
});

it('shows selection', async () => {
const wrapper = mount(VeggieSearch, {
props: {
modelValue: ['tomato'],
},
});
const wrapper = mounter(['tomato']);
const input = wrapper.find('.veggie-search__input');
await input.setValue('om');
const listItem = wrapper.findByText('li', 'tomato');
Expand All @@ -65,12 +58,15 @@ describe('VeggieSearch', () => {
});

it('shows list on button click', async () => {
const wrapper = mount(VeggieSearch, {
props: {
modelValue: [],
},
});
const wrapper = mounter();
await wrapper.find('.veggie-search__button').trigger('click');
expect(wrapper.find('.veggie-search__options').isVisible()).toBe(true);
expect(wrapper.find('#veggie-search-heading-challenge').exists()).toBe(false);
});

it('shows challenge if available', async () => {
const wrapper = mounter([], 'raspberry');
await wrapper.find('.veggie-search__button').trigger('click');
expect(wrapper.find('#veggie-search-heading-challenge').isVisible()).toBe(true);
});
});
Loading