Client - get workouts filters from route query

This commit is contained in:
Sam 2021-11-02 14:18:00 +01:00
parent 37596f52f4
commit 6c6a024ad6
3 changed files with 44 additions and 30 deletions

View File

@ -5,18 +5,32 @@
<div class="form-items-group"> <div class="form-items-group">
<div class="form-item"> <div class="form-item">
<label> {{ $t('workouts.FROM') }}: </label> <label> {{ $t('workouts.FROM') }}: </label>
<input name="from" type="date" @change="handleFilterChange" /> <input
name="from"
type="date"
:value="$route.query.from"
@change="handleFilterChange"
/>
</div> </div>
<div class="form-item"> <div class="form-item">
<label> {{ $t('workouts.TO') }}: </label> <label> {{ $t('workouts.TO') }}: </label>
<input name="to" type="date" @change="handleFilterChange" /> <input
name="to"
type="date"
:value="$route.query.to"
@change="handleFilterChange"
/>
</div> </div>
</div> </div>
<div class="form-items-group"> <div class="form-items-group">
<div class="form-item"> <div class="form-item">
<label> {{ $t('workouts.SPORT', 1) }}:</label> <label> {{ $t('workouts.SPORT', 1) }}:</label>
<select name="sport_id" @change="handleFilterChange"> <select
name="sport_id"
:value="$route.query.sport_id"
@change="handleFilterChange"
>
<option value="" /> <option value="" />
<option <option
v-for="sport in translatedSports.filter((s) => v-for="sport in translatedSports.filter((s) =>
@ -40,6 +54,7 @@
type="number" type="number"
min="0" min="0"
step="1" step="1"
:value="$route.query.distance_from"
@change="handleFilterChange" @change="handleFilterChange"
/> />
<span>{{ $t('workouts.TO') }}</span> <span>{{ $t('workouts.TO') }}</span>
@ -48,6 +63,7 @@
type="number" type="number"
min="0" min="0"
step="1" step="1"
:value="$route.query.distance_to"
@change="handleFilterChange" @change="handleFilterChange"
/> />
</div> </div>
@ -60,6 +76,7 @@
<div class="form-inputs-group"> <div class="form-inputs-group">
<input <input
name="duration_from" name="duration_from"
:value="$route.query.duration_from"
@change="handleFilterChange" @change="handleFilterChange"
pattern="^([0-9]*[0-9]):([0-5][0-9])$" pattern="^([0-9]*[0-9]):([0-5][0-9])$"
placeholder="hh:mm" placeholder="hh:mm"
@ -68,6 +85,7 @@
<span>{{ $t('workouts.TO') }}</span> <span>{{ $t('workouts.TO') }}</span>
<input <input
name="duration_to" name="duration_to"
:value="$route.query.duration_to"
@change="handleFilterChange" @change="handleFilterChange"
pattern="^([0-9]*[0-9]):([0-5][0-9])$" pattern="^([0-9]*[0-9]):([0-5][0-9])$"
placeholder="hh:mm" placeholder="hh:mm"
@ -84,6 +102,7 @@
<input <input
min="0" min="0"
name="ave_speed_from" name="ave_speed_from"
:value="$route.query.ave_speed_from"
@change="handleFilterChange" @change="handleFilterChange"
step="1" step="1"
type="number" type="number"
@ -92,6 +111,7 @@
<input <input
min="0" min="0"
name="ave_speed_to" name="ave_speed_to"
:value="$route.query.ave_speed_to"
@change="handleFilterChange" @change="handleFilterChange"
step="1" step="1"
type="number" type="number"
@ -108,6 +128,7 @@
<input <input
min="0" min="0"
name="max_speed_from" name="max_speed_from"
:value="$route.query.max_speed_from"
@change="handleFilterChange" @change="handleFilterChange"
step="1" step="1"
type="number" type="number"
@ -116,6 +137,7 @@
<input <input
min="0" min="0"
name="max_speed_to" name="max_speed_to"
:value="$route.query.max_speed_to"
@change="handleFilterChange" @change="handleFilterChange"
step="1" step="1"
type="number" type="number"
@ -137,6 +159,7 @@
<script lang="ts"> <script lang="ts">
import { computed, ComputedRef, defineComponent, PropType } from 'vue' import { computed, ComputedRef, defineComponent, PropType } from 'vue'
import { useI18n } from 'vue-i18n' import { useI18n } from 'vue-i18n'
import { useRouter } from 'vue-router'
import { ISport } from '@/types/sports' import { ISport } from '@/types/sports'
import { IUserProfile } from '@/types/user' import { IUserProfile } from '@/types/user'
@ -154,9 +177,11 @@
required: true, required: true,
}, },
}, },
emits: ['filter', 'filtersUpdate'], emits: ['filter'],
setup(props, { emit }) { setup(props, { emit }) {
const { t } = useI18n() const { t } = useI18n()
const router = useRouter()
const translatedSports: ComputedRef<ISport[]> = computed(() => const translatedSports: ComputedRef<ISport[]> = computed(() =>
translateSports(props.sports, t) translateSports(props.sports, t)
) )
@ -170,7 +195,8 @@
} }
} }
function onFilter() { function onFilter() {
emit('filter', { ...params }) emit('filter')
router.push({ path: '/workouts', query: params })
} }
return { translatedSports, onFilter, handleFilterChange } return { translatedSports, onFilter, handleFilterChange }

