diff --git a/{{cookiecutter.project_slug}}/clients/mobile/react-native/src/services/user/api.ts b/{{cookiecutter.project_slug}}/clients/mobile/react-native/src/services/user/api.ts index 5481e9267..05e4b227d 100644 --- a/{{cookiecutter.project_slug}}/clients/mobile/react-native/src/services/user/api.ts +++ b/{{cookiecutter.project_slug}}/clients/mobile/react-native/src/services/user/api.ts @@ -1,7 +1,7 @@ import { createApi, createCustomServiceCall } from '@thinknimble/tn-models' import { z } from 'zod' import { axiosInstance } from '../axios-instance' -import { forgotPasswordShape, loginShape, userCreateShape, userShape } from './models' +import { forgotPasswordShape, loginShape, partialUserShape, userCreateShape, userShape } from './models' const login = createCustomServiceCall({ inputShape: loginShape, @@ -34,6 +34,16 @@ const logout = createCustomServiceCall(async ({ client }) => { return client.post(`/logout/`) }) +const patchUser = createCustomServiceCall({ + inputShape: partialUserShape, + outputShape: userShape, + cb: async ({ client, slashEndingBaseUri, input, utils }) => { + const { id, ...body } = utils.toApi(input) + const res = await client.put(`${slashEndingBaseUri}${id}/`, body) + return utils.fromApi(res.data) + }, +}) + export const userApi = createApi({ client: axiosInstance, baseUri: '/users/', @@ -41,5 +51,5 @@ export const userApi = createApi({ create: userCreateShape, entity: userShape, }, - customCalls: { login, requestPasswordResetCode, resetPassword, logout }, + customCalls: { login, requestPasswordResetCode, resetPassword, logout, patchUser }, }) diff --git a/{{cookiecutter.project_slug}}/clients/mobile/react-native/src/services/user/models.ts b/{{cookiecutter.project_slug}}/clients/mobile/react-native/src/services/user/models.ts index ba4c4ae5c..21c661244 100644 --- a/{{cookiecutter.project_slug}}/clients/mobile/react-native/src/services/user/models.ts +++ b/{{cookiecutter.project_slug}}/clients/mobile/react-native/src/services/user/models.ts @@ -38,3 +38,24 @@ export const loginShape = { } export type LoginShape = GetInferredFromRaw + +type ZodOptionalRecord = { + [TKey in keyof T]: z.ZodOptional +} + +export const zodPartialize = (zShape: T) => { + return Object.fromEntries( + Object.entries(zShape).map(([k, v]) => { + return [k, v.optional()] + }), + ) as ZodOptionalRecord +} + +export const fullNameZod = z.string().refine( + (value) => { + return value.split(' ').filter(Boolean).length >= 2 + }, + { message: 'Please provide a full name (first and last name)' }, +) + +export const partialUserShape = { ...zodPartialize(userShape), id: z.string().uuid() }