-
-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathExerciseUpdateModal.tsx
More file actions
92 lines (87 loc) · 2.7 KB
/
ExerciseUpdateModal.tsx
File metadata and controls
92 lines (87 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// i18n-key-prefix: exerciseUpdateModal
// i18n-namespace: components/modals/ExerciseUpdateModal.tsx
import React from 'react'
import { FetchingBoundary } from '../FetchingBoundary'
import { Modal, ModalProps } from './Modal'
import { Icon, GraphicalIcon } from '../common'
import { useRequestQuery } from '../../hooks/request-query'
import { ExerciseUpdateForm } from './exercise-update-modal/ExerciseUpdateForm'
import { Exercise } from '../types'
import { useAppTranslation } from '@/i18n/useAppTranslation'
type ExerciseDiffFile = {
relativePath: string
diff: string
}
export type ExerciseDiff = {
files: ExerciseDiffFile[]
exercise: Exercise
links: {
update: string
}
}
const DEFAULT_ERROR = new Error(
"Sorry - it seems that we can't work out what needs updating for this exercise. We've been alerted and will have a look."
)
export const ExerciseUpdateModal = ({
endpoint,
...props
}: Omit<ModalProps, 'className'> & { endpoint: string }): JSX.Element => {
const { t } = useAppTranslation('components/modals/ExerciseUpdateModal.tsx')
const { data, status, error } = useRequestQuery<{ diff: ExerciseDiff }>(
['exercise-update', endpoint],
{ endpoint: endpoint, options: {} }
)
return (
<Modal {...props} className="m-update-exercise">
<FetchingBoundary
LoadingComponent={LoadingComponent}
status={status}
error={error}
defaultError={DEFAULT_ERROR}
>
{data ? (
data.diff.files.length > 0 ? (
<ExerciseUpdateForm diff={data.diff} onCancel={props.onClose} />
) : (
<AutoUpdatedNotice onClose={props.onClose} />
)
) : null}
</FetchingBoundary>
</Modal>
)
}
const AutoUpdatedNotice = ({ onClose }: { onClose: () => void }) => {
const { t } = useAppTranslation('components/modals/ExerciseUpdateModal.tsx')
return (
<div className="auto-updated-notice flex flex-col items-center text-center">
<GraphicalIcon
icon="completed-check-circle"
height={64}
width={64}
className="mb-20"
/>
<h2 className="text-h3 mb-8">
{t('exerciseUpdateModal.autoUpdatedTitle')}
</h2>
<p className="text-p-large mb-24">
{t('exerciseUpdateModal.autoUpdatedBody')}
</p>
<button
type="button"
className="btn-primary btn-m"
onClick={() => {
onClose()
window.location.reload()
}}
>
{t('exerciseUpdateModal.continue')}
</button>
</div>
)
}
const LoadingComponent = () => {
const { t } = useAppTranslation('components/modals/ExerciseUpdateModal.tsx')
return (
<Icon icon="spinner" alt={t('exerciseUpdateModal.loadingExerciseDiff')} />
)
}