Skip to content

Commit 463ea7a

Browse files
authored
feat: Admins can configure auto redirection after cloning from dashboard
AB#118658
1 parent 8499d0c commit 463ea7a

File tree

23 files changed

+827
-103
lines changed

23 files changed

+827
-103
lines changed

apps/back-office/src/app/app-preview/pages/form/form.component.html

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
<ng-container *ngIf="!loading && form">
2-
<shared-form [form]="form" (save)="onComplete($event)"></shared-form>
3-
<ng-container *ngIf="completed && !form.uniqueRecord && !hideNewRecord">
2+
<shared-form
3+
[form]="form"
4+
[record]="record"
5+
(save)="onComplete($event)"
6+
></shared-form>
7+
<!-- Form actions, only appear if not editing a record -->
8+
<ng-container *ngIf="completed && !record">
49
<div class="flex justify-center gap-x-4">
510
<!-- Action buttons -->
611
<shared-action-buttons [actionButtons]="actionButtons">
7-
<ui-button category="secondary" (click)="clearForm()">{{
8-
'models.record.new' | translate
9-
}}</ui-button>
12+
<!-- New record -->
13+
<ui-button
14+
*ngIf="!form.uniqueRecord && !hideNewRecord"
15+
category="secondary"
16+
(click)="clearForm()"
17+
>{{ 'models.record.new' | translate }}</ui-button
18+
>
1019
</shared-action-buttons>
1120
</div>
1221
</ng-container>

apps/back-office/src/app/app-preview/pages/form/form.component.ts

Lines changed: 88 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ import {
1414
ActionButton,
1515
ContextService,
1616
Record,
17+
RecordQueryResponse,
1718
} from '@oort-front/shared';
1819
import {
1920
GET_SHORT_FORM_BY_ID,
2021
GET_PAGE_BY_ID,
2122
GET_STEP_BY_ID,
23+
GET_RECORD_BY_ID,
2224
} from './graphql/queries';
23-
import { takeUntil } from 'rxjs/operators';
25+
import { switchMap, takeUntil } from 'rxjs/operators';
26+
import { Subscription } from 'rxjs';
2427

