Skip to content

Commit 3fd98d7

Browse files
committed
data + web - create checkin with multiple achievements
1 parent 97dfddb commit 3fd98d7

16 files changed

+759
-600
lines changed
+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React from 'react';
2+
import * as MUI from '@material-ui/core';
3+
import * as Icons from '@material-ui/icons';
4+
5+
export const HeaderWithTags = ({
6+
title,
7+
selected,
8+
setSelected,
9+
showCount = false
10+
}) => {
11+
return (
12+
<div
13+
style={{
14+
flexDirection: 'row',
15+
display: 'flex',
16+
alignItems: 'center',
17+
paddingBottom: 10
18+
}}
19+
>
20+
<h2 style={{ paddingRight: 10 }}>{title}</h2>
21+
<div style={{ flex: 1 }}>
22+
{Array.from(selected).map(([id, sa]) => (
23+
<MUI.Chip
24+
avatar={
25+
<MUI.Avatar>{showCount ? sa.count : null}</MUI.Avatar>
26+
}
27+
key={id}
28+
label={sa.name}
29+
style={{ marginRight: 10 }}
30+
onDelete={() => {
31+
const newSelected = new Map(selected);
32+
newSelected.delete(id);
33+
setSelected(newSelected);
34+
}}
35+
/>
36+
))}
37+
</div>
38+
{showCount && (
39+
<MUI.Chip
40+
color="primary"
41+
icon={<Icons.Add />}
42+
label={Array.from(selected).reduce(
43+
(acc: number, sa) => (acc += sa[1].count * sa[1].points),
44+
0
45+
)}
46+
/>
47+
)}
48+
</div>
49+
);
50+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { MenuItem, Select } from '@material-ui/core';
2+
import React from 'react';
3+
import { useContext, useState } from 'react';
4+
import { CreateCheckinContext } from '../context/CreateCheckinContext';
5+
6+
export const SelectAchievementCount = ({ id, value }) => {
7+
const [v, setValue] = useState(value ?? 1);
8+
const { achievementCounts, setSelectedAchievementCountById } =
9+
useContext(CreateCheckinContext);
10+
11+
const handleChange = (event) => {
12+
const count = event.target.value;
13+
setValue(count);
14+
const newAchievementCounts = new Map(achievementCounts.get);
15+
newAchievementCounts.set(id, count);
16+
achievementCounts.set(newAchievementCounts);
17+
setSelectedAchievementCountById(id, count);
18+
};
19+
20+
return (
21+
<Select style={{ flex: 1 }} value={v} onChange={handleChange}>
22+
{Array.from(Array(10).keys()).map((_, i) => (
23+
<MenuItem key={i + 1} value={i + 1}>
24+
{i + 1}
25+
</MenuItem>
26+
))}
27+
</Select>
28+
);
29+
};

0 commit comments

Comments
 (0)