-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FormClient and supporting code to make it easier to test form ses…
…sion state transitions; add currently failing test "honors first matching page rule". Will follow-up with a refactoring of page references, to refer to its pattern ID rather than array index.
- Loading branch information
1 parent
ed2b6ea
commit 8e2bc3d
Showing
9 changed files
with
219 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Form } from '../../builder'; | ||
import type { FormConfig } from '../../pattern'; | ||
import type { FormService } from '../../services'; | ||
import { type FormSession, type FormSessionId } from '../../session'; | ||
import { getActionString } from '../../submission'; | ||
import type { Blueprint } from '../../types'; | ||
|
||
type FormClientContext = { | ||
config: FormConfig; | ||
formService: FormService; | ||
}; | ||
|
||
type FormClientState = { | ||
sessionId?: FormSessionId; | ||
session: FormSession; | ||
attachments?: { | ||
fileName: string; | ||
data: Uint8Array; | ||
}[]; | ||
}; | ||
|
||
export class FormClient { | ||
private form: Form; | ||
private _state?: FormClientState; | ||
|
||
constructor( | ||
private ctx: FormClientContext, | ||
private formId: string, | ||
blueprint: Blueprint | ||
) { | ||
this.form = new Form(ctx.config, blueprint); | ||
} | ||
|
||
async getState(): Promise<FormClientState> { | ||
if (!this._state) { | ||
const result = await this.ctx.formService.getFormSession({ | ||
formId: this.formId, | ||
formRoute: this.form.getInitialFormRoute(), | ||
}); | ||
if (!result.success) { | ||
throw new Error('Error getting form session'); | ||
} | ||
this._state = { | ||
sessionId: result.data.id, | ||
session: result.data.data, | ||
}; | ||
} | ||
return this._state; | ||
} | ||
|
||
setState(state: FormClientState) { | ||
this._state = state; | ||
} | ||
|
||
async submitPage(formData: Record<string, string>): Promise<void> { | ||
const state = await this.getState(); | ||
const result = await this.ctx.formService.submitForm( | ||
state.sessionId, | ||
this.formId, | ||
{ | ||
...formData, | ||
action: getActionString({ | ||
handlerId: 'page-set', | ||
patternId: state.session.form.root, | ||
}), | ||
}, | ||
state.session.route | ||
); | ||
if (!result.success) { | ||
throw new Error(`Error submitting form: ${result.error}`); | ||
} | ||
this.setState({ | ||
sessionId: result.data.sessionId, | ||
session: result.data.session, | ||
attachments: result.data.attachments, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import type { Pattern } from '../../pattern'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters