|
| 1 | +import React, { FC, useEffect } from 'react'; |
| 2 | +import * as MUI from '@material-ui/core'; |
| 3 | +import * as Icons from '@material-ui/icons'; |
| 4 | +import { Checkin } from '@act/data/core'; |
| 5 | +import db from '@act/data/web'; |
| 6 | +import { SelectAchievementsAndUsers } from './components/SelectAchievementsAndUsers'; |
| 7 | +import { |
| 8 | + CreateCheckinContext, |
| 9 | + CreateCheckinProvider |
| 10 | +} from './context/CreateCheckinContext'; |
| 11 | + |
| 12 | +type CreateCheckinProps = { |
| 13 | + onDismiss: () => void; |
| 14 | + selectedCheckin?: Checkin; |
| 15 | +}; |
| 16 | + |
| 17 | +export const CreateCheckin: FC<CreateCheckinProps> = ({ |
| 18 | + onDismiss, |
| 19 | + selectedCheckin |
| 20 | +}) => { |
| 21 | + const classes = useStyles(); |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + // selectedCheckin && |
| 25 | + // selectedCheckin.achievements |
| 26 | + // .fetch() |
| 27 | + // .then((cas) => |
| 28 | + // cas.map(async (ca) => { |
| 29 | + // const a = await db.models.achievements._collection.query( |
| 30 | + // Q.where('id', ca.achievementId) |
| 31 | + // ); |
| 32 | + // return a[0]; |
| 33 | + // }) |
| 34 | + // ) |
| 35 | + // .then((a) => a.map(async (b) => console.log(await b))); |
| 36 | + }, []); |
| 37 | + |
| 38 | + if (!open) { |
| 39 | + return null; |
| 40 | + } |
| 41 | + |
| 42 | + return ( |
| 43 | + <CreateCheckinProvider> |
| 44 | + <CreateCheckinContext.Consumer> |
| 45 | + {({ model }) => { |
| 46 | + const { note, approved, users, achievements } = model; |
| 47 | + return ( |
| 48 | + <MUI.Paper |
| 49 | + style={{ |
| 50 | + height: '100%', |
| 51 | + display: 'flex', |
| 52 | + flexDirection: 'column' |
| 53 | + }} |
| 54 | + elevation={3} |
| 55 | + > |
| 56 | + <h2 className={classes.addCheckinHeader}> |
| 57 | + Add Checkin |
| 58 | + </h2> |
| 59 | + <div style={{ display: 'flex' }}> |
| 60 | + <MUI.TextField |
| 61 | + id="filled-basic" |
| 62 | + label="Note" |
| 63 | + variant="filled" |
| 64 | + value={note.get} |
| 65 | + onChange={(e) => note.set(e.target.value)} |
| 66 | + style={{ |
| 67 | + width: '100%', |
| 68 | + margin: 10, |
| 69 | + marginRight: 0 |
| 70 | + }} |
| 71 | + /> |
| 72 | + <MUI.FormControlLabel |
| 73 | + style={{ margin: 10, marginLeft: 0 }} |
| 74 | + control={ |
| 75 | + <MUI.Switch |
| 76 | + checked={model.approved.get} |
| 77 | + onChange={(e) => approved.set(e.target.checked)} |
| 78 | + name="checkedB" |
| 79 | + color="primary" |
| 80 | + /> |
| 81 | + } |
| 82 | + label="Approved" |
| 83 | + /> |
| 84 | + </div> |
| 85 | + <SelectAchievementsAndUsers /> |
| 86 | + <MUI.AppBar position="static"> |
| 87 | + <MUI.Toolbar |
| 88 | + style={{ |
| 89 | + flexDirection: 'column', |
| 90 | + justifyContent: 'center' |
| 91 | + }} |
| 92 | + > |
| 93 | + <div style={{ alignSelf: 'flex-end' }}> |
| 94 | + <MUI.Button |
| 95 | + variant="contained" |
| 96 | + color="default" |
| 97 | + startIcon={<Icons.Add />} |
| 98 | + onClick={() => { |
| 99 | + db.models.checkins.create( |
| 100 | + { |
| 101 | + note: note.get, |
| 102 | + approved: approved.get |
| 103 | + }, |
| 104 | + new Map( |
| 105 | + Array.from(achievements.get).map( |
| 106 | + ([key, value]) => [key, value.count] |
| 107 | + ) |
| 108 | + ), |
| 109 | + Array.from(users.get.keys()) |
| 110 | + ); |
| 111 | + onDismiss(); |
| 112 | + }} |
| 113 | + > |
| 114 | + Add Checkin |
| 115 | + </MUI.Button> |
| 116 | + <MUI.Button |
| 117 | + style={{ |
| 118 | + alignSelf: 'flex-end', |
| 119 | + marginLeft: 10 |
| 120 | + }} |
| 121 | + variant="contained" |
| 122 | + color="default" |
| 123 | + startIcon={<Icons.Cancel />} |
| 124 | + onClick={onDismiss} |
| 125 | + > |
| 126 | + Cancel |
| 127 | + </MUI.Button> |
| 128 | + </div> |
| 129 | + </MUI.Toolbar> |
| 130 | + </MUI.AppBar> |
| 131 | + </MUI.Paper> |
| 132 | + ); |
| 133 | + }} |
| 134 | + </CreateCheckinContext.Consumer> |
| 135 | + </CreateCheckinProvider> |
| 136 | + ); |
| 137 | +}; |
| 138 | + |
| 139 | +const useStyles = MUI.makeStyles((theme: MUI.Theme) => |
| 140 | + MUI.createStyles({ |
| 141 | + formControl: { |
| 142 | + margin: theme.spacing(1), |
| 143 | + minWidth: 120, |
| 144 | + maxWidth: 300 |
| 145 | + }, |
| 146 | + toolbar: theme.mixins.toolbar, |
| 147 | + addCheckinHeader: { |
| 148 | + backgroundColor: theme.palette.primary.main, |
| 149 | + margin: 0, |
| 150 | + color: 'white', |
| 151 | + padding: 20 |
| 152 | + }, |
| 153 | + chips: { |
| 154 | + display: 'flex', |
| 155 | + flexWrap: 'wrap' |
| 156 | + }, |
| 157 | + chip: { |
| 158 | + margin: 2 |
| 159 | + } |
| 160 | + }) |
| 161 | +); |
0 commit comments