Files
TheGame/packages/web/components/Setup/ProfileWizardPane.tsx
2023-02-21 09:39:39 -07:00

55 lines
1.3 KiB
TypeScript

import { ImageSources } from '@datamodels/identity-profile-basic';
import {
Maybe,
useInsertCacheInvalidationMutation,
} from 'graphql/autogen/types';
import {
ProfileValueType,
useProfileField,
useSaveCeramicProfile,
} from 'lib/hooks';
import React, { useCallback } from 'react';
import { WizardPane, WizardPaneProps } from './WizardPane';
export const ProfileWizardPane = <T extends ProfileValueType>({
field,
children,
...props
}: WizardPaneProps<T>) => {
const { value, user } = useProfileField<T>({
field,
});
const saveToCeramic = useSaveCeramicProfile({
fields: [field],
});
const [, invalidateCache] = useInsertCacheInvalidationMutation();
const onSave = useCallback(
async ({
values,
images,
setStatus,
}: {
values: Record<string, unknown>;
images: Record<string, Maybe<ImageSources>>;
setStatus: (msg: string) => void;
}) => {
setStatus('Saving to Ceramic…');
await saveToCeramic({ values, images });
if (user) {
setStatus('Invalidating Cache…');
await invalidateCache({ playerId: user.id });
}
},
[invalidateCache, saveToCeramic, user],
);
return (
<WizardPane<T> {...{ field, onSave, value, ...props }}>
{children}
</WizardPane>
);
};