Skip to content

Commit

Permalink
feat/updated form composable
Browse files Browse the repository at this point in the history
  • Loading branch information
serikovlearning committed May 9, 2024
1 parent 4d6e944 commit 83c8535
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
9 changes: 5 additions & 4 deletions frontend-admin/src/lib/components/auth-form/index.svelte
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<script lang="ts">
import 'carbon-components-svelte/css/white.css';
import { onMount } from 'svelte';
import { Button, TextInput, Loading, InlineNotification } from 'carbon-components-svelte';
import Card from '$lib/components/card/index.svelte';
import { onMount } from 'svelte';
import { checkIsEmpty } from '$lib/utils/validations';
import { store } from './store';
import { initialValues } from './consts';
import { handleInputChange } from './utils';
import { checkIsEmpty } from '$lib/utils/validations';
let isLoading = false;
let errorMessage: string | undefined;
let username = '';
let password = '';
let username = initialValues.username;
let password = initialValues.password;
let isUsernameValid = true;
let isPasswordValid = true;
let isFormHasChanges = false;
Expand Down
8 changes: 8 additions & 0 deletions frontend-admin/src/lib/components/auth-form/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ export const store = () => {
request: authRequest
});


/** TODO:
* 1. Придумать как принимать валидацию внутри composable
* 2. Придумать как автоматически обновлять поля и валидировать их,
* а не писать каждый раз руками
* 3. Придумать как обрабатывать non-required и required кейсы
*/
const setUsername = (value: string) => {
formState.update((state) => ({
...state,
Expand All @@ -29,6 +36,7 @@ export const store = () => {
}
}));
};

const setPassword = (value: string) => {
formState.update((state) => ({
...state,
Expand Down
12 changes: 10 additions & 2 deletions frontend-admin/src/lib/composable/form-composable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const formComposable = <
request,
onSuccess
}: TFormComposableParams<TResponseParams, TFields, TFields>) => {
const abortController = new AbortController();

const extendedValues = extendValuesWithMeta(initialValues);
const formState = writable(extendedValues);

Expand All @@ -25,7 +27,13 @@ export const formComposable = <
const setIsLoading = (loading: boolean) =>
formState.update((state) => ({ ...state, isLoading: loading }));

const abortController = new AbortController();
const handleSuccess: TFormComposableParams<TResponseParams, TFields, TFields>['onSuccess'] = (
data
) => {
onSuccess(data);
formState.set(extendedValues);
};

const handleSubmit = async () => {
const requestParams: TFields = {} as TFields;

Expand All @@ -42,7 +50,7 @@ export const formComposable = <
setLoadingState: setIsLoading,
onError: setError,
requestParams,
onSuccess,
onSuccess: handleSuccess,
request,
abortController
});
Expand Down

0 comments on commit 83c8535

Please sign in to comment.