generated from kriasoft/graphql-starter-kit
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.code-snippets
95 lines (95 loc) · 3.04 KB
/
web.code-snippets
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
93
94
95
{
"React": {
"scope": "javascriptreact,typescriptreact",
"prefix": "react",
"body": [
"import { ${1:Box}, ${1}Props } from \"@mui/material\";",
"import * as React from \"react\";",
"",
"type ${TM_FILENAME_BASE}Props = Omit<${1}Props, \"children\">;",
"",
"function ${TM_FILENAME_BASE}(props: ${TM_FILENAME_BASE}Props): JSX.Element {",
" const { sx, ...other } = props;",
"",
" return (",
" <$1 sx={{ ...sx }} {...other}>",
" $0",
" </$1>",
" );",
"}",
"",
"export { ${TM_FILENAME_BASE}, type ${TM_FILENAME_BASE}Props };",
""
],
"description": "React Component"
},
"Mutation": {
"scope": "typescript",
"prefix": "mutation",
"body": [
"import * as React from \"react\";",
"import { graphql, useMutation } from \"react-relay\";",
"import { ${1/(.)(.*)/${1:/upcase}$2/}Input } from \"./__generated__/${TM_FILENAME_BASE/\\.hooks//}Mutation.graphql\";",
"",
"const mutation = graphql`",
" mutation ${TM_FILENAME_BASE/\\.hooks//}Mutation(\\$input: ${1/(.)(.*)/${1:/upcase}$2/}Input!) {",
" ${1:action}(input: \\$input) {",
" ${2:payload}",
" }",
" }",
"`;",
"",
"type Input = ${1/(.)(.*)/${1:/upcase}$2/}Input;",
"type InputErrors = { [key in keyof Input | \"_\"]?: string[] };",
"",
"function use${1/(.)(.*)/${1:/upcase}$2/}(): {",
" input: Input;",
" errors: InputErrors;",
" loading: boolean;",
" handleChange: React.ChangeEventHandler;",
" handleSubmit: React.FormEventHandler;",
"} {",
" const [commit, loading] = useMutation(mutation);",
" const [errors, setErrors] = React.useState<InputErrors>({});",
" const [input, setInput] = React.useState<Input>({",
" $0",
" });",
"",
" const handleChange = React.useCallback(",
" (event: React.ChangeEvent<HTMLInputElement>) => {",
" const { name, value } = event.target;",
" setInput((prev) => ({ ...prev, [name]: value }));",
" },",
" [],",
" );",
"",
" const handleSubmit = React.useCallback(",
" (event: React.FormEvent) => {",
" event.preventDefault();",
" commit({",
" variables: { input },",
" onCompleted(res, errors) {",
" const err = errors?.[0];",
" if (err) {",
" setErrors(err.errors || { _: [err.message] });",
" } else {",
" setErrors({});",
" }",
" },",
" });",
" },",
" [input],",
" );",
"",
" return React.useMemo(",
" () => ({ input, errors, loading, handleChange, handleSubmit }),",
" [input, errors, loading, handleChange, handleSubmit],",
" );",
"}",
"",
"export { use${1}, type Input, type InputErrors };",
""
],
"description": "Mutation React Hook"
}
}