Skip to content

Commit 1a92396

Browse files
committed
Connect create badge to API and clean up
1 parent ce758f8 commit 1a92396

File tree

9 files changed

+109
-21
lines changed

9 files changed

+109
-21
lines changed

src-ts/lib/form/form-groups/form-input/input-image-picker/InputImagePicker.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const InputImagePicker: FC<InputImagePickerProps> = (props: InputImagePickerProp
3636
}
3737
}, [
3838
files,
39+
fileDataURL,
3940
])
4041

4142
return (
@@ -59,7 +60,7 @@ const InputImagePicker: FC<InputImagePickerProps> = (props: InputImagePickerProp
5960
/>
6061
{
6162
fileDataURL ? (
62-
<img src={fileDataURL} className={styles.badgeImage} />
63+
<img src={fileDataURL} alt={'Badge image preview'} className={styles.badgeImage} />
6364
) : (
6465
<div className={styles.filePickerPlaceholder}>UPLOAD<br />IMAGE</div>
6566
)

src-ts/lib/table/Table.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const Table: <T extends { [propertyName: string]: any }>(props: TableProps<T>) =
6161
columns,
6262
data,
6363
defaultSortDirectionMap,
64+
props.onToggleSort,
6465
sort,
6566
])
6667

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
@import "../../../../../lib/styles/variables";
2+
3+
.wrapper {
4+
display: flex;
5+
flex-direction: column;
6+
7+
.badge {
8+
display: flex;
9+
align-items: center;
10+
margin-bottom: $space-xxl;
11+
12+
.badge-image {
13+
width: 43px;
14+
height: 43px;
15+
margin-right: $space-xl;
16+
}
17+
18+
.badge-image-disabled {
19+
width: 43px;
20+
height: 43px;
21+
margin-right: $space-xl;
22+
opacity: 0.5;
23+
filter: grayscale(1);
24+
}
25+
26+
.badge-name {
27+
font-size: 16px;
28+
}
29+
}
30+
31+
.actions {
32+
display: flex;
33+
align-items: center;
34+
35+
a {
36+
margin-right: $space-md;
37+
}
38+
}
39+
}

src-ts/tools/gamification-admin/game-lib/modals/badge-created-modal/BadgeCreatedModal.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { FC } from 'react'
22

3-
import { BaseModal } from '../../../../../lib'
3+
import { BaseModal, Button } from '../../../../../lib'
4+
import { badgeDetailPath } from '../../../gamification-admin.routes'
5+
import { GameBadge } from '../../game-badge.model'
46

7+
import styles from './BadgeCreatedModal.module.scss'
58
export interface BadgeCreatedModalProps {
9+
badge: GameBadge
610
isOpen: boolean
711
onClose: () => void
812
}
@@ -20,7 +24,28 @@ const BadgeCreatedModal: FC<BadgeCreatedModalProps> = (props: BadgeCreatedModalP
2024
size='md'
2125
title={`Badge created`}
2226
>
23-
27+
<div className={styles.wrapper}>
28+
<div className={styles.badge}>
29+
<img
30+
alt={props.badge.badge_name}
31+
className={styles[props.badge.active ? 'badge-image' : 'badge-image-disabled']}
32+
src={props.badge.badge_image_url}
33+
/>
34+
<p className={styles['badge-name']}>{props.badge.badge_name} badge has been sucessfully created.</p>
35+
</div>
36+
<div className={styles.actions}>
37+
<Button
38+
label='View'
39+
buttonStyle='primary'
40+
route={badgeDetailPath(props.badge.id)}
41+
/>
42+
<Button
43+
label={props.badge.active ? 'Award' : 'Activate & Award'}
44+
buttonStyle='secondary'
45+
route={badgeDetailPath(props.badge.id, 'award')}
46+
/>
47+
</div>
48+
</div>
2449
</BaseModal>
2550
)
2651
}

src-ts/tools/gamification-admin/gamification-admin.routes.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import BadgeDetailPage from './pages/badge-detail/BadgeDetailPage'
55
import BadgeListingPage from './pages/badge-listing/BadgeListingPage'
66
import CreateBadgePage from './pages/create-badge/CreateBadgePage'
77

8-
const baseDetailPath: string = '/badge-detail'
9-
const createBadgePath: string = '/create-badge'
8+
export const baseDetailPath: string = '/badge-detail'
9+
export const createBadgePath: string = '/create-badge'
1010

1111
export const basePath: string = '/gamification-admin'
1212

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

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

