oc-template-elm-compiler 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oc-template-elm-compiler",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Compiler for the Elm OC template",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"elm": "^0.19.1-6",
|
|
32
32
|
"oc-template-elm": "0.1.1",
|
|
33
|
-
"oc-vite-compiler": "^3.5.
|
|
33
|
+
"oc-vite-compiler": "^3.5.2",
|
|
34
34
|
"vite-plugin-elm": "^2.8.0"
|
|
35
35
|
},
|
|
36
36
|
"files": [
|
|
@@ -7,9 +7,12 @@
|
|
|
7
7
|
"files": {
|
|
8
8
|
"data": "src/server.ts",
|
|
9
9
|
"template": {
|
|
10
|
-
"src": "src/
|
|
10
|
+
"src": "src/index.js",
|
|
11
11
|
"type": "oc-template-elm"
|
|
12
|
-
}
|
|
12
|
+
},
|
|
13
|
+
"static": [
|
|
14
|
+
"public"
|
|
15
|
+
]
|
|
13
16
|
},
|
|
14
17
|
"parameters": {
|
|
15
18
|
"userId": {
|
|
@@ -23,9 +26,9 @@
|
|
|
23
26
|
},
|
|
24
27
|
"dependencies": {},
|
|
25
28
|
"devDependencies": {
|
|
26
|
-
"oc-server": "
|
|
27
|
-
"oc-template-elm-compiler": "
|
|
28
|
-
"elm": "
|
|
29
|
-
"typescript": "
|
|
29
|
+
"oc-server": "^0.3.0",
|
|
30
|
+
"oc-template-elm-compiler": "^0.3.0",
|
|
31
|
+
"elm": "^0.19.1-6",
|
|
32
|
+
"typescript": "^5.3.3"
|
|
30
33
|
}
|
|
31
34
|
}
|
|
Binary file
|
|
@@ -28,18 +28,20 @@ main =
|
|
|
28
28
|
|
|
29
29
|
|
|
30
30
|
type alias Model =
|
|
31
|
-
{
|
|
31
|
+
{ staticPath: String, firstName : String, lastName : String, born : Int, hobbies : List String, funFact: Maybe String }
|
|
32
|
+
|
|
33
|
+
type alias FactModel =
|
|
34
|
+
{ funFact: String }
|
|
32
35
|
|
|
33
36
|
|
|
34
37
|
type alias DataRequest =
|
|
35
|
-
{
|
|
38
|
+
{ year : Int }
|
|
36
39
|
|
|
37
40
|
|
|
38
41
|
encode : DataRequest -> E.Value
|
|
39
42
|
encode data =
|
|
40
43
|
E.object
|
|
41
|
-
[ ( "
|
|
42
|
-
, ( "moreData", E.bool True )
|
|
44
|
+
[ ( "year", E.int data.year )
|
|
43
45
|
]
|
|
44
46
|
|
|
45
47
|
|
|
@@ -50,7 +52,7 @@ init flags =
|
|
|
50
52
|
model
|
|
51
53
|
|
|
52
54
|
Err _ ->
|
|
53
|
-
{
|
|
55
|
+
{ staticPath = "", firstName = "", lastName = "", born = 0, hobbies = [], funFact = Nothing }
|
|
54
56
|
, Cmd.none
|
|
55
57
|
)
|
|
56
58
|
|
|
@@ -70,111 +72,98 @@ isJust data =
|
|
|
70
72
|
|
|
71
73
|
|
|
72
74
|
type Msg
|
|
73
|
-
=
|
|
75
|
+
= GetFunFact
|
|
74
76
|
| Recv E.Value
|
|
75
77
|
|
|
76
78
|
|
|
77
79
|
update : Msg -> Model -> ( Model, Cmd Msg )
|
|
78
80
|
update msg model =
|
|
79
81
|
case msg of
|
|
80
|
-
|
|
81
|
-
( model, DataRequest model.
|
|
82
|
+
GetFunFact ->
|
|
83
|
+
( model, DataRequest model.born |> encode |> askFunFact )
|
|
82
84
|
|
|
83
85
|
Recv data ->
|
|
84
|
-
( case D.decodeValue
|
|
86
|
+
( case D.decodeValue factDecoder data of
|
|
85
87
|
Ok newModel ->
|
|
86
|
-
newModel
|
|
88
|
+
{ model | funFact = Just newModel }
|
|
87
89
|
|
|
88
90
|
Err _ ->
|
|
89
|
-
{
|
|
91
|
+
{ staticPath = "", firstName = "", lastName = "", born = 0, hobbies = [], funFact = Nothing }
|
|
90
92
|
, Cmd.none
|
|
91
93
|
)
|
|
92
94
|
|
|
93
|
-
|
|
94
|
-
|
|
95
95
|
-- VIEW
|
|
96
96
|
|
|
97
|
-
|
|
98
|
-
inputStyle : Style
|
|
99
|
-
inputStyle =
|
|
100
|
-
Css.batch
|
|
101
|
-
[ border3 (px 5) solid (rgb 255 240 255)
|
|
102
|
-
, boxShadow5 inset zero zero (px 8) (rgba 0 0 0 0.1)
|
|
103
|
-
, padding (px 15)
|
|
104
|
-
, backgroundColor (rgba 255 255 255 0.5)
|
|
105
|
-
, margin (px 10)
|
|
106
|
-
]
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
htmlIf : Html msg -> Bool -> Html msg
|
|
110
|
-
htmlIf el cond =
|
|
111
|
-
if cond then
|
|
112
|
-
el
|
|
113
|
-
|
|
114
|
-
else
|
|
115
|
-
text ""
|
|
116
|
-
|
|
117
|
-
|
|
118
97
|
view : Model -> Html Msg
|
|
119
98
|
view model =
|
|
120
|
-
div
|
|
121
|
-
|
|
122
|
-
|
|
99
|
+
div [ css
|
|
100
|
+
[ backgroundColor (hex "3b246c")
|
|
101
|
+
, color (hex "fff")
|
|
102
|
+
, fontFamilies [ "sans-serif" ]
|
|
103
|
+
, padding (px 40)
|
|
123
104
|
]
|
|
124
105
|
]
|
|
125
|
-
[
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
,
|
|
129
|
-
,
|
|
130
|
-
|
|
131
|
-
[]
|
|
132
|
-
|
|
133
|
-
[ css [ inputStyle ]
|
|
134
|
-
, type_ "text"
|
|
135
|
-
, placeholder "Email"
|
|
136
|
-
, value model.email
|
|
106
|
+
[ img [ Html.Styled.Attributes.width 50, Html.Styled.Attributes.height 50, src (model.staticPath ++ "public/logo.png"), alt "Logo" ] []
|
|
107
|
+
, h1 [ css [ margin4 (px 0) (px 0) (px 20) (px 0) ] ]
|
|
108
|
+
[ text ("Hello, ")
|
|
109
|
+
, span [ css [ textDecoration underline ] ] [text (model.firstName)]
|
|
110
|
+
, text( " " ++ model.lastName ) ]
|
|
111
|
+
, div [ css [ marginBottom (px 20) ] ]
|
|
112
|
+
[ div [ css [ margin2 (px 6) (px 0) ] ] [ text ("Born: " ++ String.fromInt model.born) ]
|
|
113
|
+
, div [ css [ margin2 (px 6) (px 0) ] ] [ text ("Hobbies: " ++ String.join ", " (List.map String.toLower model.hobbies)) ]
|
|
137
114
|
]
|
|
138
|
-
|
|
139
|
-
, case model.age of
|
|
115
|
+
, case model.funFact of
|
|
140
116
|
Nothing ->
|
|
141
117
|
text ""
|
|
142
118
|
|
|
143
|
-
Just
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
119
|
+
Just fact ->
|
|
120
|
+
div [] [text (fact)]
|
|
121
|
+
, button [ css
|
|
122
|
+
[ backgroundColor (hex "a97613")
|
|
123
|
+
, border (px 0)
|
|
124
|
+
, padding2 (px 15) (px 32)
|
|
125
|
+
, textAlign center
|
|
126
|
+
, fontSize (px 16)
|
|
127
|
+
, textDecoration none
|
|
128
|
+
, display inlineBlock
|
|
129
|
+
, color inherit
|
|
130
|
+
, cursor pointer
|
|
131
|
+
]
|
|
132
|
+
, onClick GetFunFact
|
|
133
|
+
]
|
|
134
|
+
[ text "Fun year fact" ]
|
|
135
|
+
|
|
152
136
|
]
|
|
153
137
|
|
|
154
138
|
|
|
155
|
-
|
|
156
139
|
-- SUBSCRIPTIONS
|
|
157
140
|
|
|
158
141
|
|
|
159
142
|
subscriptions : Model -> Sub Msg
|
|
160
143
|
subscriptions _ =
|
|
161
|
-
|
|
144
|
+
getFunFact Recv
|
|
162
145
|
|
|
163
146
|
|
|
164
147
|
decoder : D.Decoder Model
|
|
165
148
|
decoder =
|
|
166
|
-
D.
|
|
167
|
-
(D.field "
|
|
168
|
-
(D.field "
|
|
169
|
-
(D.field "
|
|
170
|
-
(D.
|
|
149
|
+
D.map6 Model
|
|
150
|
+
(D.field "staticPath" D.string)
|
|
151
|
+
(D.field "firstName" D.string)
|
|
152
|
+
(D.field "lastName" D.string)
|
|
153
|
+
(D.field "born" D.int)
|
|
154
|
+
(D.field "hobbies" (D.list D.string))
|
|
155
|
+
(D.maybe (D.field "funFact" D.string))
|
|
171
156
|
|
|
157
|
+
-- decoder of a single string field
|
|
158
|
+
factDecoder : D.Decoder String
|
|
159
|
+
factDecoder =
|
|
160
|
+
D.field "funFact" D.string
|
|
172
161
|
|
|
173
162
|
|
|
174
163
|
-- PORTS
|
|
175
164
|
|
|
176
165
|
|
|
177
|
-
port
|
|
166
|
+
port askFunFact : E.Value -> Cmd msg
|
|
178
167
|
|
|
179
168
|
|
|
180
|
-
port
|
|
169
|
+
port getFunFact : (E.Value -> msg) -> Sub msg
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { serverClient } from 'oc-server';
|
|
2
|
+
import { Elm } from './Main.elm';
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
js: (app) => {
|
|
6
|
+
app.ports.askFunFact.subscribe(async (params) => {
|
|
7
|
+
const data = await serverClient.funFact(params);
|
|
8
|
+
app.ports.getFunFact.send(data);
|
|
9
|
+
});
|
|
10
|
+
},
|
|
11
|
+
program: Elm.Main,
|
|
12
|
+
};
|
|
@@ -1,22 +1,51 @@
|
|
|
1
1
|
import { Server } from 'oc-server';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
{ name: 'John Doe',
|
|
5
|
-
{ name: 'Jane Doe',
|
|
3
|
+
const userDatabase = [
|
|
4
|
+
{ name: 'John Doe', born: 1986, hobbies: ['Swimming', 'Basketball'] },
|
|
5
|
+
{ name: 'Jane Doe', born: 1991, hobbies: ['Running', 'Rugby'] },
|
|
6
6
|
];
|
|
7
7
|
|
|
8
|
+
const yearsFunFactDatabase: Record<number, string> = {
|
|
9
|
+
1986: "Halley's Comet made its closest approach to Earth in 1986, visible to the naked eye.",
|
|
10
|
+
1987: 'The first Final Fantasy game was released in 1987.',
|
|
11
|
+
1991: 'The first web page was created in 1991 by Tim Berners-Lee.',
|
|
12
|
+
};
|
|
13
|
+
|
|
8
14
|
async function getUser(userId: number) {
|
|
9
|
-
return
|
|
15
|
+
return userDatabase[userId];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function getFunFact(year: number) {
|
|
19
|
+
return yearsFunFactDatabase[year];
|
|
10
20
|
}
|
|
11
21
|
|
|
12
|
-
export const server = new Server(async (params: { userId: number
|
|
13
|
-
const {
|
|
14
|
-
const
|
|
22
|
+
export const server = new Server(async (params: { userId: number }, ctx) => {
|
|
23
|
+
const { userId } = params;
|
|
24
|
+
const user = await getUser(userId);
|
|
25
|
+
const [firstName, lastName] = user.name.split(/\s+/);
|
|
26
|
+
|
|
27
|
+
if (firstName === 'Invalid') {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
15
30
|
|
|
16
31
|
return {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
32
|
+
firstName,
|
|
33
|
+
lastName,
|
|
34
|
+
born: user.born,
|
|
35
|
+
hobbies: user.hobbies,
|
|
36
|
+
staticPath: ctx.staticPath,
|
|
37
|
+
};
|
|
38
|
+
}).action('funFact', async (params: { year: number }) => {
|
|
39
|
+
const { year } = params;
|
|
40
|
+
const funFact = await getFunFact(year);
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
funFact,
|
|
21
44
|
};
|
|
22
45
|
});
|
|
46
|
+
|
|
47
|
+
declare module 'oc-server' {
|
|
48
|
+
interface Register {
|
|
49
|
+
server: typeof server;
|
|
50
|
+
}
|
|
51
|
+
}
|