Skip to content

Commit

Permalink
Merge branch 'main' into feature/III-5746
Browse files Browse the repository at this point in the history
  • Loading branch information
brampauwelyn committed Oct 10, 2023
2 parents 3c25b2f + b77d717 commit 88a167d
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 55 deletions.
19 changes: 19 additions & 0 deletions src/hooks/api/organizers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,26 @@ const useDeleteOrganizerEducationalDescriptionMutation = (
...configuration,
});

const changeLocation = async ({ headers, organizerId, language, location }) => {
return fetchFromApi({
path: `/organizers/${organizerId.toString()}/address/${language}`,
options: {
method: 'PUT',
headers,
body: JSON.stringify(location),
},
});
};

const useChangeLocationMutation = (configuration = {}) =>
useAuthenticatedMutation({
mutationFn: changeLocation,
mutationKey: 'organizer-change-location',
...configuration,
});

export {
useChangeLocationMutation,
useCreateOrganizerMutation,
useDeleteOrganizerByIdMutation,
useDeleteOrganizerEducationalDescriptionMutation,
Expand Down
45 changes: 23 additions & 22 deletions src/pages/organizers/create/OrganizerForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import { locationStepConfiguration } from '@/pages/steps/LocationStep';
import { Steps, StepsConfiguration } from '@/pages/steps/Steps';
import { Organizer } from '@/types/Organizer';
import { Button, ButtonVariants } from '@/ui/Button';
import { Inline } from '@/ui/Inline';
import { Link, LinkVariants } from '@/ui/Link';
import { Page } from '@/ui/Page';
import { Text } from '@/ui/Text';
import { getValueFromTheme } from '@/ui/theme';
import { getLanguageObjectOrFallback } from '@/utils/getLanguageObjectOrFallback';

import { NameAndUrlStep } from './steps/NameAndUrlStep';
Expand All @@ -41,6 +45,8 @@ const typeAndThemeStepConfiguration: StepsConfiguration<'nameAndUrl'> = {
}),
};

const getValue = getValueFromTheme('createPage');

