Skip to content

Commit

Permalink
[Issue#2015] updated create child endpoint (#2033)
Browse files Browse the repository at this point in the history
Co-authored-by: Viktor Patraboi <vpatr@softserveinc.com>
  • Loading branch information
okedo and Viktor Patraboi authored Mar 10, 2023
1 parent 84da353 commit 68eb43d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/app/shared/enum/enumUA/messageBer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export enum SnackbarText {
blockAdmin = 'SERVICE_MESSAGES.SNACK_BAR_TEXT.BLOCK_ADMIN',

createChild = 'SERVICE_MESSAGES.SNACK_BAR_TEXT.CREATE_CHILD',
createChildren = 'SERVICE_MESSAGES.SNACK_BAR_TEXT.CREATE_CHILDREN',
updateChild = 'SERVICE_MESSAGES.SNACK_BAR_TEXT.UPDATE_CHILD',
deleteChild = 'SERVICE_MESSAGES.SNACK_BAR_TEXT.DELETE_CHILD',

Expand Down
10 changes: 9 additions & 1 deletion src/app/shared/services/children/children.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,21 @@ export class ChildrenService {
}

/**
* This method create Child
* This method create single Child
* @param child: Child
*/
createChild(child: Child): Observable<Child> {
return this.http.post<Child>('/api/v1/Child/Create', child);
}

/**
* This method create array of Child
* @param children: Child[]
*/
public createChildren(children: Child[]): Observable<Child[]> {
return this.http.post<Child[]>('/api/v1/Child/CreateChildren', children);
}

/**
* This method update Child
* @param child: Child
Expand Down
19 changes: 17 additions & 2 deletions src/app/shared/store/parent.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,24 @@ export class GetAllUsersChildrenByParentId {
constructor(public payload: RequestParams) {}
}

export class CreateChild {
static readonly type = '[parent] create Child';
constructor(public payload: Child) {}
}

export class OnCreateChildFail {
static readonly type = '[parent] create Child fail';
constructor(public payload: HttpErrorResponse) {}
}

export class OnCreateChildSuccess {
static readonly type = '[parent] create Child success';
constructor() {}
}

export class CreateChildren {
static readonly type = '[parent] create Children';
constructor(public payload: Child) {}
constructor(public payload: Child[]) {}
}

export class OnCreateChildrenFail {
Expand All @@ -73,7 +88,7 @@ export class OnCreateChildrenFail {

export class OnCreateChildrenSuccess {
static readonly type = '[parent] create Children success';
constructor() {}
constructor(public multipleChildren?: boolean) {}
}

export class DeleteChildById {
Expand Down
23 changes: 18 additions & 5 deletions src/app/shared/store/parent.state..ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { MarkFormDirty, ShowMessageBar } from './app.actions';
import {
CreateApplication,
CreateChildren,
CreateChild,
CreateFavoriteWorkshop,
CreateRating,
DeleteChildById,
Expand All @@ -31,6 +32,8 @@ import {
OnCreateApplicationSuccess,
OnCreateChildrenFail,
OnCreateChildrenSuccess,
OnCreateChildFail,
OnCreateChildSuccess,
OnCreateRatingFail,
OnCreateRatingSuccess,
OnDeleteChildFail,
Expand Down Expand Up @@ -275,10 +278,19 @@ export class ParentState {
this.location.back();
}

@Action(CreateChildren)
createChildren({ dispatch }: StateContext<ParentStateModel>, { payload }: CreateChildren): Observable<Observable<void> | Child> {
@Action(CreateChild)
createChild({ dispatch }: StateContext<ParentStateModel>, { payload }: CreateChild): Observable<Observable<void> | Child> {
return this.childrenService.createChild(payload).pipe(
tap(() => dispatch(new OnCreateChildrenSuccess())),
tap(() => dispatch(new OnCreateChildSuccess())),
catchError((error: HttpErrorResponse) => of(dispatch(new OnCreateChildFail(error))))
);
}

@Action(CreateChildren)
createChildren({ dispatch }: StateContext<ParentStateModel>, { payload }: CreateChildren): Observable<Observable<void> | Child[]> {
const multipleChildren = payload.length > 1;
return this.childrenService.createChildren(payload).pipe(
tap(() => dispatch(new OnCreateChildrenSuccess(multipleChildren))),
catchError((error: HttpErrorResponse) => of(dispatch(new OnCreateChildrenFail(error))))
);
}
Expand All @@ -289,10 +301,11 @@ export class ParentState {
}

@Action(OnCreateChildrenSuccess)
onCreateChildrenSuccess({ dispatch }: StateContext<ParentStateModel>, {}: OnCreateChildrenSuccess): void {
onCreateChildrenSuccess({ dispatch }: StateContext<ParentStateModel>, { multipleChildren }: OnCreateChildrenSuccess): void {
const message = multipleChildren ? SnackbarText.createChildren : SnackbarText.createChild;
dispatch([
new ShowMessageBar({
message: SnackbarText.createChild,
message,
type: 'success'
}),
new MarkFormDirty(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,8 @@ export class CreateChildComponent extends CreateFormComponent implements OnInit,
const child: Child = new Child(this.ChildrenFormArray.controls[0].value, parent.id, this.child.id);
this.store.dispatch(new UpdateChild(child));
} else {
this.ChildrenFormArray.controls.forEach((form: FormGroup) => {
const child: Child = new Child(form.value, parent.id);
this.store.dispatch(new CreateChildren(child));
});
const controlsData = this.ChildrenFormArray.controls.map((form: FormGroup) => new Child(form.value, parent.id));
this.store.dispatch(new CreateChildren(controlsData));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@
"DELETE_ADMIN": "Administrator deleted!",
"BLOCK_ADMIN": "The administrator is blocked!",
"CREATE_CHILD": "Thank you! Child has been successfully added.",
"CREATE_CHILDREN": "Thank you! Children have been successfully added.",
"UPDATE_CHILD": "Child edited successfully",
"DELETE_CHILD": "Child's data has been successfully deleted!",
"CREATE_ACHIEVEMENT": "New achievement added!",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/uk.json
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@
"DELETE_ADMIN": "Адміністратора видалено!",
"BLOCK_ADMIN": "Адміністратора заблоковано!",
"CREATE_CHILD": "Дякуємо! Дитина була успішно додана.",
"CREATE_CHILDREN": "Дякуємо! Діти були успішно додані.",
"UPDATE_CHILD": "Дитина успішно відредагована",
"DELETE_CHILD": "Дані дитини усіпшно видалено!",
"CREATE_ACHIEVEMENT": "Нове досягнення додано!",
Expand Down

0 comments on commit 68eb43d

Please sign in to comment.