oc-template-elm-compiler 0.3.0 → 0.3.2

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.0",
3
+ "version": "0.3.2",
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.0",
33
+ "oc-vite-compiler": "^3.5.4",
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/Main.elm",
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
- { userId : Int, name : String, email : String, age : Maybe Int }
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
- { userId : Int }
38
+ { year : Int }
36
39
 
37
40
 
38
41
  encode : DataRequest -> E.Value
39
42
  encode data =
40
43
  E.object
41
- [ ( "userId", E.int data.userId )
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
- { name = "", email = "", userId = 0, age = Nothing }
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
- = GetData
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
- GetData ->
81
- ( model, DataRequest model.userId |> encode |> getData )
82
+ GetFunFact ->
83
+ ( model, DataRequest model.born |> encode |> askFunFact )
82
84
 
83
85
  Recv data ->
84
- ( case D.decodeValue decoder data of
86
+ ( case D.decodeValue factDecoder data of
85
87
  Ok newModel ->
86
- newModel
88
+ { model | funFact = Just newModel }
87
89
 
88
90
  Err _ ->
89
- { name = "", email = "", userId = 0, age = Nothing }
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
- [ css
122
- [ padding (px 20)
99
+ div [ css
100
+ [ backgroundColor (hex "3b246c")
101
+ , color (hex "fff")
102
+ , fontFamilies [ "sans-serif" ]
103
+ , padding (px 40)
123
104
  ]
124
105
  ]
125
- [ input
126
- [ css [ inputStyle ]
127
- , type_ "text"
128
- , placeholder "Name"
129
- , value model.name
130
- ]
131
- []
132
- , input
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 age ->
144
- input
145
- [ css [ inputStyle ]
146
- , type_ "number"
147
- , placeholder "Age"
148
- , String.fromInt age |> value
149
- ]
150
- []
151
- , button [ onClick GetData ] [ text "Get more data" ]
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
- dataReceiver Recv
144
+ getFunFact Recv
162
145
 
163
146
 
164
147
  decoder : D.Decoder Model
165
148
  decoder =
166
- D.map4 Model
167
- (D.field "userId" D.int)
168
- (D.field "name" D.string)
169
- (D.field "email" D.string)
170
- (D.maybe (D.field "age" D.int))
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 getData : E.Value -> Cmd msg
166
+ port askFunFact : E.Value -> Cmd msg
178
167
 
179
168
 
180
- port dataReceiver : (E.Value -> msg) -> Sub msg
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 database = [
4
- { name: 'John Doe', email: 'john@doe.com', age: 32 },
5
- { name: 'Jane Doe', email: 'jane@doe.com', age: 31 }
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 database[userId];
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; moreData?: boolean }) => {
13
- const { moreData, userId } = params;
14
- const { email, name, age } = await getUser(userId);
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
- userId,
18
- name,
19
- email,
20
- age: moreData && age
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
+ }