View File

@ -108,6 +108,7 @@
capitalize, capitalize,
onBeforeMount, onBeforeMount,
} from 'vue' } from 'vue'
import { LocationQuery, useRoute } from 'vue-router'
import StaticMap from '@/components/Common/StaticMap.vue' import StaticMap from '@/components/Common/StaticMap.vue'
import NoWorkouts from '@/components/Workouts/NoWorkouts.vue' import NoWorkouts from '@/components/Workouts/NoWorkouts.vue'
@ -126,10 +127,6 @@
StaticMap, StaticMap,
}, },
props: { props: {
params: {
type: Object as PropType<Record<string, string>>,
required: true,
},
user: { user: {
type: Object as PropType<IUserProfile>, type: Object as PropType<IUserProfile>,
required: true, required: true,
@ -138,8 +135,10 @@
type: Object as PropType<ITranslatedSport[]>, type: Object as PropType<ITranslatedSport[]>,
}, },
}, },
setup(props) { setup() {
const store = useStore() const store = useStore()
const route = useRoute()
const workouts: ComputedRef<IWorkout[]> = computed( const workouts: ComputedRef<IWorkout[]> = computed(
() => store.getters[WORKOUTS_STORE.GETTERS.USER_WORKOUTS] () => store.getters[WORKOUTS_STORE.GETTERS.USER_WORKOUTS]
) )
@ -147,23 +146,23 @@
const page = ref(1) const page = ref(1)
onBeforeMount(() => { onBeforeMount(() => {
loadWorkouts() loadWorkouts(route.query)
}) })
function loadWorkouts() { function loadWorkouts(newQuery: LocationQuery) {
page.value = 1 page.value = 1
store.dispatch(WORKOUTS_STORE.ACTIONS.GET_USER_WORKOUTS, { store.dispatch(WORKOUTS_STORE.ACTIONS.GET_USER_WORKOUTS, {
page: page.value, page: page.value,
per_page, per_page,
...defaultOrder, ...defaultOrder,
...props.params, ...newQuery,
}) })
} }
watch( watch(
() => props.params, () => route.query,
async () => { async (newQuery) => {
loadWorkouts() loadWorkouts(newQuery)
} }
) )

View File

@ -5,7 +5,7 @@
<WorkoutsFilters <WorkoutsFilters
:sports="translatedSports" :sports="translatedSports"
:authUser="authUser" :authUser="authUser"
@filter="updateParams" @filter="toggleFilters"
/> />
</div> </div>
<div class="display-filters"> <div class="display-filters">
@ -20,18 +20,14 @@
</div> </div>
</div> </div>
<div class="list-container"> <div class="list-container">
<WorkoutsList <WorkoutsList :user="authUser" :sports="translatedSports" />
:user="authUser"
:params="params"
:sports="translatedSports"
/>
</div> </div>
</div> </div>
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts">
import { ComputedRef, Ref, computed, defineComponent, ref } from 'vue' import { ComputedRef, computed, defineComponent, ref } from 'vue'
import { useI18n } from 'vue-i18n' import { useI18n } from 'vue-i18n'
import WorkoutsFilters from '@/components/Workouts/WorkoutsFilters.vue' import WorkoutsFilters from '@/components/Workouts/WorkoutsFilters.vue'
@ -60,13 +56,8 @@
const translatedSports: ComputedRef<ITranslatedSport[]> = computed(() => const translatedSports: ComputedRef<ITranslatedSport[]> = computed(() =>
translateSports(sports.value, t) translateSports(sports.value, t)
) )
const params: Ref<Record<string, string>> = ref({})
const hiddenFilters = ref(true) const hiddenFilters = ref(true)
function updateParams(filters: Record<string, string>) {
params.value = filters
hiddenFilters.value = true
}
function toggleFilters() { function toggleFilters() {
hiddenFilters.value = !hiddenFilters.value hiddenFilters.value = !hiddenFilters.value
} }
@ -74,10 +65,8 @@
return { return {
authUser, authUser,
hiddenFilters, hiddenFilters,
params,
translatedSports, translatedSports,
toggleFilters, toggleFilters,
updateParams,
} }
}, },
}) })