|
1 | 1 | import Component from '@glimmer/component'; |
2 | 2 | import t from 'ember-intl/helpers/t'; |
3 | | -import { on } from '@ember/modifier'; |
4 | | -import type RouterService from '@ember/routing/router-service'; |
5 | 3 | import { service } from '@ember/service'; |
| 4 | +import TpkForm from '@triptyk/ember-input-validation/components/tpk-form'; |
| 5 | +import type HandleSaveService from '@libs/shared-front/services/handle-save'; |
| 6 | +import type CurriculumService from '#src/services/curriculum.ts'; |
| 7 | +import ImmerChangeset from 'ember-immer-changeset'; |
| 8 | +import { createCurriculumValidationSchema } from '#src/components/curriculums/curriculum-validation.ts'; |
| 9 | +import type { ValidatedCurriculum } from '#src/components/curriculums/curriculum-validation.ts'; |
| 10 | +import type Owner from '@ember/owner'; |
| 11 | +import type { IntlService } from 'ember-intl'; |
6 | 12 |
|
7 | 13 | interface CurriculumAddSignature { |
8 | 14 | Element: HTMLDivElement; |
9 | | - // eslint-disable-next-line @typescript-eslint/no-empty-object-type |
10 | | - Args: {}; |
| 15 | + Args: { |
| 16 | + onAdd: () => void; |
| 17 | + }; |
11 | 18 | } |
12 | 19 |
|
13 | 20 | class CurriculumAdd extends Component<CurriculumAddSignature> { |
14 | | - @service declare router: RouterService; |
| 21 | + @service declare handleSave: HandleSaveService; |
| 22 | + @service declare curriculum: CurriculumService; |
| 23 | + @service declare intl: IntlService; |
| 24 | + |
| 25 | + changeset = new ImmerChangeset<ValidatedCurriculum>( |
| 26 | + {} as ValidatedCurriculum |
| 27 | + ); |
| 28 | + validationSchema: ReturnType<typeof createCurriculumValidationSchema>; |
| 29 | + |
| 30 | + constructor(owner: Owner, args: CurriculumAddSignature['Args']) { |
| 31 | + super(owner, args); |
| 32 | + this.validationSchema = createCurriculumValidationSchema(this.intl); |
| 33 | + } |
| 34 | + |
| 35 | + onSubmit = async ( |
| 36 | + data: ValidatedCurriculum, |
| 37 | + c: ImmerChangeset<ValidatedCurriculum> |
| 38 | + ) => { |
| 39 | + await this.handleSave.handleSave({ |
| 40 | + saveAction: () => this.curriculum.create(data), |
| 41 | + changeset: c, |
| 42 | + successMessage: 'curriculums.create.createSuccess', |
| 43 | + }); |
15 | 44 |
|
16 | | - addCurriculum = () => { |
17 | | - this.router.transitionTo('dashboard.curriculums.create'); |
| 45 | + this.args.onAdd?.(); |
18 | 46 | }; |
19 | 47 |
|
20 | 48 | <template> |
21 | | - <button |
22 | | - data-test-curriculum-add-button |
23 | | - type="button" |
24 | | - class="flex flex-col cursor-pointer items-center justify-center w-51 m-5 mb-20 border-2 border-dashed border-gray-300 rounded-lg text-gray-500 hover:border-gray-400 hover:text-gray-700 transition-colors duration-200" |
25 | | - {{on "click" this.addCurriculum}} |
| 49 | + <TpkForm |
| 50 | + class="flex flex-col cursor-pointer items-center justify-center w-51 m-5 p-2 mb-20 border-2 border-dashed border-gray-300 rounded-lg text-gray-500 hover:border-gray-400 hover:text-gray-700 transition-colors duration-200" |
| 51 | + @changeset={{this.changeset}} |
| 52 | + @onSubmit={{this.onSubmit}} |
| 53 | + @validationSchema={{this.validationSchema}} |
| 54 | + data-test-todos-form |
| 55 | + as |F| |
26 | 56 | > |
27 | | - <span class="text-3xl font-bold">+</span> |
28 | | - <span class="mt-2 text-sm font-medium"> |
29 | | - {{t "curriculums.view.createNewCV"}} |
30 | | - </span> |
31 | | - </button> |
| 57 | + <F.TpkInputPrefab |
| 58 | + @label={{t "curriculums.create.title"}} |
| 59 | + @validationField="title" |
| 60 | + class="col-span-12 m-2 md:col-span-4" |
| 61 | + /> |
| 62 | + <button |
| 63 | + data-test-curriculum-add-button |
| 64 | + type="submit" |
| 65 | + class="border-gray-300 hover:border-gray-400 rounded-lg p-4 flex flex-col items-center justify-center transition-colors duration-200 w-full mt-4" |
| 66 | + > |
| 67 | + <span class="text-3xl font-bold">+</span> |
| 68 | + <span class="mt-2 text-sm font-medium"> |
| 69 | + {{t "curriculums.view.createNewCV"}} |
| 70 | + </span> |
| 71 | + </button> |
| 72 | + </TpkForm> |
32 | 73 | </template> |
33 | 74 | } |
34 | 75 |
|
|
0 commit comments