const configurations = [
typeAndThemeStepConfiguration,
{
Expand All @@ -52,10 +58,11 @@ const configurations = [
variant: AdditionalInformationStepVariant.ORGANIZER,
name: 'location' as StepsConfiguration['name'],
defaultValue: locationStepConfiguration.defaultValue,
validation: locationStepConfiguration.validation,
},
];

const OrganizerForm = (props) => {
const OrganizerForm = () => {
const scope = ScopeTypes.ORGANIZERS;
const { form } = useParseStepConfiguration(configurations);
const { t, i18n } = useTranslation();
Expand Down Expand Up @@ -89,8 +96,6 @@ const OrganizerForm = (props) => {
};
};

// const toast = useToast(toastConfiguration);

// TODO better type query
const getOrganizerByIdQuery = useGetOrganizerByIdQuery(
{ id: urlOrganizerId },
Expand Down Expand Up @@ -121,18 +126,7 @@ const OrganizerForm = (props) => {

if (urlOrganizerId) {
mutation = updateOrganizerMutation;
attributes = {
...attributes,
organizerId: urlOrganizerId,
address: {
[i18n.language]: {
addressCountry: getValues('location.country'),
addressLocality: getValues('location.municipality.name'),
postalCode: getValues('location.municipality.zip'),
streetAddress: getValues('location.streetAndNumber'),
},
},
};
attributes = { ...attributes, organizerId: urlOrganizerId };
}

const { organizerId } = await mutation.mutateAsync(attributes);
Expand Down Expand Up @@ -162,17 +156,24 @@ const OrganizerForm = (props) => {
configurations={configurations}
form={form}
labels={organizerLabels}
onChange={() => {
if (urlOrganizerId) onSuccess();
}}
/>
</Page.Content>
<Page.Footer>
{urlOrganizerId ? (
<Button
disabled={hasErrors}
variant={ButtonVariants.PRIMARY}
onClick={handleSubmit(onSuccess)}
>
{t('organizers.create.step2.save')}
</Button>
<Inline spacing={3} alignItems="center">
<Link
href={`/organizer/${urlOrganizerId}/preview`}
variant={LinkVariants.BUTTON_SUCCESS}
>
<Text>{t('create.footer.done_editing')}</Text>
</Link>
<Text color={getValue('footer.color')} fontSize="0.9rem">
{t('create.footer.auto_save')}
</Text>
</Inline>
) : (
<Button
disabled={hasErrors || !formState.isDirty}
Expand Down
38 changes: 13 additions & 25 deletions src/pages/organizers/create/steps/NameAndUrlStep.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
import { Controller } from 'react-hook-form';

import { getStepProps } from '@/pages/steps/Steps';
import { getStepProps, StepProps } from '@/pages/steps/Steps';
import { parseSpacing } from '@/ui/Box';
import { Stack } from '@/ui/Stack';

import { NameStep } from './NameStep';
import { UrlStep } from './UrlStep';

const NameAndUrlStep = ({
control,
name,
onChange,
shouldHideType,
...props
}: any) => {
return (
<Controller
name={name}
control={control}
render={() => {
return (
<Stack spacing={4} maxWidth={parseSpacing(9)}>
<NameStep {...getStepProps(props)} name={name} control={control} />
<UrlStep {...getStepProps(props)} name={name} control={control} />
</Stack>
);
}}
/>
);
};

NameAndUrlStep.defaultProps = {};
const NameAndUrlStep = ({ control, name, ...props }: StepProps) => (
<Controller
name={name}
control={control}
render={() => (
<Stack spacing={4} maxWidth={parseSpacing(9)}>
<NameStep {...getStepProps(props)} name={name} control={control} />
<UrlStep {...getStepProps(props)} name={name} control={control} />
</Stack>
)}
/>
);

export { NameAndUrlStep };
8 changes: 4 additions & 4 deletions src/pages/organizers/create/steps/NameStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ const NameStep = ({
...field.value,
name: (event.target as HTMLInputElement).value,
});
// onChange({
// ...field.value,
// name: (event.target as HTMLInputElement).value,
// });
onChange({
...field.value,
name: (event.target as HTMLInputElement).value,
});
}}
/>
}
Expand Down
5 changes: 4 additions & 1 deletion src/pages/organizers/create/steps/UrlStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ const UrlStep = ({
existingOrganizer &&
parseOfferId(existingOrganizer['@id']) !== query.organizerId
) {
console.log('should set error');
setError('nameAndUrl.url', { type: 'not_unique' });
return;
}
Expand Down Expand Up @@ -94,6 +93,10 @@ const UrlStep = ({
...field.value,
url: prefixUrlWithHttps(newValue),
});
onChange({
...field.value,
url: prefixUrlWithHttps(newValue),
});
}}
/>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { useEffect } from 'react';
import { useTranslation } from 'react-i18next';

import { useChangeLocationMutation } from '@/hooks/api/organizers';
import {
TabContentProps,
ValidationStatus,
Expand All @@ -15,6 +17,9 @@ function PhysicalLocationStep({
...props
}: PhysicalLocationStepProps) {
const location = props.watch('location');
const changeLocation = useChangeLocationMutation();
const { i18n } = useTranslation();

useEffect(() => {
onValidationChange(
location?.streetAndNumber
Expand All @@ -23,7 +28,25 @@ function PhysicalLocationStep({
);
}, [onValidationChange, location]);

return <LocationStep {...props} onChange={props.onSuccessfulChange} />;
const onChange = (updatedLocation: typeof location) => {
props.onSuccessfulChange(updatedLocation);
if (!updatedLocation.streetAndNumber) {
return;
}

changeLocation.mutate({
organizerId: props.offerId,
language: i18n.language,
location: {
addressCountry: updatedLocation?.country,
addressLocality: updatedLocation?.municipality?.name,
postalCode: updatedLocation?.municipality?.zip,
streetAddress: updatedLocation?.streetAndNumber,
},
});
};

return <LocationStep {...props} onChange={onChange} />;
}

export { PhysicalLocationStep };
4 changes: 4 additions & 0 deletions src/pages/steps/LocationStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,10 @@ const locationStepConfiguration: StepsConfiguration<'location'> = {
validation: yup.lazy((value) => {
const url = window.location.href;

if (url.includes('organizers') && url.includes('create')) {
return yup.object();
}

const scope = url.includes('events')
? OfferTypes.EVENTS
: OfferTypes.PLACES;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/steps/Steps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ const Steps = ({
};

return (
<Stack {...getStackProps(props)} spacing={5} width="100%">
<Stack spacing={5} width="100%" {...getStackProps(props)}>
{configurationsWithComponent.map(
(
{
Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/create-organizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ test('create an organizer', async ({ baseURL, page }) => {

await page.getByText('100/100').click();

await page.getByRole('button', { name: 'Bewaren' }).click();
await page.getByRole('button', { name: 'Klaar met bewerken' }).click();
});

0 comments on commit 88a167d

Please sign in to comment.