Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Commit

Permalink
Merge pull request #292 from City-of-Helsinki/TILA-1391
Browse files Browse the repository at this point in the history
TILA-1391 filter by units
  • Loading branch information
kieferbonk authored Apr 29, 2022
2 parents d1c9cad + b1f92a9 commit 5ff6867
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 31 deletions.
25 changes: 23 additions & 2 deletions ui/components/application/Page1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { useForm } from "react-hook-form";
import { useQuery } from "@apollo/client";
import { sortBy } from "lodash";
import { sortBy, uniq } from "lodash";
import { useRouter } from "next/router";
import ApplicationEvent from "../applicationEvent/ApplicationEvent";
import {
Expand All @@ -24,7 +24,10 @@ import {
ApplicationRoundType,
ReservationUnitType,
} from "../../modules/gql-types";
import { RESERVATION_PURPOSES } from "../../modules/queries/params";
import {
RESERVATION_PURPOSES,
SEARCH_FORM_PARAMS_UNIT,
} from "../../modules/queries/params";

type Props = {
applicationRound: ApplicationRoundType;
Expand Down Expand Up @@ -62,13 +65,29 @@ const Page1 = ({
const [options, setOptions] = useState<OptionTypes>();

const [purposeOptions, setPurposeOptions] = useState<OptionType[]>([]);
const [unitOptions, setUnitOptions] = useState<OptionType[]>([]);

const history = useRouter();

const { t } = useTranslation();

const { application } = editorState;

useQuery<Query>(SEARCH_FORM_PARAMS_UNIT, {
onCompleted: (res) => {
const unitsInApplicationRound = uniq(
applicationRound.reservationUnits.flatMap((resUnit) => resUnit.unit.pk)
);
const units = res?.units?.edges
?.filter(({ node }) => unitsInApplicationRound.includes(node.pk))
.map(({ node }) => ({
id: String(node.pk),
name: getTranslation(node, "name"),
}));
setUnitOptions(mapOptions(sortBy(units, "name") as StringParameter[]));
},
});

useQuery<Query>(RESERVATION_PURPOSES, {
onCompleted: (res) => {
const purposes = res?.reservationPurposes?.edges?.map(({ node }) => ({
Expand All @@ -79,6 +98,7 @@ const Page1 = ({
mapOptions(sortBy(purposes, "name") as StringParameter[])
);
},
skip: unitOptions.length < 1,
});

const form = useForm({
Expand Down Expand Up @@ -218,6 +238,7 @@ const Page1 = ({
optionTypes={{
...options,
purposeOptions,
unitOptions,
}}
selectedReservationUnits={selectedReservationUnits}
onSave={form.handleSubmit((app: Application) =>
Expand Down
3 changes: 3 additions & 0 deletions ui/components/applicationEvent/ApplicationEvent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type OptionTypes = {
abilityGroupOptions: OptionType[];
reservationUnitTypeOptions: OptionType[];
participantCountOptions: OptionType[];
unitOptions: OptionType[];
};

type Props = {
Expand Down Expand Up @@ -171,6 +172,7 @@ const ApplicationEvent = ({
purposeOptions,
reservationUnitTypeOptions,
participantCountOptions,
unitOptions,
} = optionTypes;

const { t, i18n } = useTranslation();
Expand Down Expand Up @@ -307,6 +309,7 @@ const ApplicationEvent = ({
purposeOptions,
reservationUnitTypeOptions,
participantCountOptions,
unitOptions,
}}
/>
<SubHeadLine>
Expand Down
1 change: 1 addition & 0 deletions ui/components/reservation-unit/ReservationUnitList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type OptionTypes = {
purposeOptions: OptionType[];
reservationUnitTypeOptions: OptionType[];
participantCountOptions: OptionType[];
unitOptions: OptionType[];
};

type Props = {
Expand Down
15 changes: 15 additions & 0 deletions ui/components/reservation-unit/ReservationUnitModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ type OptionsType = {
purposeOptions: OptionType[];
reservationUnitTypeOptions: OptionType[];
participantCountOptions: OptionType[];
unitOptions: OptionType[];
};

const emptyOption = {
Expand All @@ -291,6 +292,7 @@ const ReservationUnitModal = ({
const [reservationUnitType, setReservationUnitType] = useState<
OptionType | undefined
>(undefined);
const [unit, setUnit] = useState<OptionType | undefined>(undefined);
const [results, setResults] = useState<ReservationUnitType[]>([]);
const [maxPersons, setMaxPersons] = useState<OptionType | undefined>(
undefined
Expand All @@ -304,6 +306,8 @@ const ReservationUnitModal = ({
options.participantCountOptions
);

const unitOptions = [emptyOption].concat(options.unitOptions);

const { t } = useTranslation();

const { data, refetch, loading } = useQuery<Query, QueryReservationUnitsArgs>(
Expand All @@ -316,6 +320,7 @@ const ReservationUnitModal = ({
? [reservationUnitType?.value?.toString()]
: [],
applicationRound: [applicationRound.pk.toString()],
unit: unit?.value ? [unit?.value?.toString()] : [],
orderBy: "nameFi",
isDraft: false,
isVisible: true,
Expand Down Expand Up @@ -370,6 +375,16 @@ const ReservationUnitModal = ({
}}
defaultValue={emptyOption}
/>
<Select
id="reservationUnitSearch.unit"
placeholder={t("common:select")}
options={unitOptions}
label={t("reservationUnitModal:searchReservationUnitTypeLabel")}
onChange={(selection: OptionType): void => {
setUnit(selection);
}}
defaultValue={emptyOption}
/>
</Filters>
<ButtonContainer>
<SearchButton
Expand Down
24 changes: 0 additions & 24 deletions ui/cypress/integration/applications.ts

This file was deleted.

60 changes: 60 additions & 0 deletions ui/mocks/handlers/applicationRound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ const applicationRoundsGQL = graphql.query<Query, QueryApplicationRoundsArgs>(
reservationUnits: [
{
pk: 2,
unit: {
pk: 3,
},
} as ReservationUnitType,
{
pk: 6,
unit: {
pk: 2,
},
} as ReservationUnitType,
],
allocating: false,
Expand Down Expand Up @@ -70,9 +76,15 @@ const applicationRoundsGQL = graphql.query<Query, QueryApplicationRoundsArgs>(
reservationUnits: [
{
pk: 7,
unit: {
pk: 5,
},
} as ReservationUnitType,
{
pk: 1,
unit: {
pk: 3,
},
} as ReservationUnitType,
],
allocating: false,
Expand Down Expand Up @@ -103,6 +115,9 @@ const applicationRoundsGQL = graphql.query<Query, QueryApplicationRoundsArgs>(
reservationUnits: [
{
pk: 7,
unit: {
pk: 5,
},
} as ReservationUnitType,
],
allocating: false,
Expand Down Expand Up @@ -133,12 +148,21 @@ const applicationRoundsGQL = graphql.query<Query, QueryApplicationRoundsArgs>(
reservationUnits: [
{
pk: 9,
unit: {
pk: 5,
},
} as ReservationUnitType,
{
pk: 6,
unit: {
pk: 5,
},
} as ReservationUnitType,
{
pk: 7,
unit: {
pk: 5,
},
} as ReservationUnitType,
],
allocating: false,
Expand Down Expand Up @@ -169,12 +193,21 @@ const applicationRoundsGQL = graphql.query<Query, QueryApplicationRoundsArgs>(
reservationUnits: [
{
pk: 9,
unit: {
pk: 5,
},
} as ReservationUnitType,
{
pk: 6,
unit: {
pk: 5,
},
} as ReservationUnitType,
{
pk: 7,
unit: {
pk: 5,
},
} as ReservationUnitType,
],
allocating: false,
Expand Down Expand Up @@ -202,12 +235,21 @@ const applicationRoundsGQL = graphql.query<Query, QueryApplicationRoundsArgs>(
reservationUnits: [
{
pk: 2,
unit: {
pk: 1,
},
} as ReservationUnitType,
{
pk: 6,
unit: {
pk: 2,
},
} as ReservationUnitType,
{
pk: 7,
unit: {
pk: 3,
},
} as ReservationUnitType,
],
allocating: false,
Expand Down Expand Up @@ -235,12 +277,21 @@ const applicationRoundsGQL = graphql.query<Query, QueryApplicationRoundsArgs>(
reservationUnits: [
{
pk: 9,
unit: {
pk: 5,
},
} as ReservationUnitType,
{
pk: 6,
unit: {
pk: 5,
},
} as ReservationUnitType,
{
pk: 7,
unit: {
pk: 5,
},
} as ReservationUnitType,
],
allocating: false,
Expand Down Expand Up @@ -271,12 +322,21 @@ const applicationRoundsGQL = graphql.query<Query, QueryApplicationRoundsArgs>(
reservationUnits: [
{
pk: 9,
unit: {
pk: 5,
},
} as ReservationUnitType,
{
pk: 6,
unit: {
pk: 5,
},
} as ReservationUnitType,
{
pk: 7,
unit: {
pk: 5,
},
} as ReservationUnitType,
],
allocating: false,
Expand Down
3 changes: 3 additions & 0 deletions ui/modules/queries/applicationRound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export const APPLICATION_ROUNDS = gql`
criteriaSv
reservationUnits {
pk
unit {
pk
}
}
}
}
Expand Down
Loading

0 comments on commit 5ff6867

Please sign in to comment.