-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #350 from scottbenton/feat/add-rules-definition
Feat/add rules definition
- Loading branch information
Showing
55 changed files
with
2,620 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { deleteDoc } from "firebase/firestore"; | ||
import { getHomebrewCollectionDoc } from "./_getRef"; | ||
import { createApiFunction } from "api-calls/createApiFunction"; | ||
|
||
export const deleteHomebrewExpansion = createApiFunction<{ id: string }, void>( | ||
(params) => { | ||
const { id } = params; | ||
|
||
return new Promise((resolve, reject) => { | ||
const promises: Promise<unknown>[] = []; | ||
|
||
promises.push(deleteDoc(getHomebrewCollectionDoc(id))); | ||
|
||
Promise.all(promises) | ||
.then(() => { | ||
resolve(); | ||
}) | ||
.catch(reject); | ||
}); | ||
}, | ||
"Failed to delete expansion." | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { DocumentReference, doc } from "firebase/firestore"; | ||
import { constructHomebrewCollectionDocPath } from "../_getRef"; | ||
import { firestore } from "config/firebase.config"; | ||
import { StoredRules } from "types/HomebrewCollection.type"; | ||
|
||
export function constructHomebrewRulesDocPath(homebrewId: string) { | ||
return `${constructHomebrewCollectionDocPath(homebrewId)}/rules/rules`; | ||
} | ||
|
||
export function getHomebrewRulesDoc(homebrewId: string) { | ||
return doc( | ||
firestore, | ||
constructHomebrewRulesDocPath(homebrewId) | ||
) as DocumentReference<Partial<StoredRules>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { createApiFunction } from "api-calls/createApiFunction"; | ||
import { PartialWithFieldValue, updateDoc } from "firebase/firestore"; | ||
import { getHomebrewRulesDoc } from "./_getRef"; | ||
import { StoredRules } from "types/HomebrewCollection.type"; | ||
|
||
export const updateHomebrewRules = createApiFunction< | ||
{ | ||
homebrewId: string; | ||
rules: PartialWithFieldValue<Partial<StoredRules>>; | ||
}, | ||
void | ||
>((params) => { | ||
const { homebrewId, rules } = params; | ||
return new Promise((resolve, reject) => { | ||
updateDoc(getHomebrewRulesDoc(homebrewId), rules) | ||
.then(() => { | ||
resolve(); | ||
}) | ||
.catch(reject); | ||
}); | ||
}, "Failed to update rules."); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { onSnapshot } from "firebase/firestore"; | ||
import { StoredRules } from "types/HomebrewCollection.type"; | ||
import { getHomebrewRulesDoc } from "./_getRef"; | ||
|
||
export function listenToHomebrewRules( | ||
homebrewId: string, | ||
updateRules: (collectionId: string, rules: Partial<StoredRules>) => void, | ||
onError: (error: unknown) => void, | ||
onLoaded: () => void | ||
) { | ||
return onSnapshot( | ||
getHomebrewRulesDoc(homebrewId), | ||
(snapshot) => { | ||
const doc = snapshot.data(); | ||
if (doc) { | ||
updateRules(homebrewId, doc); | ||
} else { | ||
onLoaded(); | ||
} | ||
}, | ||
(error) => { | ||
console.error(error); | ||
onError(error); | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { createApiFunction } from "api-calls/createApiFunction"; | ||
import { PartialWithFieldValue, setDoc } from "firebase/firestore"; | ||
import { getHomebrewRulesDoc } from "./_getRef"; | ||
import { StoredRules } from "types/HomebrewCollection.type"; | ||
|
||
export const updateExpansionRules = createApiFunction< | ||
{ | ||
homebrewId: string; | ||
rules: PartialWithFieldValue<StoredRules>; | ||
}, | ||
void | ||
>((params) => { | ||
const { homebrewId, rules } = params; | ||
return new Promise((resolve, reject) => { | ||
setDoc(getHomebrewRulesDoc(homebrewId), rules, { merge: true }) | ||
.then(() => { | ||
resolve(); | ||
}) | ||
.catch(reject); | ||
}); | ||
}, "Failed to update rules."); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { updateDoc } from "firebase/firestore"; | ||
import { getHomebrewCollectionDoc } from "./_getRef"; | ||
import { BaseExpansion } from "types/HomebrewCollection.type"; | ||
import { createApiFunction } from "api-calls/createApiFunction"; | ||
|
||
export const updateHomebrewExpansion = createApiFunction< | ||
{ id: string; expansion: Partial<BaseExpansion> }, | ||
void | ||
>((params) => { | ||
const { id, expansion } = params; | ||
|
||
return new Promise((resolve, reject) => { | ||
updateDoc(getHomebrewCollectionDoc(id), expansion) | ||
.then(() => resolve()) | ||
.catch(reject); | ||
}); | ||
}, "Failed to update expansion."); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Box } from "@mui/material"; | ||
import { MarkdownRenderer, MarkdownRendererProps } from "./MarkdownRenderer"; | ||
|
||
export interface ClampedMarkdownRendererProps extends MarkdownRendererProps { | ||
maxLines?: number; | ||
} | ||
|
||
export function ClampedMarkdownRenderer(props: ClampedMarkdownRendererProps) { | ||
const { maxLines = 1, ...markdownRendererProps } = props; | ||
return ( | ||
<Box | ||
component={"span"} | ||
sx={{ | ||
display: "-webkit-box", | ||
WebkitBoxOrient: "vertical", | ||
WebkitLineClamp: maxLines, | ||
overflow: "hidden", | ||
}} | ||
> | ||
<MarkdownRenderer {...markdownRendererProps} /> | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { useEditor } from "@tiptap/react"; | ||
import StarterKit from "@tiptap/starter-kit"; | ||
import { Markdown } from "tiptap-markdown"; | ||
import { Editor } from "./Editor"; | ||
import { Box, Typography } from "@mui/material"; | ||
import { MarkdownEditorToolbar } from "./MarkdownEditorToolbar"; | ||
export interface MarkdownEditorProps { | ||
label: string; | ||
content: string; | ||
onChange: (markdown: string) => void; | ||
onBlur: () => void; | ||
} | ||
|
||
export function MarkdownEditor(props: MarkdownEditorProps) { | ||
const { label, content, onChange, onBlur } = props; | ||
|
||
const editor = useEditor( | ||
{ | ||
extensions: [StarterKit, Markdown], | ||
content, | ||
onBlur, | ||
onUpdate: ({ editor }) => { | ||
const markdown = editor.storage.markdown.getMarkdown(); | ||
onChange(markdown); | ||
}, | ||
}, | ||
[] | ||
); | ||
|
||
return ( | ||
<Editor | ||
toolbar={ | ||
<> | ||
<Box | ||
display={"flex"} | ||
position={"relative"} | ||
sx={(theme) => ({ | ||
top: theme.spacing(-1.5), | ||
left: theme.spacing(1), | ||
})} | ||
> | ||
<Typography | ||
component={"span"} | ||
variant={"caption"} | ||
color={"text.secondary"} | ||
sx={{ | ||
px: 1, | ||
bgcolor: "background.paper", | ||
}} | ||
> | ||
{label} | ||
</Typography> | ||
</Box> | ||
<MarkdownEditorToolbar editor={editor} /> | ||
</> | ||
} | ||
editor={editor} | ||
editable | ||
outlined | ||
minHeight={100} | ||
/> | ||
); | ||
} |
Oops, something went wrong.