+
{
const q = query(contracts, where('groupSlugs', 'array-contains', slug))
const snapshot = await getDocs(q)
- console.log(snapshot.docs.map((doc) => doc.data()))
return snapshot.docs.map((doc) => doc.data())
}
diff --git a/web/lib/firebase/groups.ts b/web/lib/firebase/groups.ts
index f782f6a8..debc9a97 100644
--- a/web/lib/firebase/groups.ts
+++ b/web/lib/firebase/groups.ts
@@ -129,56 +129,61 @@ export async function addContractToGroup(
contract: Contract,
userId: string
) {
- if (contract.groupLinks?.map((l) => l.groupId).includes(group.id)) return // already in that group
+ if (!contract.groupLinks?.map((l) => l.groupId).includes(group.id)) {
+ const newGroupLinks = [
+ ...(contract.groupLinks ?? []),
+ {
+ groupId: group.id,
+ createdTime: Date.now(),
+ slug: group.slug,
+ userId,
+ name: group.name,
+ } as GroupLink,
+ ]
- const newGroupLinks = [
- ...(contract.groupLinks ?? []),
- {
- groupId: group.id,
- createdTime: Date.now(),
- slug: group.slug,
- userId,
- name: group.name,
- } as GroupLink,
- ]
-
- await updateContract(contract.id, {
- groupSlugs: uniq([...(contract.groupSlugs ?? []), group.slug]),
- groupLinks: newGroupLinks,
- })
- return await updateGroup(group, {
- contractIds: uniq([...group.contractIds, contract.id]),
- })
- .then(() => group)
- .catch((err) => {
- console.error('error adding contract to group', err)
- return err
+ await updateContract(contract.id, {
+ groupSlugs: uniq([...(contract.groupSlugs ?? []), group.slug]),
+ groupLinks: newGroupLinks,
})
+ }
+ if (!group.contractIds.includes(contract.id)) {
+ return await updateGroup(group, {
+ contractIds: uniq([...group.contractIds, contract.id]),
+ })
+ .then(() => group)
+ .catch((err) => {
+ console.error('error adding contract to group', err)
+ return err
+ })
+ }
}
export async function removeContractFromGroup(
group: Group,
contract: Contract
) {
- if (!contract.groupLinks?.map((l) => l.groupId).includes(group.id)) return // not in that group
-
- const newGroupLinks = contract.groupLinks?.filter(
- (link) => link.slug !== group.slug
- )
- await updateContract(contract.id, {
- groupSlugs:
- contract.groupSlugs?.filter((slug) => slug !== group.slug) ?? [],
- groupLinks: newGroupLinks ?? [],
- })
- const newContractIds = group.contractIds.filter((id) => id !== contract.id)
- return await updateGroup(group, {
- contractIds: uniq(newContractIds),
- })
- .then(() => group)
- .catch((err) => {
- console.error('error removing contract from group', err)
- return err
+ if (contract.groupLinks?.map((l) => l.groupId).includes(group.id)) {
+ const newGroupLinks = contract.groupLinks?.filter(
+ (link) => link.slug !== group.slug
+ )
+ await updateContract(contract.id, {
+ groupSlugs:
+ contract.groupSlugs?.filter((slug) => slug !== group.slug) ?? [],
+ groupLinks: newGroupLinks ?? [],
})
+ }
+
+ if (group.contractIds.includes(contract.id)) {
+ const newContractIds = group.contractIds.filter((id) => id !== contract.id)
+ return await updateGroup(group, {
+ contractIds: uniq(newContractIds),
+ })
+ .then(() => group)
+ .catch((err) => {
+ console.error('error removing contract from group', err)
+ return err
+ })
+ }
}
export async function setContractGroupLinks(
diff --git a/web/pages/group/[...slugs]/index.tsx b/web/pages/group/[...slugs]/index.tsx
index eebf0619..dd712a36 100644
--- a/web/pages/group/[...slugs]/index.tsx
+++ b/web/pages/group/[...slugs]/index.tsx
@@ -49,6 +49,7 @@ import { useWindowSize } from 'web/hooks/use-window-size'
import { CopyLinkButton } from 'web/components/copy-link-button'
import { ENV_CONFIG } from 'common/envs/constants'
import { useSaveReferral } from 'web/hooks/use-save-referral'
+import { Button } from 'web/components/button'
export const getStaticProps = fromPropz(getStaticPropz)
export async function getStaticPropz(props: { params: { slugs: string[] } }) {
@@ -541,10 +542,26 @@ function GroupLeaderboards(props: {
function AddContractButton(props: { group: Group; user: User }) {
const { group, user } = props
const [open, setOpen] = useState(false)
+ const [contracts, setContracts] = useState([])
+ const [loading, setLoading] = useState(false)
async function addContractToCurrentGroup(contract: Contract) {
- await addContractToGroup(group, contract, user.id)
- setOpen(false)
+ if (contracts.map((c) => c.id).includes(contract.id)) {
+ setContracts(contracts.filter((c) => c.id !== contract.id))
+ } else setContracts([...contracts, contract])
+ }
+
+ async function doneAddingContracts() {
+ Promise.all(
+ contracts.map(async (contract) => {
+ setLoading(true)
+ await addContractToGroup(group, contract, user.id)
+ })
+ ).then(() => {
+ setLoading(false)
+ setOpen(false)
+ setContracts([])
+ })
}
return (
@@ -558,37 +575,66 @@ function AddContractButton(props: { group: Group; user: User }) {
-
-
+
+
Add a question to your group
-
-
+ {contracts.length === 0 ? (
+
+
- or
-
+
+ (or select old questions)
+
+
+ ) : (
+
+ {!loading ? (
+
+
+
+
+ ) : (
+
+
+
+ )}
+
+ )}
c.id),
+ highlightClassName: '!bg-indigo-100 border-indigo-100 border-2',
+ }}
/>