33
import { Breadcrumb, BreadcrumbItemModel, ContentLayout } from '../../../../lib'
4-
import { useGamificationBreadcrumb } from '../../game-lib'
4+
import { GameBadge, useGamificationBreadcrumb } from '../../game-lib'
55
import { BadgeCreatedModal } from '../../game-lib/modals/badge-created-modal'
66

77
import { CreateBadgeForm, createBadgeFormDef } from './create-badge-form'
@@ -19,7 +19,11 @@ const CreateBadgePage: FC = () => {
1919
const [showBadgeCreatedModal, setShowBadgeCreatedModal]: [boolean, Dispatch<SetStateAction<boolean>>]
2020
= useState<boolean>(false)
2121

22-
function onSave() {
22+
const [createdBadge, setCreatedBadge]: [GameBadge | undefined, Dispatch<SetStateAction<GameBadge | undefined>>]
23+
= useState<GameBadge | undefined>()
24+
25+
function onSave(newBadge: GameBadge): void {
26+
setCreatedBadge(newBadge)
2327
setShowBadgeCreatedModal(true)
2428
}
2529

@@ -34,10 +38,13 @@ const CreateBadgePage: FC = () => {
3438
onSave={onSave}
3539
/>
3640
</div>
37-
<BadgeCreatedModal
38-
isOpen={showBadgeCreatedModal}
39-
onClose={() => setShowBadgeCreatedModal(false)}
40-
/>
41+
{
42+
createdBadge && <BadgeCreatedModal
43+
badge={createdBadge}
44+
isOpen={showBadgeCreatedModal}
45+
onClose={() => setShowBadgeCreatedModal(false)}
46+
/>
47+
}
4148
</ContentLayout>
4249
)
4350
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { FC } from 'react'
22

33
import { Form, FormDefinition, formGetInputModel, FormInputModel } from '../../../../../lib'
4+
import { GamificationConfig } from '../../../game-config'
5+
import { GameBadge } from '../../../game-lib'
46

57
import { CreateBadgeFormField } from './create-badge-form.config'
68
import { CreateBadgeRequest } from './create-badge-functions'
79
import { createBadgeSubmitRequestAsync } from './create-badge-functions/create-badge-store'
810

911
export interface CreateBadgeFormProps {
1012
formDef: FormDefinition
11-
onSave: () => void
13+
onSave: (createdBadge: GameBadge) => void
1214
}
1315

1416
const CreateBadgeForm: FC<CreateBadgeFormProps> = (props: CreateBadgeFormProps) => {
@@ -23,16 +25,16 @@ const CreateBadgeForm: FC<CreateBadgeFormProps> = (props: CreateBadgeFormProps)
2325
badgeActive,
2426
badgeDesc,
2527
badgeName,
26-
file: files[0],
28+
badgeStatus: 'Active', // not used currently thus fixed field
29+
files,
30+
orgID: GamificationConfig.ORG_ID,
2731
}
2832
}
2933

3034
async function saveAsync(request: CreateBadgeRequest): Promise<void> {
31-
console.log('saveAsync', request)
32-
3335
return createBadgeSubmitRequestAsync(request)
34-
.then(() => {
35-
props.onSave()
36+
.then((createdBadge: GameBadge) => {
37+
props.onSave(createdBadge)
3638
})
3739
}
3840

src-ts/tools/gamification-admin/pages/create-badge/create-badge-form/create-badge-functions/create-badge-store/create-badge-request.model.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@ export interface CreateBadgeRequest {
22
badgeActive: boolean
33
badgeDesc: string
44
badgeName: string
5-
file: File
5+
badgeStatus: string
6+
files: FileList
7+
orgID: string
68
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import { EnvironmentConfig } from '../../../../../../../config'
22
import { xhrPostAsync } from '../../../../../../../lib'
3+
import { GameBadge } from '../../../../../game-lib'
34

45
import { CreateBadgeRequest } from './create-badge-request.model'
56

6-
export async function submitRequestAsync(request: CreateBadgeRequest): Promise<void> {
7-
console.log('submitRequestAsync', request)
7+
export async function submitRequestAsync(request: CreateBadgeRequest): Promise<GameBadge> {
88
const url: string = `${EnvironmentConfig.API.V5}/gamification/badges`
9-
await xhrPostAsync(url, request)
9+
10+
const form: any = new FormData()
11+
12+
// fill the form
13+
form.append('file', request.files[0])
14+
form.append('organization_id', request.orgID)
15+
form.append('badge_status', request.badgeStatus)
16+
form.append('badge_name', request.badgeName)
17+
form.append('badge_description', request.badgeDesc)
18+
form.append('active', request.badgeActive ? 'true' : 'false')
19+
20+
return xhrPostAsync(url, form)
1021
}

0 commit comments

Comments
 (0)