2528
/**
2629
* Application preview form page component.
@@ -34,8 +37,6 @@ export class FormComponent extends UnsubscribeComponent implements OnInit {
3437
/** Form component */
3538
@ViewChild(SharedFormComponent)
3639
private formComponent?: SharedFormComponent;
37-
38-
// === DATA ===
3940
/** Loading state */
4041
public loading = true;
4142
/** Current form id */
@@ -44,20 +45,20 @@ export class FormComponent extends UnsubscribeComponent implements OnInit {
4445
public form?: Form;
4546
/** Is form completed */
4647
public completed = false;
48+
/** Ongoing query */
49+
public querySubscription?: Subscription;
4750
/** Should possibility to add new records be hidden */
4851
public hideNewRecord = false;
4952
/** Form button actions */
5053
public actionButtons: ActionButton[] = [];
51-
52-
// === ROUTER ===
5354
/** Current page */
5455
public page?: Page;
5556
/** Current step */
5657
public step?: Step;
57-
58-
// === ROUTE ===
5958
/** Is this form part of step */
6059
public isStep = false;
60+
/** Current record, optional */
61+
public record?: Record;
6162

6263
/**
6364
* Application preview form page component.
@@ -84,58 +85,106 @@ export class FormComponent extends UnsubscribeComponent implements OnInit {
8485
this.loading = true;
8586
this.id = params.id;
8687
this.isStep = this.router.url.includes('/workflow/');
88+
// If a query is already loading, cancel it
89+
if (this.querySubscription) {
90+
this.querySubscription.unsubscribe();
91+
}
8792
if (this.isStep) {
88-
this.apollo
93+
this.querySubscription = this.apollo
8994
.query<StepQueryResponse>({
9095
query: GET_STEP_BY_ID,
9196
variables: {
9297
id: this.id,
9398
},
9499
})
95-
.subscribe(({ data }) => {
96-
this.step = data.step;
97-
this.actionButtons = data.step.buttons as ActionButton[];
98-
this.apollo
99-
.query<FormQueryResponse>({
100-
query: GET_SHORT_FORM_BY_ID,
101-
variables: {
102-
id: this.step.content,
103-
},
104-
})
105-
.subscribe(({ data, loading }) => {
106-
this.form = data.form;
107-
this.loading = loading;
108-
});
100+
.pipe(
101+
switchMap(({ data }) => {
102+
this.step = data.step;
103+
this.actionButtons = data.step.buttons as ActionButton[];
104+
const recordId = this.route.snapshot.queryParams.id;
105+
if (recordId) {
106+
return this.getRecordQuery(recordId).pipe(
107+
switchMap((recordResponse) => {
108+
this.record = recordResponse.data.record;
109+
// Then, proceed to fetch the form
110+
return this.getFormQuery(this.step?.content ?? '');
111+
})
112+
);
113+
}
114+
this.record = undefined;
115+
return this.getFormQuery(this.step.content ?? '');
116+
}),
117+
takeUntil(this.destroy$)
118+
)
119+
.subscribe(({ data, loading }) => {
120+
this.form = data.form;
121+
this.loading = loading;
109122
});
110123
} else {
111-
this.apollo
124+
this.querySubscription = this.apollo
112125
.query<PageQueryResponse>({
113126
query: GET_PAGE_BY_ID,
114127
variables: {
115128
id: this.id,
116129
},
117130
})
118-
.subscribe(({ data }) => {
119-
this.page = data.page;
120-
this.actionButtons = data.page.buttons as ActionButton[];
121-
this.apollo
122-
.query<FormQueryResponse>({
123-
query: GET_SHORT_FORM_BY_ID,
124-
variables: {
125-
id: this.page.content,
126-
},
127-
})
128-
.subscribe(({ data, loading }) => {
129-
if (data) {
130-
this.form = data.form;
131-
}
132-
this.loading = loading;
133-
});
131+
.pipe(
132+
switchMap(({ data }) => {
133+
this.page = data.page;
134+
this.actionButtons = data.page.buttons as ActionButton[];
135+
const recordId = this.route.snapshot.queryParams.id;
136+
if (recordId) {
137+
return this.getRecordQuery(recordId).pipe(
138+
switchMap((recordResponse) => {
139+
this.record = recordResponse.data.record;
140+
// Then, proceed to fetch the form
141+
return this.getFormQuery(this.page?.content ?? '');
142+
})
143+
);
144+
}
145+
this.record = undefined;
146+
return this.getFormQuery(this.page.content ?? '');
147+
}),
148+
takeUntil(this.destroy$)
149+
)
150+
.subscribe(({ data, loading }) => {
151+
this.form = data.form;
152+
this.loading = loading;
134153
});
135154
}
136155
});
137156
}
138157

158+
/**
159+
* Returns a form query stream for the given id
160+
*
161+
* @param {string} id form id to fetch
162+
* @returns a query stream
163+
*/
164+
private getFormQuery(id: string) {
165+
return this.apollo.query<FormQueryResponse>({
166+
query: GET_SHORT_FORM_BY_ID,
167+
variables: {
168+
id,
169+
},
170+
});
171+
}
172+
173+
/**
174+
* Returns query for the given record id
175+
*
176+
* @param id record id
177+
* @returns record query for the given id
178+
*/
179+
private getRecordQuery(id: string) {
180+
return this.apollo.query<RecordQueryResponse>({
181+
query: GET_RECORD_BY_ID,
182+
variables: {
183+
id,
184+
},
185+
});
186+
}
187+
139188
/**
140189
* Handles complete event.
141190
*

apps/back-office/src/app/app-preview/pages/form/graphql/queries.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,22 @@ export const GET_SHORT_FORM_BY_ID = gql`
123123
}
124124
}
125125
`;
126+
127+
/** Graphql request for getting a record by its id */
128+
export const GET_RECORD_BY_ID = gql`
129+
query GetRecordById($id: ID!) {
130+
record(id: $id) {
131+
id
132+
data
133+
createdAt
134+
createdBy {
135+
name
136+
}
137+
modifiedAt
138+
modifiedBy {
139+
name
140+
}
141+
canUpdate
142+
}
143+
}
144+
`;

apps/back-office/src/app/application/pages/form/form.component.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,18 @@ <h1 class="!m-0 overflow-hidden text-ellipsis">
4646
</div>
4747
</div>
4848
<!-- Form -->
49-
<shared-form [form]="form" (save)="onComplete($event)"></shared-form>
50-
<!-- Form actions -->
51-
<div
52-
class="mt-6 flex justify-center gap-x-4"
53-
*ngIf="completed && !form.uniqueRecord && !hideNewRecord"
54-
>
49+
<shared-form
50+
[form]="form"
51+
[record]="record"
52+
(save)="onComplete($event)"
53+
></shared-form>
54+
<!-- Form actions, only appear if not editing a record -->
55+
<div class="mt-6 flex justify-center gap-x-4" *ngIf="completed && !record">
5556
<!-- Action buttons -->
5657
<shared-action-buttons [actionButtons]="actionButtons">
5758
<!-- New record -->
5859
<ui-button
60+
*ngIf="!form.uniqueRecord && !hideNewRecord"
5961
[isBlock]="!actionButtons.length"
6062
class="max-w-xs"
6163
variant="primary"

apps/back-office/src/app/application/pages/form/form.component.ts

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ import {
1818
StepQueryResponse,
1919
UnsubscribeComponent,
2020
WorkflowService,
21+
RecordQueryResponse,
2122
} from '@oort-front/shared';
2223
import { Apollo } from 'apollo-angular';
2324
import { Observable, Subscription } from 'rxjs';
2425
import { filter, map, switchMap, takeUntil } from 'rxjs/operators';
2526
import {
2627
GET_PAGE_BY_ID,
28+
GET_RECORD_BY_ID,
2729
GET_SHORT_FORM_BY_ID,
2830
GET_STEP_BY_ID,
2931
} from './graphql/queries';
@@ -40,7 +42,6 @@ export class FormComponent extends UnsubscribeComponent implements OnInit {
4042
/** Form component */
4143
@ViewChild(SharedFormComponent)
4244
private formComponent?: SharedFormComponent;
43-
4445
/** Loading indicator */
4546
public loading = true;
4647
/** Current form id */
@@ -67,6 +68,8 @@ export class FormComponent extends UnsubscribeComponent implements OnInit {
6768
public isStep = false;
6869
/** Configured form action buttons */
6970
public actionButtons: ActionButton[] = [];
71+
/** Current record, optional */
72+
public record?: Record;
7073

7174
/**
7275
* Form page in application
@@ -119,8 +122,20 @@ export class FormComponent extends UnsubscribeComponent implements OnInit {
119122
switchMap(({ data }) => {
120123
this.step = data.step;
121124
this.actionButtons = data.step.buttons as ActionButton[];
122-
return this.getFormQuery(this.step.content ?? '');
123-
})
125+
const recordId = this.route.snapshot.queryParams.id;
126+
if (recordId) {
127+
return this.getRecordQuery(recordId).pipe(
128+
switchMap((recordResponse) => {
129+
this.record = recordResponse.data.record;
130+
// Then, proceed to fetch the form
131+
return this.getFormQuery(this.step?.content ?? '');
132+
})
133+
);
134+
}
135+
this.record = undefined;
136+
return this.getFormQuery(this.step?.content ?? '');
137+
}),
138+
takeUntil(this.destroy$)
124139
)
125140
.subscribe(({ data, loading }) => {
126141
this.handleFormQueryResponse(data, 'step');
@@ -138,8 +153,20 @@ export class FormComponent extends UnsubscribeComponent implements OnInit {
138153
switchMap(({ data }) => {
139154
this.page = data.page;
140155
this.actionButtons = data.page.buttons as ActionButton[];
141-
return this.getFormQuery(this.page.content ?? '');
142-
})
156+
const recordId = this.route.snapshot.queryParams.id;
157+
if (recordId) {
158+
return this.getRecordQuery(recordId).pipe(
159+
switchMap((recordResponse) => {
160+
this.record = recordResponse.data.record;
161+
// Then, proceed to fetch the form
162+
return this.getFormQuery(this.page?.content ?? '');
163+
})
164+
);
165+
}
166+
this.record = undefined;
167+
return this.getFormQuery(this.page?.content ?? '');
168+
}),
169+
takeUntil(this.destroy$)
143170
)
144171
.subscribe(({ data, loading }) => {
145172
this.handleFormQueryResponse(data, 'page');
@@ -164,6 +191,21 @@ export class FormComponent extends UnsubscribeComponent implements OnInit {
164191
});
165192
}
166193

194+
/**
195+
* Returns query for the given record id
196+
*
197+
* @param id record id
198+
* @returns record query for the given id
199+
*/
200+
private getRecordQuery(id: string) {
201+
return this.apollo.query<RecordQueryResponse>({
202+
query: GET_RECORD_BY_ID,
203+
variables: {
204+
id,
205+
},
206+
});
207+
}
208+
167209
/**
168210
* Handle response for the form query
169211
*

apps/back-office/src/app/application/pages/form/graphql/queries.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,21 @@ export const GET_SHORT_FORM_BY_ID = gql`
128128
}
129129
}
130130
`;
131+
132+
/** Graphql request for getting a record by its id */
133+
export const GET_RECORD_BY_ID = gql`
134+
query GetRecordById($id: ID!) {
135+
record(id: $id) {
136+
id
137+
data
138+
createdAt
139+
createdBy {
140+
name
141+
}
142+
modifiedAt
143+
modifiedBy {
144+
name
145+
}
146+
}
147+
}
148+
`;

0 commit comments

Comments
 (0)