Skip to content

Commit f98d0de

Browse files
committed
init badge creation
1 parent 5c836c9 commit f98d0de

File tree

11 files changed

+158
-20
lines changed

11 files changed

+158
-20
lines changed
Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
.contentLayout {
2-
width: 100%;
3-
padding-bottom: 0;
4-
5-
.contentLayout-outer {
6-
width: 100%;
7-
8-
.contentLayout-inner {
9-
width: 100%;
10-
overflow: visible;
11-
}
12-
}
13-
}
14-
151
.container {
162
display: flex;
173
}

src-ts/tools/gamification-admin/pages/create-badge/CreateBadgePage.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { FC, useMemo } from 'react'
1+
import { Dispatch, FC, SetStateAction, useMemo, useState } from 'react'
22

3-
import { Breadcrumb, BreadcrumbItemModel, ContentLayout } from '../../../../lib'
3+
import { Breadcrumb, BreadcrumbItemModel, ContentLayout, FormDefinition, formOnReset, formGetInputFields } from '../../../../lib'
44
import { baseUrl } from '../../gamification-admin.routes'
55
import { toolTitle } from '../../GamificationAdmin'
6+
import { CreateBadgeForm, createBadgeFormDef } from './create-badge-form'
67

78
import styles from './CreateBadgePage.module.scss'
89

@@ -12,16 +13,25 @@ const CreateBadgePage: FC = () => {
1213
{ name: 'create badge', url: '#' },
1314
], [])
1415

16+
const [formDef, setFormDef]: [FormDefinition, Dispatch<SetStateAction<FormDefinition>>]
17+
= useState<FormDefinition>({ ...createBadgeFormDef })
18+
19+
function onSave(): void {
20+
const updatedForm: FormDefinition = { ...formDef }
21+
formOnReset(formGetInputFields(updatedForm.groups || []))
22+
setFormDef(updatedForm)
23+
}
24+
1525
return (
1626
<ContentLayout
17-
contentClass={styles['contentLayout']}
18-
outerClass={styles['contentLayout-outer']}
19-
innerClass={styles['contentLayout-inner']}
2027
title='Create Badge'
2128
>
2229
<Breadcrumb items={breadcrumb} />
2330
<div className={styles.container}>
24-
31+
<CreateBadgeForm
32+
formDef={createBadgeFormDef}
33+
onSave={onSave}
34+
/>
2535
</div>
2636
</ContentLayout>
2737
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@import '../../../../../lib/styles/includes';
2+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { FC, useContext } from 'react'
2+
3+
import { Form, FormDefinition, formGetInputModel, FormInputModel, profileContext, ProfileContextData } from '../../../../../lib'
4+
5+
import { CreateBadgeFormField } from './create-badge-form.config'
6+
import { CreateBadgeRequest } from './create-badge-functions'
7+
import { createBadgeSubmitRequestAsync } from './create-badge-functions/create-badge-store'
8+
import styles from './CreateBadgeForm.module.scss'
9+
10+
export interface CreateBadgeFormProps {
11+
formDef: FormDefinition
12+
onSave: () => void
13+
}
14+
15+
const CreateBadgeForm: FC<CreateBadgeFormProps> = (props: CreateBadgeFormProps) => {
16+
17+
const { profile }: ProfileContextData = useContext(profileContext)
18+
19+
function generateRequest(inputs: ReadonlyArray<FormInputModel>): CreateBadgeRequest {
20+
const badgeName: string = formGetInputModel(inputs, CreateBadgeFormField.badgeName).value as string
21+
const badgeDesc: string = formGetInputModel(inputs, CreateBadgeFormField.badgeDesc).value as string
22+
const badgeActive: string = formGetInputModel(inputs, CreateBadgeFormField.badgeActive).value as string
23+
return {
24+
badgeActive,
25+
badgeName,
26+
badgeDesc,
27+
}
28+
}
29+
30+
async function saveAsync(request: CreateBadgeRequest): Promise<void> {
31+
return createBadgeSubmitRequestAsync(request)
32+
.then(() => {
33+
props.onSave()
34+
})
35+
}
36+
37+
return (
38+
<>
39+
<Form
40+
formDef={props.formDef}
41+
formValues={profile}
42+
requestGenerator={generateRequest}
43+
save={saveAsync}
44+
/>
45+
</>
46+
)
47+
}
48+
49+
export default CreateBadgeForm
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { FormDefinition, validatorRequired, IconOutline, RadioButton } from '../../../../../lib'
2+
3+
export enum CreateBadgeFormField {
4+
badgeActive = 'badgeActive',
5+
badgeName = 'badgeName',
6+
badgeDesc = 'badgeDesc',
7+
}
8+
9+
export const createBadgeFormDef: FormDefinition = {
10+
buttons: {
11+
primaryGroup: [
12+
{
13+
buttonStyle: 'primary',
14+
isSubmit: true,
15+
label: 'Save Badge',
16+
size: 'lg',
17+
type: 'submit',
18+
},
19+
],
20+
secondaryGroup: [
21+
{
22+
buttonStyle: 'icon-bordered',
23+
size: 'lg',
24+
icon: IconOutline.ChevronLeftIcon,
25+
route: `/gamification-admin`
26+
27+
},
28+
]
29+
},
30+
groups: [
31+
{
32+
inputs: [
33+
{
34+
label: 'Badge Name',
35+
name: CreateBadgeFormField.badgeName,
36+
type: 'text',
37+
validators: [
38+
{
39+
validator: validatorRequired,
40+
},
41+
],
42+
},
43+
{
44+
label: 'Badge Description',
45+
name: CreateBadgeFormField.badgeDesc,
46+
type: 'textarea',
47+
validators: [
48+
{
49+
validator: validatorRequired,
50+
},
51+
],
52+
},
53+
{
54+
label: 'Activate',
55+
name: CreateBadgeFormField.badgeActive,
56+
type: 'checkbox',
57+
validators: [
58+
{
59+
validator: validatorRequired,
60+
},
61+
],
62+
}
63+
],
64+
},
65+
],
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface CreateBadgeRequest {
2+
badgeActive: string
3+
badgeName: string
4+
badgeDesc: string
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { EnvironmentConfig } from '../../../../../../../config'
2+
import { xhrPostAsync } from '../../../../../../../lib'
3+
4+
import { CreateBadgeRequest } from './create-badge-request.model'
5+
6+
export async function submitRequestAsync(request: CreateBadgeRequest): Promise<void> {
7+
const url: string = `${EnvironmentConfig.API.V5}/gamification/badges`
8+
await xhrPostAsync(url, request)
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './create-badge-request.model'
2+
export { submitRequestAsync as createBadgeSubmitRequestAsync } from './create-badge.store'
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { CreateBadgeRequest, createBadgeSubmitRequestAsync } from './create-badge-store'
2+
3+
export async function submitRequestAsync(request: CreateBadgeRequest): Promise<void> {
4+
return createBadgeSubmitRequestAsync(request)
5+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { type CreateBadgeRequest } from './create-badge-store'
2+
export { submitRequestAsync as contactSupportSubmitRequestAsync } from './create-badge.functions'

0 commit comments

Comments
 (0)