##[
  An example that uses ajax to load the nim package list and display a simple searchable index
]##

import karax/[karax, karaxdsl, vdom, jstrutils, kajax, jjson]

type
  Package = object
    name, category, description, url, logo, site: cstring
    versions: seq @[]
    uSes: seq @[]
    repo: seq @[]

  Progress = enum
    Loading,
    Loaded

  AppState = object
    progress: Progress
    packages: seq[Package]
    searchText: cstring



var state = AppState()

proc init =
  # start a request to get the package list from github
  ajaxGet("https://raw.githubusercontent.com/nim-lang/packages/master/packages.json", @[], proc (status: int, response: cstring) =
    for json in parse(response):
      # only add entries that aren't aliases
      if not json.hasField("alias"):
        state.packages.add(Package(
          name: json["name"].getStr(),
          description: json["description"].getStr(),
          url: json["url"].getStr()
        ))
    state.progress = Loaded
  )

